Arduino and Visual Basic – ON OFF buttons

In this example, we will be using Visual Basic to create a user interface to control electrical equipments through Arduino

Open your VB, and add form and 2 buttons then name it “ON” and “OFF”

arduino vb








Drag “Serial Port” control from your Tool Box  

Then add following codes to your form1.vb (assume the blank project)

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.PortName = “com6” ‘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 ‘very important!
    End Sub

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

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

now connect your arduino to your computer, and check the port (the port, arduino board is connected to) and change above code

now change above VB code’s line SerialPort1.PortName = “com6” ‘change com port to match your Arduino port to match with your port number

Then copy below code and paste it in your arduino code editor

int ledPin = 13; // the number of the LED pin 
void setup() {
pinMode(ledPin, OUTPUT); 
digitalWrite(ledPin, LOW); //turn off LED
void loop(){
while (Serial.available() == 0); 
int val = – ‘0’; 
if (val == 1) { 
Serial.println(“LED on”);
digitalWrite(ledPin, HIGH); // turn on LED
else if (val == 0) 
Serial.println(“LED OFF”);
digitalWrite(ledPin, LOW); // turn off LED

Serial.flush(); // clear serial port

and connect your Led or Relay Input to Pin Number 13 and ground the other end.

Once you successfully compile and write these codes into your arduino, run the VB Form

If Every thing goes right, you should see 2 Buttons ON and OFF, then press ON and the LED on the arduino board is on