3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Using Visual Basic 2010 to control Arduino Uno

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 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 -----------------
 
8 comments
May 8, 2012. 9:07 AMcreativen says:
I use visual Basic 2010 but it can not run well, it shows:

Error 1 'SerialPort1' is not declared. It may be inaccessible due to its protection level. C:\Documents and Settings\Sugianto\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 10 9 WindowsApplication1

Anyone knows what happen?
May 8, 2012. 11:35 PMcreativen says:
Finally i got the cause. It is not about the COM things.
I just forgot to drag Serial Port to the design window, now it works well.

Thank you for responding.

Anyway, in your code:

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


why do you start with SerialPort1.Close() ?
May 6, 2012. 5:18 PMcrob09 says:
How do I do this for a stepper?
I have the VB code working thanks for that!
But I would love to have this work for a stepper motor and don't know how to write the sketch.
Any help greatly appreciated,
Rob

Here is what I have:

#include

while (val == 1) { // test for command 1 then turn on stepper
do
int enA = 10; // Enable pin 1 on Motor Control Shield
int enB = 11; // Enable pin 2 on Motor Control Shield
int dirA = 12; // Direction pin dirA on Motor Control Shield
int dirB = 13; // Direction pin dirB on Motor Control Shield
#If
(val == 0) // test for command 0 then turn on next stepper
do
int enA = 14; // Enable pin 1 on Motor Control Shield
int enB = 15; // Enable pin 2 on Motor Control Shield
int dirA = 16; // Direction pin dirA on Motor Control Shield
int dirB = 17; // Direction pin dirB on Motor Control Shield
end whileloop

const int stepsPerRevolution = 20; // Change this to fit the number of steps per revolution
// for your motor

// Initialize the stepper library on pins 12 and 13:
Stepper myStepper(stepsPerRevolution, dirA, dirB);


void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);

// Enable power to the motor
pinMode(enA, OUTPUT);
digitalWrite (enA, HIGH);

pinMode(enB, OUTPUT);
digitalWrite (enB, HIGH);
}

void loop() {
// Step five revolutions into one direction:
myStepper.step(stepsPerRevolution*5);
delay(2000);

// Step five revolutions in the other direction:
myStepper.step(-stepsPerRevolution*5);
delay(2000);
}

Feb 25, 2012. 6:56 PMrirdrifta says:
Thank You i did it and it worked made it wireless too using xbee. I am going to do crazy amazing things with this like controlling an rc car.
Feb 13, 2012. 6:15 PMcmcg182 says:
This is great stuff... very helpful!!!! This code helped me understand the concept, however, there's still something I don't understand. How do you control different pins? For instance, if I wanted to add 2 more buttons (button 3 and 4) and have them turn on and off pin 12, along with the other two buttons still controlling pin 13, how would I do that? Thanks!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
33
Followers
18
Author:techbitar
Did I unplug the solder iron?