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)
}
}
}
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.
hello possible program basic for two servos ? pan-tilt command ? regards frank
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
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 =)
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
how to controll wifi arduino on visual basic code?
Hi, actually I never tried that, i know it’s possible but I would need to search bit to be able to do that… Sorry
thanks for reply, actually i’m a technician, i know the connection but not on programming so can you research for me using visual basic to ethertnet shield connection?
I’m sorry actually I don’t have time to do that, I have 3 projects running right now and one contest, otherwise i would do some research about that…
que versao de arduino usou ?
utilizei o Arduino Uno, mas funciona com as outras versões
The code doesn’t look like VB 🙂 The grammar suggests the C family (C# probably).
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
Buenas tardes disculpa quisiera que me mandaras tu código de VB, el motivo es por que el linck esta caído. Gracias de antemano
Hi! the download link was already working fine.
Anyway go ahead and download the code here:
https://app.box.com/s/2663yu97s9to5i1snw9o
Thanks
where is the code?
Hi Mohamed,
you can either download the code by clicking “Like”, “tweet” or “+1”.
Or simply go to this page:
https://randomnerdtutorials.com/download
and you’ll receive access to all my project and future updates for free.
Thanks for taking the time to read my project,
Rui Santos
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…
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
Rui,
Notifications work fine now!
Thanks!
– Jaap
That’s awesome Jaap!
Thanks for trying my projects!
and I’m glad I could fix the notifications.
All the best,
Rui
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
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???
Hi Al,
thanks for trying my project.
Can you tell me more about that error?
It never happened to me…
First , I want to know what visual basic you used in this project so I can download it now. I’ll try to run it here on my pc. I tried that on my friends laptop.
I made this project with Microsoft Visual Basic 2010.
Have a nice day,
Rui
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
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!
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.
I’ve just tested the download link and it works fine for me.
Please try again! thank you Nico
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 !
Hi Thierry!
Yes, you can modify my VB code to do exactly what you’re looking for.
Thanks for asking,
Rui
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!
is it possible to have 5 servo motors ??
Yes, the Arduino has that amount of PWM pins.
Thanks,
Rui