Arduino Visual Basic Integration

Usually, I use the Serial Monitor of the Arduino IDE to communicate with the Arduino hardware. But now, I want to build an application on my computer that allows me to click some buttons to control the Arduino. To do this, I am using Visual Basic — the programming language that I’m familiar with and that can create a Graphical User Interface (GUI) program. I am using the VB2008 Express edition of Visual Basic because it’s free and already installed on my computer. In this tutorial, I will create an elementary program in Visual Basic that allows the user to turn the on-board LED (connected to D13 of the Arduino microcontroller) on or off with the help of two buttons.


Hardware Setup

Requisite hardware setup is very simple: just verify and upload the below sketch to Arduino Uno and that’s all! The sketch enables Arduino to receive instructions (which will actually be in ASCII) from the connected computer. The serial port baud rate is set to 9600, and the Arduino Uno uses COM34. Visual Basic 2008 comes with the SerialPort function, so it’s pretty easy to program. All that we need the Arduino to do is read the instructions from the computer and control its on-board indicator in tune with it. The following basic code is sufficient for that purpose.

Arduino Sketch

  void setup() {   pinMode (13,OUTPUT);   Serial.begin(9600);  }    void loop() {    int val;    if(Serial.available()){      delay(100);      while(Serial.available() >0){;        if(val=='1'){digitalWrite(13,HIGH);}        else if (val=='0') {digitalWrite (13,LOW);        }      }    }  }    

Visual Basic (VB) Program

First, create a Visual Basic Windows Forms project and add a picture box and two buttons to the form. The buttons are called “btnOn” and “btnOff.” The picture box should be named “picLed” (remember to create and save a suitable picture of an LED for later use). Next, add the required imports, pictures, event handlers, etc. as usual with VB programming. Here is the VB code for your reference.

  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 = "com34" 'change com port to match your Arduino port          SerialPort1.BaudRate = 9600          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 btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click          picLed.Visible = True          SerialPort1.Open()          SerialPort1.Write("1")          SerialPort1.Close()      End Sub        Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click          picLed.Visible = False          SerialPort1.Open()          SerialPort1.Write("0")          SerialPort1.Close()      End Sub  End Class  

Note that you will need to change the COM port (SerialPort1.PortName= “”) so that it matches the one on which your Arduino connects. Assuming that the Arduino is running and connected to the computer, the program should now control the LED as learned!