Arduino Controlled by Visual Basic Program

Arduino Vb Banner
Before this I’ve always used the Serial Monitor of the Arduino IDE to communicate with the Arduino. Actually what the Serial Monitor does is basically reading or sending data through the Serial interface. In my future project, I want to create an application in the computer that allows the user to click some buttons to control the Arduino.

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 :)

Download Code

  1. twitter.com09-19-2014

    Gable Fronted: They are also square shaped and have huge roof windows.
    Another advantage of purchasing these locks is that you should not have a lot
    of difficulty getting the amount you need, whereas this may not be
    the case in a salvage shop. If you have old timber framed windows and
    doors you should consider replacing them with u – PVC windows and u – PVC doors.

  2. Johnb7109-05-2014

    Im grateful for the blog article.Much thanks again. Cool. baefkedgdggk

  3. Smithd4709-04-2014

    Exclusively at Target 42 Bluray Combo Pack with exclusive dafdadaedegdkdbg

  4. 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.

  5. Charissa04-05-2014

    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

  6. 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.

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

  8. Andy02-22-2014

    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 —————–

    • waihung03-16-2014

      The arduino code is in the article itself, not in the download.
      Look closely.

  9. office clearance02-19-2014

    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.

  10. Tom12-09-2013

    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

    • waihung12-11-2013

      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 :)

  11. ohyeah11-25-2013

    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!

    • waihung11-29-2013

      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.

  12. JunYeong10-23-2013

    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.

  13. khiang09-24-2013

    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 :)

    • Wai Hung09-24-2013

      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.

      • khiang09-28-2013

        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 :)

        • Wai Hung09-28-2013

          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.

  14. andri09-18-2013

    i want to display propane gas sensor (MQ5) data on VB 2010, but i get problem to arrange for this program. pleace help me..

  15. Adi07-02-2013

    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.

  16. Jan Peersen05-22-2013

    I wonder would it be posible to control several pins? or at least two?

    • waihung06-20-2013

      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 :)

  17. ozzykaka02-17-2013

    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?

    • Wai Hung02-17-2013

      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.

      • ozzykaka02-17-2013

        Thanks for your help~

        And it’s work after following your commend~

        • Wai Hung02-17-2013

          You’re welcome. Glad it worked :)

  18. keith02-14-2013

    thanks for sharing, nice work.. is there any difference if im going to use that code visual studio 2008?.

    • Wai Hung02-14-2013

      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.

  19. nexus01-28-2013

    im a newbie on arduino and vb2010, can u help me translate your vb2010 code to vb6 plss?

    • Wai Hung02-13-2013

      I’m sorry, I don’t have any knowledge on VB6. I started learning VB from the 2008 version.

  20. glenn posadas01-24-2013

    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

    • waihung01-26-2013

      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.

  21. Henk12-26-2012

    Hello,

    I want to control a Arduino uno powerd servo motor with VB2010.
    Can you help me?

    • waihung01-26-2013

      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.

    • Wai Hung02-13-2013

      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 :D

  22. Ruzel11-13-2012

    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.

    • Wai Hung11-13-2012

      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.

      • Ruzel11-14-2012

        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?,

        • waihung11-17-2012

          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?

          • Ruzel11-17-2012

            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

            • Ruzel11-17-2012

              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!,

  23. Beto Cardenas07-03-2012

    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!!

  24. Beto Cardenas07-02-2012

    thanks it’s working :D

    • Wai Hung07-02-2012

      You’re welcome :) glad it helped

  25. Beto Cardenas07-02-2012

    Can you help me? , what`s wrong with this?
    http://img526.imageshack.us/img526/9060/ledd.jpg

    • Wai Hung07-02-2012

      After your last line which is digitalwrite 13 low, you need to add a }

  26. Laerte06-04-2012

    Would be possible to get the code? I am doing a similar project, but I am trying to get a solenoid to open.

Leave a Reply