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

Print Friendly
  1. bandar dominoqq04-30-2020

    ำ€t’s an awesome paragraph ั–n support ฮฟf ะฐll tาปe online viewers; tาปey ิill ฿‹btain benefit
    from ั–t I am ั•ure.

  2. dominic10-16-2018

    Thanks po sa code.
    that help me a lot
    that was very good guide for student like me
    thanks po.

  3. Sivamani j04-16-2018

    Its working Thanks

  4. service11-27-2017

    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?

  5. Sandro04-18-2017

    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?

  6. Oscar M.09-20-2016

    Hi, I checked it, is a very good program to shear
    Thank You

  7. nichoma07-09-2016

    there is an updated code , with dynamic port selection refer this page arduino port selection

  8. Carlo04-10-2015

    Hi Guys!
    If i want to manage not the 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.

    • Leland Lewis05-16-2015

      I believe you are looking for the “mouse_down” event, this fires before click.

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

  10. ivie12-28-2014

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

    Private Sub btnPON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPON.Click
    End Sub

    Private Sub btnPOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPOFF.Click
    End Sub
    End Class

    // Arduino code
    const int laserPin = 4;
    const int buzzPin = 13;
    int LDR = 0;

    void setup()
    pinMode(laserPin, OUTPUT);

    void loop()
    int val;
    LDR = analogRead(0);

    digitalWrite(laserPin, HIGH);

    if(LDR 0)
    if (val==’1′){digitalWrite(buzzPin, HIGH);}
    else if (val==’0′){digitalWrite(buzzPin, LOW);}
    {digitalWrite(buzzPin, LOW);}


    • ivie12-28-2014

      Opps error spotted in ” if(LDR 0) ” sorry, instead its ” if(LDR < 750) "

  11. john11-18-2014

    I just want to monitor the LED not control it, how should i start to do that?

  12. Johnb7109-05-2014

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

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

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

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

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

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


    //————- START OF ARDUINO SKETCH —————–
    // Mixed by: Hazim Bitar
    // Based on: Science Guy 14 youTube tutorial

    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 = – ‘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.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.

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

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



    • 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 ๐Ÿ™‚

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

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

  22. khiang09-24-2013

    hi, i just wondering how to control morethan one led on/off using 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.

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

  24. 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(){
    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.println(” cm”);

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

    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
    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 unable to display the data on VB. can you check my code for both Arduino and VB. is there any error with it??


    • Angel02-17-2016

      hi adi… can you help me..
      what is wrong with your vb code? ty

      • Angel02-17-2016

        i need to display the output of ultrasonic sensor to vb.. how can i do that sir? ty

  25. 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 command.
      With that you can send different characters to perform different tasks.
      The limits are endless ๐Ÿ™‚

  26. 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 ๐Ÿ™‚

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

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

  29. glenn posadas01-24-2013

    hey wai hung, can you make a project that includes bluetooth, a program and arduino?
    I mean, I saw your arduino+bluetooth project, and I need program.


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

  30. Henk12-26-2012


    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

      Thanks ๐Ÿ˜€

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

      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
        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
        we use ListBox for values we received from arduino.

        and here’s the code for our VB

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


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

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

    Help please!!

  33. Beto Cardenas07-02-2012

    thanks it’s working ๐Ÿ˜€

    • Wai Hung07-02-2012

      You’re welcome ๐Ÿ™‚ glad it helped

  34. Beto Cardenas07-02-2012

    Can you help me? , what`s wrong with this?

    • Wai Hung07-02-2012

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

  35. 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 to Adi Click here to cancel reply.