Connecting Arduino and Visual Basic Express via Serial Communication

At school, one common question I get in the Mechatronics class I help TA  is how to connect the Arduino to Visual Basic Express. While I don’t particularly like using VBE, it is a requirement for this class so I thought I would take the time to put together a quick tutorial on how to have it communicate with the Arduino.

What this tutorial is going to show is how to use Visual Basic Express to blink the onboard LED on Pin13 using a Visual Basic GUI. The Arduino will also send back a string that will be displayed on the User Interface.

final GUI

 

First let’s go over the Arduino code:

We begin the Serial communication on baud rate 9600 and then we wait for some data to be sent. If the data that is sent is a ‘L’ then the LED on pin 13 will turn on and if the data that is sent is a ‘l’ (lower case L), then the LED will turn off. Additionally, the Arduino will send a response back using the println command. NOTE: this is different from using the print command because the println command also sends a carriage return (‘\r’) and newline (‘\n’) character. This is important to know when we program the GUI.

char tmp;

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);

}

void loop() {
if (Serial.available() > 0){
   tmp = Serial.read();

   if (tmp == 'L'){
      digitalWrite(13,HIGH);
      Serial.println("LED is On");
   }
   else if (tmp == 'l'){
       digitalWrite(13, LOW);
       Serial.println("LED is Off");
  }
}
 
}

VISUAL BASIC GUI

So now that the Arduino is setup, we can set up the GUI

First create a  new visual basic windows form application and drag on two buttons, a serialport, and a label. For this tutorial, I will not be renaming these components.

Double click the form and on the form load we are going to open the Serial port. Make sure the Baud Rate matches the baud rate on the arduino and the portname is whatever port your Arduino is connected on.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With SerialPort1
.PortName = “com4”
.DataBits = 8
.BaudRate = 9600
.Handshake = IO.Ports.Handshake.None
.StopBits = IO.Ports.StopBits.One
.Parity = IO.Ports.Parity.None

SerialPort1.Open()

End With

End Sub

 

Next add the code for the two buttons. What we are going to do is send the character that will turn the LED on or off with the different buttons. In Button 1, we send a ‘L’ to turn the LED on and then we check if there is anything in the buffer. If there is something in the buffer, then we can change the label text to what is being sent by the Arduino (LED is on). Notice that again we are using a Readline() command. This command expects a carriage return and newline character to be provided and will hang the program if it does not receive them. This is why is was important to use the println command.

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

SerialPort1.WriteLine(“L”)
If SerialPort1.ReadBufferSize > 0 Then
Label1.Text = SerialPort1.ReadLine()
End If

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

SerialPort1.WriteLine(“l”)
If SerialPort1.ReadBufferSize > 0 Then
Label1.Text = SerialPort1.ReadLine()
End If

End Sub

Finally on the form close, we want to close the Serial port. In order to find the event for the form close go to the properties screen and find the lightning bolt then the load line and double click it.

events

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
SerialPort1.Close()
End Sub

 

2 thoughts on “Connecting Arduino and Visual Basic Express via Serial Communication”

  1. Mister Difilippo,
    It works perect !!
    Just what I neat.

    To get Weather results from Arduino Uno to VB 2010.
    This I must work out.

    Thanks again.
    Fred Multech

Leave a Reply

Your email address will not be published. Required fields are marked *