Currently, the only programming language that I’m familiar with and is able to create a GUI (Graphical User Interface) Program (aka not the white text on a black background program) is Visual Basic. I’ll be using the Express edition since it’s free. Who doesn’t like free stuff? 🙂
In this demonstration, I’ll be creating a simple program in Visual Basic 2010 that allows the user to turn an LED On or Off on the Arduino. The LED is connected to Digital Pin 13 of the Arduino Uno.
Don’t freak out cause I didn’t use a resistor for the LED. This particular type of LED I bought has very high impedance, I’m not worried that it will burn out.
I wrote a code for the Arduino so that it can receive instructions from the computer.
I enclosed the 1 and 0 with inverted commas because the value that I’ll be sending from the computer will actually be in ASCII.
Here is a screenshot of the program written in Visual Studio 2010 Express.
The serial port baud rate is set to 9600 and the Arduino Uno is using COM4. Visual Basic 2010 comes with the SerialPort function, so it’s pretty simple to program.
Here is a video demonstrating how the program and the Arduino work together.
Thanks for reading 🙂
Thanks po sa code.
that help me a lot
that was very good guide for student like me
thanks po.
Its working Thanks
Tremendous issues here. I am very glad to peer your post. Thank
you so much and I’m taking a look forward to touch you. Will you
please drop me a e-mail?
How can i add more than 1 LED. I Added more “slots” in visual basic but how can i add more in the arduino sketch?
Hi, I checked it, is a very good program to shear
Thank You
there is an updated code , with dynamic port selection refer this page arduino vb.net port selection
Hi Guys!
If i want to manage not the button.click event but the PRESS on the button…how i have to do? My request is:
I want to sent in the serial 1 or 0 while i press the button. If i release it the serial must send nothing. In possible? Why this request? Because with this 2 buttons i wanto to move a stepper motor during the pression on the button.
I believe you are looking for the “mouse_down” event, this fires before click.
Wonderful items from you, man. I’ve take into accout your stuff prior
to and you’re just extremely fantastic. I actually
like what you’ve received here, really like what you are stating and the best
way in which you assert it. You’re making
it entertaining and you continue to take care of to stay it sensible.
I cant wait to read far more from you. That is really a terrific web site.
Hi Waihung can you please help me? I have this laser thing sensor using the LDR that will automatically rang using the buzzer when there is a low light detected. Also I want to control my buzzer using the vb10. Then, i try to run vb code and control it but then my sensor was not working with the vb code. I think my code in vb works fine except in arduino. Here’s my vb code and arduino code below. Can you please help me with this ? What’s wrong in my Arduino code?
//VB.NET Code
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class frmMain
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = “com11”
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Sub
Private Sub btnPON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPON.Click
SerialPort1.Open()
SerialPort1.Write(“1”)
SerialPort1.Close()
End Sub
Private Sub btnPOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPOFF.Click
SerialPort1.Open()
SerialPort1.Write(“0”)
SerialPort1.Close()
End Sub
End Class
// Arduino code
const int laserPin = 4;
const int buzzPin = 13;
int LDR = 0;
void setup()
{
pinMode(laserPin, OUTPUT);
pinMode(buzzPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
int val;
LDR = analogRead(0);
digitalWrite(laserPin, HIGH);
if(LDR 0)
{
val=Serial.read();
if (val==’1′){digitalWrite(buzzPin, HIGH);}
else if (val==’0′){digitalWrite(buzzPin, LOW);}
}
}
}
else
{digitalWrite(buzzPin, LOW);}
}
Opps error spotted in ” if(LDR 0) ” sorry, instead its ” if(LDR < 750) "
I just want to monitor the LED not control it, how should i start to do that?
Im grateful for the blog article.Much thanks again. Cool. baefkedgdggk
Well, actually, it’s a bit more complicated than that.
So whenever any mold growth is spotted, immediate actions should be taken and
the molds should be removed immediately. Keeping humidity levels between 40-60% will
also help prevent excess moisture.
Good day! Would you mind if I share your blog with my facebook group?
There’s a loot of people that I think woyld really enjoy
ylur content. Please let me know.Thank you
obviously like your website however you need to check the spelling on quite a
few of your posts. A number of them are rife with spelling issues and I
to find it very troublesome to inform the truth on the other
hand I will definitely come back again.
Appreciating the dedication you put into your blog and in depth information you provide.
It’s nice to come across a blog every once in a while that isn’t the same
unwanted rehashed material. Excellent read! I’ve saved your site and I’m adding your RSS feeds to my Google account.
Great info. thank you… but no Arduino code in the download.
The following code worked for me without alteration.
Hope it helps others like me!
From: http://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/
//————- START OF ARDUINO SKETCH —————–
//
// Mixed by: Hazim Bitar
// Based on: Science Guy 14 youTube tutorial http://youtu.be/g0pSfyXOXj8
int ledPin = 13; // the number of the LED pin
void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED
}
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) { // test for command 1 then turn on LED
Serial.println(“LED on”);
digitalWrite(ledPin, HIGH); // turn on LED
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println(“LED OFF”);
digitalWrite(ledPin, LOW); // turn off LED
}
else // if not one of above command, do nothing
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}
//————- END OF ARDUINO SKETCH —————–
The arduino code is in the article itself, not in the download.
Look closely.
Link exchange is nothing elsse however it is only placing the other
person’s website link on your page at proper place and other person will also do similar in favor of
you.
Just curious, why is the “SerialPort1.Encoding” line “very important”? If you go to the web site that I was asked to provide, you’ll see what I’ve been doing. It has been working fine, but I put in your line of code just for fun and it still works fine. Just wondering if I should keep that line for some good reason!
Cool information though!!!
Thanks!
Tom
Hi! Thanks for your comment. I believe the encoding line ensures that the text input to the serial port is in standard form. For PCs installed with another encoding (language) such as Chinese, entering a ‘1’ in that computer is not the same as entering ‘1’ in a normal English computer. I hope you get what I’m on about here 🙂
Do you have a VB6 equivalent of the program?I’m stuck with serial communication of arduino using VB6..I think my arduino program is good, but the VB6 is’nt..Please help thanks!
Sorry, I’ve got no experience with VB6. The code will be similar though, maybe make sure you’ve selected the right COM port? It’s in the Form1.Load private sub.
Good afternoon. I have a project that one.
LED 6, as above, the project is to control the dog.
5 However, not all came from only one ganeunghande
Arduino not only the 0-9 i heard bonaeneu
Anything to solve this problem raises questions.
The number of 10 or more, please tell us how to do it.
hi, i just wondering how to control morethan one led on/off using vb.net? I see that arduino uno board has 13 pin out, can every pin out use to control led on/off? Like i want to turn the number 5 and 7, while the other are off or etc ? Kindly advise and thanks for the gr8 information provide here 🙂
Yes, by changing the Arduino code you can turn on/off more than 1 LED.
You can only use 11 out of the 13 digital pins. Digital 0 and 1 cannot be used because it’s connected to the serial RX/TX line.
If that’s not enough you can always use Analog 0 to Analog 5 for LED output.
Hi Wai Hung, thanks for your reply. I just got my board by today. When i plug in into usb port, the power led (GREEN) turn on and the ‘L’ led near the pin 13 is blinking. Is it the correct thing? I,ve installed the driver and recognized as com5 on my computer
But when i try to use downloaded program from this site, and i press on/off, the LED that i put on pin 13 keep blinking. Can’t on/off as i expected. Kindly advise, thank you 🙂
You need to upload the code to the Arduino.
Just follow the code in the screenshot. I lost the soft copy of the arduino code.
I did test using arduino example that include in IDE, it works.
I mean when using the VB.NET code you provide (http://waihung.net/sketch/Arduino_LED.rar) it can not control the Led? Do i miss something?
i want to display propane gas sensor (MQ5) data on VB 2010, but i get problem to arrange for this program. pleace help me..
Have a look at my other project.
That project has the code which will allow you to send data to VB.
http://waihung.net/arduino-fake-screenshot-trigger/
hi Waihung,
i want to display ultrasonic sensor data on VB 2010.this is the arduino code for the Ultrasonic sensor
//define the variable and pins
int trigPin = 8;
int echoPin = 9;
void setup(){
Serial.begin(19200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(){
int Duration, Distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds (1000);
digitalWrite(trigPin, LOW);
Duration = pulseIn(echoPin, HIGH);
Distance = (Duration/2) / 29.1;
Serial.print(Distance);
Serial.println(” cm”);
delay(500);
}
and this is my VB code
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
PortComboBox.Items.AddRange(myPort)
BaudComboBox.Items.Add(9600)
BaudComboBox.Items.Add(19200)
BaudComboBox.Items.Add(38400)
BaudComboBox.Items.Add(57600)
BaudComboBox.Items.Add(115200)
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
End Sub
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudComboBox.Text
SerialPort1.Open()
lblMessage.Text = PortComboBox.Text & ” Connected.”
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
SerialPort1.Close()
lblMessage.Text = SerialPort1.PortName & ” Disconnected.”
DisconnectButton.Enabled = False
ConnectButton.Enabled = True
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim Distance As Byte = SerialPort1.ReadExisting
SensorTextBox.Text = Distance
End Sub
unfortunately..im unable to display the data on VB. can you check my code for both Arduino and VB. is there any error with it??
thanks.
hi adi… can you help me..
what is wrong with your vb code? ty
i need to display the output of ultrasonic sensor to vb.. how can i do that sir? ty
I wonder would it be posible to control several pins? or at least two?
Sorry for the late reply. Yes it’s possible.
Just add an if statement to the serial.read command.
With that you can send different characters to perform different tasks.
The limits are endless 🙂
it’s cool~
But it still not work when I try it in VB
I “change com port to match your Arduino port”
And it work when I test in ARDUINO.(To check if it’s ok)
If there any thing I need to check?
Restart your PC, make sure don’t open the Arduino software at all.
Just open the VB program and it should work.
And make sure its COM1, not COM 1. No spaces.
Sometimes even after closing the Arduino software, it’s still holding on to the COM port, that’s why VB cannot use it.
Do update me. Thanks.
Thanks for your help~
And it’s work after following your commend~
You’re welcome. Glad it worked 🙂
thanks for sharing, nice work.. is there any difference if im going to use that code visual studio 2008?.
You’re welcome.
Try opening the project file in vb 2008. If it doesn’t work try to create the form on your own but copy and paste the code from the .vb file.
It should work because the syntax of vb 2008 and 2010 is pretty much the same.
im a newbie on arduino and vb2010, can u help me translate your vb2010 code to vb6 plss?
I’m sorry, I don’t have any knowledge on VB6. I started learning VB from the 2008 version.
hey wai hung, can you make a project that includes bluetooth, a vb.net program and arduino?
I mean, I saw your arduino+bluetooth project, and I need arduino+bluetooth+vb.net program.
glenn
Hi. The bluetooth project I did was using a software called 232Analyzer downloaded from the internet. I did not make that program. You can use any terminal software to send commands over serial.
You can download the code for this project and look at how I send serial commands. It will be the same for bluetooth, just set the COM port to the port of your bluetooth adapter.
Hope it helps.
Hello,
I want to control a Arduino uno powerd servo motor with VB2010.
Can you help me?
Sorry for the super late reply.
I’ve not tried it before but it will be very similar to this program.
Just download the source code and look at how I sent commands over the serial port.
Hope this helps.
Hi. Glad to update that I’ve made a VB program that will control a servo on an Arduino.
Check out http://waihung.net/arduino-visual-basic-servo-control/
Thanks 😀
I have a problem regarding our project, our topic is more on temperature and humidity sensors. Our problem now is on how to receive data both sensors synchronously from arduino to Visual Basic.
I have tried to supply data on temperature and it shows data in the GUI, my problem now is that I have hard time on what code to use to work both temperature and humidity synchronously.
Hope you could help us in this matter. Thank you.
Basically the structure of the code will be the same except the writing to serial part.
You can refer to the code example here on how to read from serial.
http://msdn.microsoft.com/en-us/library/7ya7y41k.aspx
And just create a simple text field to display the data.
To capture two data, maybe you can write them on the same line but separate them using a comma or something. Later in vb, detect where the comma is and split the data.
thanks for the quick response!, I really hard time to figure it out, hope you could help us. (sorry for poor english)
I will let you show our program on temperature sensor separately from humidity sensor in Arduino here’s the site
http://img827.imageshack.us/img827/9233/arduinod.jpg
the temperature could already read in the GUI from Visual Basic, and now our problem is how to combine those sensors in one code for arduino.
here’s the picture of our GUI in VB
http://img27.imageshack.us/img27/430/guivb.jpg
we use ListBox for values we received from arduino.
and here’s the code for our VB
http://img651.imageshack.us/img651/1618/code2vb.jpg
http://img62.imageshack.us/img62/5173/codevb.jpg
we set it a timer1 so that for every second the values will display in ListBox
when I try to build only the temperature will display, could you help me with this problem?,
Hi. Sorry for the late reply.
I’ve looked through your code but first I need to know how does the data coming from the serial port looks like?
Are you using the SHT15 humidity and temp sensor?
I’m using CM-R humidity sensor
http://img836.imageshack.us/img836/3951/humiditysensor.jpg
i connect my humidity sensor in Digital I/O pin 8
and LM35 for temperature sensor!,
for temp sensor i connect it to Analog A0,
thanks
the output of my humidity sensor is in Frequency and I want it to be in percentage,could you help me about this?, i’m a newbie of programming!,
Hey dude i’m trying to write another code but I just don’t know why ther’s a mistake on SerialPort1
http://img341.imageshack.us/img341/8026/erroroh.jpg
Help please!!
You need to drag the serialport item from your tool box to your form.
Please refer to this screenshot.
http://www.mediafire.com/?dissbybhz79j5mq
🙂
thanks it’s working 😀
You’re welcome 🙂 glad it helped
Can you help me? , what`s wrong with this?
http://img526.imageshack.us/img526/9060/ledd.jpg
After your last line which is digitalwrite 13 low, you need to add a }
Would be possible to get the code? I am doing a similar project, but I am trying to get a solenoid to open.
I didnt save the code for the Arduino side, maybe u can type it out again by referring to the screenshot above?
I only have the VB file.
http://www.mediafire.com/?og97b1r1fa8mkd7
Hope it helps 🙂
That helped a lot, thanks.
You’re welcomed 🙂