Using Visual Basic 2010 to Control Arduino Uno

160,173

106

60

Published

Introduction: Using Visual Basic 2010 to Control Arduino Uno

About: Did I unplug the solder iron?

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
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express


'------------ 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.Close()
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
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub
End Class


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


//------------- START OF ARDUINO SKETCH -----------------
//
// Mixed by: Hazim Bitar
// Based on: Science Guy 14 youTube tutorial http://youtu.be/g0pSfyXOXj8

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 = Serial.read() - '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.println(val);
Serial.flush(); // clear serial port
}

//------------- END OF ARDUINO SKETCH -----------------
 

Share

Recommendations

  • Clocks Contest

    Clocks Contest
  • Oil Contest

    Oil Contest
  • Creative Misuse Contest

    Creative Misuse Contest

60 Discussions

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?

1 reply

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.

0
user
TanL22

1 year ago

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?

2 replies

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

0
user
AfiqI1

2 years ago

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?

errorrrrrrrrr.PNG
0
user
adals1

2 years ago

very nice

Thank you for the info ,,, do you consult ? digivalve@gmail.com

Thanks for the tutorial mate.
Q Have create a vb.net UI for my arduino project. i needed to change some arduino variables through the interface . how can i do this?
IE int lite=13;
int line=6;
int lex=8;
void setup() {
pinMode(lite, OUTPUT);
pinMode(line, OUTPUT);
pinMode(lex, OUTPUT); }
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

so change int lite=13 to say 7 ,int line to 9 and lex to 11 through the UI... Help please thanks in adv

thanx brother for giving example in simple... helped me to understand functions and methodology...

i have code on vb.net that will turn on led on my arduino but how can i turn it on and off if i want to add button in my arduino?
how can i add button here in my code?



#include <LiquidCrystal.h>
int ledPin = 13;
int buttonPin = 2;
// the number of the pushbutton pin
// the number of the LED pin
int buttonState = 0;

// variables will change:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED

//

//
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(" DaNeiL");
lcd.setCursor(0,1);
lcd.print("GASOLINE-STATION");
}


void loop(){


while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
int buttonstate = Serial.read();


buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);
Serial.println("PLEASE WAIT WHILE REFUELING");
Serial.print('\n');
delay(9000);
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
Serial.println("price 40");
Serial.print('\n');
Serial.print('\n');
Serial.println("liters 1");
}
else
// turn LED off:


if (val == 1) { // test for command 1 then turn on LED
Serial.println("P20.00 1/2");
delay(10);
digitalWrite(ledPin, HIGH);// turn on LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRICE= P20.00");
lcd.setCursor(0,1);
lcd.print("MILLILITERS=500");


}




{
Serial.println("P40.00 1");
delay(10);
digitalWrite(ledPin, HIGH); // turn off LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRICE= P40.00");
lcd.setCursor(0,1);
lcd.print("MILLILITERS=1000");

}
if (val == 3) { // test for command 1 then turn on LED
Serial.println("P80.00 2");
delay(1000);
digitalWrite(ledPin, HIGH);// turn on LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRICE= P80.00");
lcd.setCursor(0,1);
lcd.print("MILLILITERS=2000");

}
else if (val == 4) // test for command 0 then turn off LED
{
Serial.println("P120.00 3");
delay(1000);
digitalWrite(ledPin, HIGH); // turn off LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRICE= P120.00");
lcd.setCursor(0,1);
lcd.print("MILLILITERS=3000");

}

if (val == 5) { // test for command 1 then turn on LED
Serial.println("P160.00 4 ");
delay(10);
digitalWrite(ledPin, HIGH);// turn on LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRICE= P160.00");
lcd.setCursor(0,1);
lcd.print("MILLILITERS=4000");

}
else if (val == 6) // test for command 0 then turn off LED
{
Serial.println("P200.00 5");
delay(10);
digitalWrite(ledPin, HIGH); // turn off LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRICE= P200.00");
lcd.setCursor(0,1);
lcd.print("MILLILITERS=5000");

}
if (val == 8) { // test for command 1 then turn on LED
delay(10);
digitalWrite(ledPin, LOW);// turn on LED
lcd.clear();
lcd.print(" DaNeiL");
lcd.setCursor(0,1);
lcd.print("GASOLINE-STATION");

}

if (val == 9) { // test for command 1 then turn on LED
delay(10);
digitalWrite(ledPin, HIGH);// turn on LED
lcd.clear();
lcd.print(" FULL TANK");
lcd.setCursor(0,1);
lcd.print("PLEASE WAIT....");

}




if (val == 0) { // test for command 1 then turn on LED
delay(10);
digitalWrite(ledPin, LOW);// turn on LED



}
else if (val == 12) // test for command 0 then turn off LED
{
delay(10);
digitalWrite(ledPin, HIGH); // turn off LED





}
else {

}

}

can some one please explain this code for me i really feel stupid :(



int incomingByte = 0;
int r = 9;
int g = 10;
int b = 11;
int datar = 0;
int datag = 0;
int datab = 0;
int part = 0;
int numbertrans = 0;

void setup() {
Serial.begin(9600);
}

void loop() {


if (Serial.available() > 0) {

incomingByte = Serial.read();
if (incomingByte == 48) { numbertrans = 1; }
if (incomingByte == 49) { numbertrans = 25; }
if (incomingByte == 50) { numbertrans = 51; }
if (incomingByte == 51) { numbertrans = 76; }
if (incomingByte == 52) { numbertrans = 102; }
if (incomingByte == 53) { numbertrans = 127; }
if (incomingByte == 54) { numbertrans = 153; }
if (incomingByte == 55) { numbertrans = 178; }
if (incomingByte == 56) { numbertrans = 204; }
if (incomingByte == 57) { numbertrans = 255; }
if (incomingByte == 44) { part++; }
if (part == 0) {datar = numbertrans;}
if (part == 1) {datag = numbertrans;}
if (part == 2) {datab = numbertrans;}
if (part == 3) {
analogWrite(9, datar);
analogWrite(10, datag);
analogWrite(11, datab);
part = 0;}
// Below is evil dubug stuff
Serial.println(incomingByte, DEC);
}
}

1 reply

Error1'SerialPort1' is not declared. It may be inaccessible due to its protection level.C:\Users\Felix\AppData\Local\Temporary Projects\Arduino1\Form1.vb109Arduino1

how should i declare it?

1 reply