Introduction: Using VB to Control Arduino

Picture of Using VB to Control Arduino

I know there are many instructables on this topic, but I couldn't find one which doesn't required edit the code to add the serial port.

So, I made one program that will ask the serial port and baud rate to use on startup.

Step 1: Requirements

  1. Any Arduino (I am using Arduino Uno)
  2. Visual Basic Express 2010

You can download VB from here

Step 2: Making the VB Program

Picture of Making the VB Program

In this instructable, we will be making a program to control leds, using 4 buttons. Follow the following steps.

  • Click on New Project
  • Select New Windows From Application
  • Enter a name for your application
  • Drag & drop 4 Buttons from the Toolbox onto the Form1
  • Drag & drop one SerialPort also
  • Double click on the form. You will see the coding area.
  • Copy-paste the following code there.

Imports System.IO

Imports System.IO.Ports

Imports System.Threading

Public Class Form1

Shared _continue As Boolean

Shared _serialPort As SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SerialPort1.Close()

SerialPort1.PortName = InputBox("Please enter the COM port")

SerialPort1.BaudRate = InputBox("Please enter the baudrate")

SerialPort1.DataBits = 8

SerialPort1.Parity = Parity.None

SerialPort1.StopBits = StopBits.One

SerialPort1.Handshake = Handshake.None

SerialPort1.Encoding = System.Text.Encoding.Default

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

SerialPort1.Open()

SerialPort1.Write("1")

SerialPort1.Close()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()

SerialPort1.Write("2")

SerialPort1.Close()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()

SerialPort1.Write("3")

SerialPort1.Close()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()

SerialPort1.Write("4")

SerialPort1.Close()

End Sub

End Class

Step 3: Verifying the VB Code and Building the Program

Picture of Verifying the VB Code and Building the Program

Press F5 to start debugging.

If everything is correct, an input box will appear, asking you to enter the COM port. Type your port, for example COM1. Note that you have to type com1, not just 1.

After pressing OK, next input box will ask for baud rate. Enter the baud rate that you are using in the arduino.

Then you will see the 4 buttons. Do not press any buttons, as you will get an error "The port 'com21' does not exist.", as we haven't connected the arduino yet.

Then select Build from Debug menu. Go to the location shown in the Output box to get your program.

Step 4: Arduino Sketch

Upload the following code to your arduino.

int ledPin1 = 2;
int ledPin2 = 3;

int ledPin3 = 4;

int ledPin4 = 5;

int ledPin5 = 6;

void setup(){

Serial.begin(9600);//set serial speed. Enter this value for baud rate

}

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){//for button 1

digitalWrite(ledPin1,HIGH);

delay(500);

digitalWrite(ledPin2,HIGH);

delay(500);

digitalWrite(ledPin3,HIGH);

delay(500);

digitalWrite(ledPin4,HIGH);

delay(500);

digitalWrite(ledPin5,HIGH);

delay(500);

}

else if (val == 2){//for button 2

digitalWrite(ledPin5,LOW);

delay(500);

digitalWrite(ledPin4,LOW);

delay(500);

digitalWrite(ledPin3,LOW);

delay(500);

digitalWrite(ledPin2,LOW);

delay(500);

digitalWrite(ledPin1,LOW);

delay(500);

}

else if (val == 3){//for button 3

digitalWrite(ledPin1,HIGH);

digitalWrite(ledPin2,HIGH);

digitalWrite(ledPin3,HIGH);

digitalWrite(ledPin4,HIGH);

digitalWrite(ledPin5,HIGH);

} else if (val == 4){//for button 4

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

digitalWrite(ledPin4,LOW);

digitalWrite(ledPin5,LOW);

} else{//if not one of above command, do nothing

}

Serial.flush(); // clear serial port

}

Step 5: Testing

Open the VB program that you created.

Enter the serial port and speed.

Click on the buttons to see what happens.

For button1, leds 1-5 sequentially light up afer 0.5 seconds.

For button2, leds 5-1 sequentially turn off after 0.5 seconds.

For button3, all leds light up together.

For button4, all leds turn off together.

Comments

alain19 (author)2016-11-20

thx you for this tuto !! i love !

BlumeH (author)2015-09-21

And I want to make Arduino inside Visual Basic identifier any codes I want to be know
I note tyro

BlumeH (author)2015-09-21

Thanks very much

Do you have a book available to make some basic cool project with arduino + visual basic 2012 or 2010

akruti2 (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

hathem.habeed (author)2015-02-23

Peace and God's mercy and blessings

May Allah is better to adequate explanation

I have an important question: I am working on simulations Protoss program and on the plate Ardinio uno type but I do not understand is how I can connect program Alvjul Basic Balardinio Alardinio because the system uses files from hex type I ask you to give me note that I tyro

If it was possible to communicate with me on Skype redflower788 because I needed explanation verbally

And to you my sincere thanks and appreciation

sunilksree (author)hathem.habeed2015-02-23

I am sorry, but I dont know how to do that.

But you can get better help if you ask it here:

https://www.instructables.com/answers/ask

hathem.habeed (author)2015-02-11

السلام عليكم ورحمه الله وبركاته

جزاكم الله خير على الشرح الوافي

لدي سؤال مهم : انا اعمل على برنامج محاكات البروتوس وعلى لوحه اردينيو نوع uno لكن الذي لا افهمه هو كيف استطيع ربط برنامج الفجول بيسك بالاردينيو لان نظام الاردينيو يستخدم ملفات من نوع hex ارجو منكم افادتي علما اني مبتدا

واذا كان من الممكن التواصل معي على السكايب redflower788 لاني احتاج الشرح كلاميا

ولكم خالص الشكر والتقدير

sunilksree (author)hathem.habeed2015-02-11

Hi,
Can you type in english please?

cibercristie (author)2014-11-19

Hi, I'm new making a GUI to control arduino. I have a project of an robotic hand, were I already have my code. I have a code to make a simple testing of hand movements that runs automatically, the other code is using flexsensors, we have 5 servo motors that moves depending on the flex maded in the flexsensors.

I have to do a GUI that have two buttons, one (testing) this one would make the testing code run. The other (flexsensors) have to make the flexsensor code run. That's my idea, but I don't know how to make that the buttons when pressed run certain code that i would like in the arduino.

Can you help me on this?

Thanks

=)

sunilksree (author)cibercristie2014-11-19

Hi,

Send me your code (and video of your working model if possible), and I will try to make the code for you.

But my exams are going on now, so it will take some time.

cibercristie (author)2014-11-19

Hi, I'm new making a GUI to control arduino. I have a project of an robotic hand, were I already have my code. I have a code to make a simple testing of hand movements that runs automatically, the other code is using flexsensors, we have 5 servo motors that moves depending on the flex maded in the flexsensors.

I have to do a GUI that have two buttons, one (testing) this one would make the testing code run. The other (flexsensors) have to make the flexsensor code run. That's my idea, but I don't know how to make that the buttons when pressed run certain code that i would like in the arduino.

Can you help me on this?

Thanks

=)

Scifiswapnil (author)2014-07-27

how do we read stuff from arduino .??

sunilksree (author)Scifiswapnil2014-07-27

Use SerialPort.ReadLine()

Refer this page for more info.

Scifiswapnil (author)sunilksree2014-07-27

thanx ...
a really very helpful instructable ..!!