{"id":336,"date":"2013-02-12T20:42:30","date_gmt":"2013-02-12T20:42:30","guid":{"rendered":"http:\/\/randomnerdtutorials.wordpress.com\/?p=336"},"modified":"2019-04-02T10:23:40","modified_gmt":"2019-04-02T10:23:40","slug":"arduino-control-servo-with-visual-basic","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-control-servo-with-visual-basic\/","title":{"rendered":"Arduino &#8211; Control Servo with Visual Basic"},"content":{"rendered":"<p>Today I&#8217;ll show how you can control a servo with Visual basic. Before this project I\u2019ve never used Visual basic so if anyone finds any mistake\u00a0in my code please leave a comment here and help me improve it.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-977\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/4thumbVB.png?resize=699%2C393&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"699\" height=\"393\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/4thumbVB.png?w=1366&amp;quality=100&amp;strip=all&amp;ssl=1 1366w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/4thumbVB.png?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/4thumbVB.png?resize=1024%2C575&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/p>\n<p>I&#8217;ve always used the Serial Monitor of the Arduino IDE to communicate with the Arduino, but today we will use a visual basic program that I&#8217;ve created.\u00a0Basically\u00a0in the VB program we have 4 buttons that will interact with the Arduino when we press them.<\/p>\n<p>I\u2019ll be showing program in Visual Basic that allows the user to rotate a servo attached to the Arduino. You need to make 3 connections from the servo to your arduino:<\/p>\n<ul>\n<li><span style=\"line-height: 16px;\">Red: 3.3V (depends on your Servo motor)<\/span><\/li>\n<li>Brown: Ground<\/li>\n<li>Orange: Digital Pin 9<\/li>\n<\/ul>\n<h2>Parts required<\/h2>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/micro-servo-motor-tool\/\" target=\"_blank\" rel=\"noopener noreferrer\">Servo motor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Upload the Arduino code below<\/h2>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Control a servo motor with Visual Basic \n * Created by Rui Santos, http:\/\/randomnerdtutorials.com\n*\/\n \n#include &lt;Servo.h&gt; \n \nServo myservo;  \/\/ create servo object to control a servo \n \nvoid setup() \n{ \n  myservo.attach(9);  \/\/ attaches the servo on pin 9 to the servo object \n  Serial.begin(9600); \/\/begins serial communication\n} \n  \nvoid loop() \n{ \n  int pos;\n  if (Serial.available()){\n    delay(100);\n    while(Serial.available()&gt;0){\n      pos=Serial.read();     \/\/reads the value sent from Visual Basic  \n      if(pos=='0')\n        myservo.write(90);   \/\/rotates the servo 90 degrees (Left)\n      else if(pos=='1')\n        myservo.write(-90);  \/\/rotates the servo 90 degrees (right)\n      else if(pos=='2')\n        myservo.write(180);  \/\/rotates the servo 180 degrees (Left)\n      else if(pos=='3')\n        myservo.write(-180); \/\/rotates the servo 180 degrees (right)     \n    }\n  } \n} \n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Control_Servo_with_Visual_Basic.c\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Run the Visual Basic script<\/h2>\n<p><strong><a title=\"Click Here to download the VB code\" href=\"https:\/\/www.box.com\/s\/2663yu97s9to5i1snw9o\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Click here to download the Visual Basic Script<\/a><\/strong><\/p>\n<p><strong>NOTE:<\/strong><\/p>\n<ul>\n<li><span style=\"line-height: 16px;\">When you&#8217;re using the Visual Basic Program the Serial monitor on the Arduino IDE must be closed<\/span><\/li>\n<li>You need to match the com port on your Visual Basic Code to the right com port you&#8217;re Arduino is using<\/li>\n<\/ul>\n<p>I don&#8217;t know why, but YouTube cropped my video, the tast two buttons are\u00a0<strong>&#8220;Rotate 180\u00ba Left&#8221; <\/strong>and<strong> &#8220;Rotate 180\u00ba right&#8221; .<\/strong><\/p>\n<h2>Watch the video demonstration<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/rza_ujeK398?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my <a href=\"https:\/\/www.facebook.com\/RandomNerdTutorials?ref=hl\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Facebook Page.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I&#8217;ll show how you can control a servo with Visual basic. Before this project I\u2019ve never used Visual basic so if anyone finds any mistake\u00a0in my code please leave &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Arduino &#8211; Control Servo with Visual Basic\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/arduino-control-servo-with-visual-basic\/#more-336\" aria-label=\"Read more about Arduino &#8211; Control Servo with Visual Basic\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":977,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,2,267,264,10],"tags":[241,31,50,71,80,138,163,166,171],"class_list":["post-336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-arduino-project","category-project","category-a-tutorials","tag-arduino","tag-basic","tag-control","tag-free","tag-homemade","tag-servo","tag-tutorial","tag-uno","tag-visual"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/07\/4thumbVB.png?fit=1366%2C768&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/977"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}