Introduction: Arduino - Control Servo With Visual Basic

Hi guys,

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.

you can visit my website for more electronic projects, interesting news and tips:
http://randomnerdtutorials.com/

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

   Click here to download the VB code
   Click here to download the Arduino Sketch

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 Last two buttons are "Rotate 180º Left" and "Rotate 180º right" . anyway watch it working :)


If you have any questions leave a comment!
Thanks

Comments

author
JimsonN1 made it!(author)2017-01-02

Hi Sir, i have my project using Visual basic connected to an arduino to drive 5led and 5 servo motors to do a sweep (once) movement. can someone help me to figure out why my servos are not rotating? the led are working well except for the servos which likely receiving a pulse signal only. i already tried sweep but not activating when triggered by serial from my vb program. below is my code: appreciate very much your help..

#include <Servo.h>

Servo myservo1;

Servo myservo2;

Servo myservo3;

Servo myservo4;

Servo myservo5;

int ledPin1 = 13;

int ledPin2 = 12;

int ledPin3 = 11;

int ledPin4 = 10;

int ledPin5 = 9;

void setup() {

Serial.begin(9600); // set serial spee

pinMode(ledPin1, OUTPUT); // set LED as output

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

pinMode(ledPin4, OUTPUT);

pinMode(ledPin5, OUTPUT);

digitalWrite(ledPin1, LOW); //turn off LED

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

digitalWrite(ledPin4,LOW);

digitalWrite(ledPin5,LOW);

myservo1.attach(1);

myservo2.attach(2);

myservo3.attach(3);

myservo4.attach(4);

myservo5.attach(5);

}

void loop()

{

while (Serial.available() == 0); // do nothing if nothing sent

int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

if (val == 1) {

Serial.println("LED on");

digitalWrite(ledPin1, HIGH);

delay(500);

myservo1.write(180);

delay(20);

myservo1.write(0);

delay(20);

digitalWrite(ledPin1, LOW);

}

else if (val == 2) {

Serial.println("LED on");

digitalWrite(ledPin2, HIGH);

delay(500);

myservo2.write(180);

delay(20);

myservo2.write(0);

delay(20);

digitalWrite(ledPin2, LOW);

}

else if (val == 3) {

Serial.println("LED on");

digitalWrite(ledPin3, HIGH);

delay(500);

myservo3.write(180);

delay(20);

myservo3.write(0);

delay(20);

digitalWrite(ledPin3, LOW);

}

else if (val == 4) {

Serial.println("LED on");

digitalWrite(ledPin4, HIGH);

delay(500);

myservo4.write(180);

delay(20);

myservo4.write(0);

delay(20);

digitalWrite(ledPin4, LOW);

}

else if (val == 5) {

Serial.println("LED on");

digitalWrite(ledPin5, HIGH);

delay(500);

myservo5.write(180);

delay(20);

myservo5.write(0);

delay(20);

digitalWrite(ledPin5, LOW);

}else

{

//val = val;

}

Serial.println(val);

Serial.flush();

}

author
PrimaA made it!(author)2016-02-24

Hi..

how to reduce speed movement the servo?

i have tried to use delay. but it not success.

any idea?

author
shristee made it!(author)2015-12-29

Thank you for this tutorial.i successfully running the single servo.

how to connect the more than four servo to vb +arduino

author
JustinF30 made it!(author)2015-12-16

You could cut the VB code down 1/3 by adding this:

Sub send(number As String)

SerialPort1.Open()

SerialPort1.Write(num)

SerialPort1.Close()

End Sub

call it like this: send("1")

author
akruti2 made it!(author)2015-05-15

here book is available to make some basic cool project with arduino + visual basic 6.0

http://www.amazon.com/Arduino-Visual-Basic-6-0-software/dp/1511667753

author
btu%C4%9Fcu made it!(author)2015-04-05

Hi i couldnt upload arduino port driver. i'we installed but does not appear in the device manager

author
AMK93 made it!(author)2014-12-11

Hi,
i had tried this with two servos but a problem was found which is the buttons won't work at the same time.
any help please?

author
C%C3%A9sar%C3%811 made it!(author)2014-10-23

Hii!, What Visual Basic Do you used in this proyect?

Because I have a Visual Basic 6 But I can´t see the visual basic code.

author
C%C3%A9sar%C3%811 made it!(author)2014-10-23

Hii!, What Visual Basic Do you used in this proyect?

Because I have a Visual Basic 6 But I can´t see the visual basic code.

author
greatidea made it!(author)2014-09-17

Excellent tutorial. I succeeded in properly running this.

author
bsairam made it!(author)2014-04-28

ur stuff is really nice, can u make the vb code for the following video

for my home purpose

thanx in advance

author
Fyodor made it!(author)2014-04-04

thanks, but I wish this vid was longer. I'd like an explanation of how VB is interacting with the Arduino, the concept is fascinating.

author
asyukriadinata made it!(author)2014-03-06

How to connect VB to Arduino ?

author
allc1865 made it!(author)2014-02-12

Hi, how can I download the source code for this project?

The source code link provided does not allow me to download the file.

Thanks! :)

author
RuiSantos made it!(author)2014-02-12

What's exactly you mean ?

https://app.box.com/s/2663yu97s9to5i1snw9o

https://gist.github.com/ruisantos16/4773241

I've just tried and both links works fine for me :)

Please try again,

Rui

author
allc1865 made it!(author)2014-02-13

Hi thank you for the links. The first one:

https://app.box.com/s/2663yu97s9to5i1snw9o

downloads as a internet explorer file which does not allow me to download the code.

The second one, however, https://gist.github.com/ruisantos16/4773241

is good enough, but where's the servo.h file?

Thanks! :)

author
RuiSantos made it!(author)2014-02-13

Try to download with mozilla or something
https://app.box.com/s/2663yu97s9to5i1snw9o

That file is hosted by box.com. (similar to dropbox, and that folder is shared so anyone can download... try with another browser, because everyone downloaded the code successfully.. :) ).

the servo.h file is included in the Arduino IDE (1.0+) by default.

I hope this helps,

Rui

author
BDS29 made it!(author)2014-02-17

Hey guy,

Thank you for all your job, it's really interesting.

However, I don't really understand some step of the program. For example, why do you differenciate if the angle is under 10 or 100?

I would like to use your code in order to create a sinusoidal movement, do you think that's possible?

Thank You,

BDS29

About This Instructable

41,839views

64favorites

License:

Bio: Author, Blogger, Electronics Enthusiast and Entrepreneur. For complete Electronics Projects and Tutorials go to: http://randomnerdtutorials.com/start-here
More by RuiSantos:ESP8266 Web Server (Without Arduino)Arduino Simple Memory GameArduino – Datalogger with Temperature Sensor and Photoresistor
Add instructable to: