Arduino – Control Servo with Visual Basic

Today I’ll show how you can control a servo with Visual basic. Before this project I’ve never used Visual basic so if anyone finds any mistake in my code please leave a comment here and help me improve it.

I’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’ve created. Basically in the VB program we have 4 buttons that will interact with the Arduino when we press them.

I’ll 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:

  • Red: 3.3V (depends on your Servo motor)
  • Brown: Ground
  • Orange: Digital Pin 9

Parts required

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Upload the Arduino code below

/*
 * Control a servo motor with Visual Basic 
 * Created by Rui Santos, https://randomnerdtutorials.com
*/
 
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600); //begins serial communication
} 
  
void loop() 
{ 
  int pos;
  if (Serial.available()){
    delay(100);
    while(Serial.available()>0){
      pos=Serial.read();     //reads the value sent from Visual Basic  
      if(pos=='0')
        myservo.write(90);   //rotates the servo 90 degrees (Left)
      else if(pos=='1')
        myservo.write(-90);  //rotates the servo 90 degrees (right)
      else if(pos=='2')
        myservo.write(180);  //rotates the servo 180 degrees (Left)
      else if(pos=='3')
        myservo.write(-180); //rotates the servo 180 degrees (right)     
    }
  } 
} 

View raw code

Run the Visual Basic script

Click here to download the Visual Basic Script

NOTE:

  • When you’re using the Visual Basic Program the Serial monitor on the Arduino IDE must be closed
  • You need to match the com port on your Visual Basic Code to the right com port you’re Arduino is using

I don’t know why, but YouTube cropped my video, the tast two buttons are “Rotate 180º Left” and “Rotate 180º right” .

Watch the video demonstration

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 Facebook Page.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

34 thoughts on “Arduino – Control Servo with Visual Basic”

  1. yes it’s possible to do that! you can attach 2 servos or more.
    I based my arduino code in the example from the arduino IDE called ‘sweep’.
    but for a pan tilt comand you to change the code a bit, you will need to move the servo from degree to degree when the key is pressed on VB, instead of rotating 90 or 180 degrees at once.
    if you know something about VB you just need to addapt the buttons to do whant you want. (turn: right, left, up and down) basically you just need to change the text name from my buttons.
    the most work you need to do it’s on the arduino code

    Reply
    • Hi .Can you please try to do a little modification with the vb which can allow user to enter their own value for the servo to tilt. I need it for my research thnx =)

      Reply
    • Hi,
      I have a question regarding Arduino. In my new project, I want to use Arduino to control 4 relays based on some stored value in the EEPROM of the Arduino. I was thinking of using either nano or mirco which have a USB connector. The issue is that when the system is delivered, the user needs to occasionally change the EEPROM data inside the Arduino. I was thinking of writing a C++, JAVA or VB program for the PC that communicates with Arduino via USB. Is this possible? Do I need a USB shield or can I use the existing USB connector on the Arduino nano/micro? Do you have any example or tech info on this please?
      Thanks,
      Sharok

      Reply
    • yeah, it looks like a C# family language, but i’ve used the Visual basic IDE to write the code… and the buttons they create automatically you just need to drag and drop, then edit the name of the object

      Reply
  2. Hello Rui,
    I have to change the com#. You used com6 in the VB program but my Arduino is connected to com3.
    Can I use the free “Visual Studio Express for Windows Desktop” to do this?
    Thanks,
    – Jaap

    PS: I think the “Notify me of follow-up comments by email” option of your blog is not working…

    Reply
    • Hi Jaap.
      Thanks for letting me know about the “Notify me…” function.
      I’ll see what I can do to fix :S

      I’m not sure if it will work with that version …
      The easiest way is to try my project and see if it works…
      You right you need change the com port, otherwise visual basic won’t be able to communicate with your arduino…

      All the best,
      Rui

      Reply
  3. Rui,

    I tried and Yes “Visual Studio Express for Windows Desktop” works fine to make some changes to the VB code.

    It works fine now.

    Thanks!

    – Jaap

    Reply
  4. Hi , your code gives an error like “invalid outside procedure” when I try to run it. I’m new to VB. What exactly am I doing wrong???

    Reply
  5. hey I build a glider and i put servos on it for my school project and ii need to use visual basic as a requirement, I was wondering if you could help me out with the code I’m not sure how to connect my arduino to visual basic and how to upload the code if you will be so kind to give me your website where i can see your code because I can’t open an RAR file. thanks

    Reply
    • Hi Jose,
      why can’t you open the .rar file?
      I’ve just downloaded the .rar file myself just fine… please try again.

      You don’t upload the Visual basic code into the arduino.
      The VB code keeps running on your laptop only.
      And it talks to your Arduino via serial communication.

      Let me know how can I help!

      Reply
  6. Hi¡
    Sorry but the VB Script link isn’t working.
    It would be possible to update the link, or you may send me the VB script?
    Thanks,
    Regards Nico.

    Reply
  7. Hi Rui,
    Thanks for your tutorial.

    Is there a way to have :
    > A single command (instead of 4 in your exemple) with variable number (45, 90, 180 …. for exemple)
    > getting this variable number from a spreadsheet (excel for exemple)
    > keeping just on / off button in VB and servo obeying automaticcaly to the variable spreadsheet cell content (45, 90, …)

    Thanks in advance !

    Reply
  8. Hi!
    I am trying to run a DC Brushless motor using similar method. Is there any quick fix changes to the code and circuit that I can make to your project and run a DC brushless motor?

    Thanks!

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.