Using Visual Basic 2010 to Control Arduino Uno





Introduction: Using Visual Basic 2010 to Control Arduino Uno

I know this has been done in the past a few times so here's one more. This is a skeletal Visual Basic 2010 and Arduino Sketch that I mixed together to test the PC to Arduino Uno connection via serial. It simply turns LED 13 on or off. 

The Visual Basic 2010 code assumes you have Form1 with 2 buttons Button1 and Button2 and SerialPort1 controls. Button1 sends a 1 and Button2 sends a 0 to the serial port COM10 (change this to match your PC to Arduino port setting)

See attached photo of my simple form design.

The Arduino Uno Sketch code simply waits and reads the serial port. If it see 1 it will turn PIN 13 on and if it sees 0 it will turn PIN 13 off. If you have an LED on PIN 13, you can turn it on and off. On the Arduino Uno, PIN 13 is attached to a an on-board LED.

I used COM10 as a serial port but you can (and must) change it to match your Arduino serial port.

The purpose of this code is to simplify explanation of how to connect VB to Arduino. You can add error processing and more intelligence based on your particular needs.  

Make sure you drag the Serial Port control icon from the Toolbox onto your form. It should have the name SerialPort1

WARNING: On my PC I had to close the Arduino IDE Serial Monitor window while runing the VB program, else I run into all sorts of error message about COM port access denied and the program will fail.

You can download Visual Basic Express 2010 for free from Microsoft

'------------ START OF VB 2010 CODE -----------------
' NOTE: I am using COM10 so you need to change the Visual Basic code to match your COM port

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 = "com10" '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

'------------ END OF VB 2010 CODE -----------------

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


  • nice, thanks :)and h...-AnezA

    AnezA made it!


  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Sew Warm Contest 2018

    Sew Warm Contest 2018

We have a be nice policy.
Please be positive and constructive.




Well this is easy because you can add a text box to connect to communication port and set your own com port

Is it possible to communicate between arduino and vb wirelessly?

Late reply, but you could use bluetooth shield and use your PC's built-in bluetooth reciever (or a Bluetooth dongle if it doesn't have one) to connect them wirelessly.

Hi, i happened to face systemnullreference exception error on the serialport1.close in the pub form1 class...
How do i fix this?

thank you for this amazing tutorial but ca you explain more how does the connection between the arduino and the VB work?

This is really cool, but could somebody please tell me how I can run it without visual studio after I compile it?

yeah, the .exe file is in your project folder. There is a folder, that is named after your project and in this folder there is a bin folder this contains a the debug folder and in it is your projects .exe file

i would like to as anyone in here... when i run the program in Visual basic, error message shown "access to the comport 3 is denied" ?? did anyone know how to overcome this problem?


very nice