Arduino Projects Online

Complete Details about the Arduino

Arduino Visual Basic Integration

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.

Arduino Visual Basic Integration

Arduino Visual Basic Integration Program GUI

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

  1. void setup() {
  2. pinMode (13,OUTPUT);
  3. Serial.begin(9600);
  4. }
  6. void loop() {
  7. int val;
  8. if(Serial.available()){
  9. delay(100);
  10. while(Serial.available() >0){
  12. if(val==‘1’){digitalWrite(13,HIGH);}
  13. else if (val==‘0’) {digitalWrite (13,LOW);
  14. }
  15. }
  16. }
  17. }

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.

  1. Imports System.IO
  2. Imports System.IO.Ports
  3. Imports System.Threading
  5. Public Class Form1
  7. Shared _continue As Boolean
  8. Shared _serialPort As SerialPort
  10. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load
  11. SerialPort1.Close()
  12. SerialPort1.PortName = “com34” ‘change com port to match your Arduino port
  13. SerialPort1.BaudRate = 9600
  14. SerialPort1.DataBits = 8
  15. SerialPort1.Parity = Parity.None
  16. SerialPort1.StopBits = StopBits.One
  17. SerialPort1.Handshake = Handshake.None
  18. SerialPort1.Encoding = System.Text.Encoding.Default
  19. End Sub
  21. Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
  22. picLed.Visible = True
  23. SerialPort1.Open()
  24. SerialPort1.Write(“1”)
  25. SerialPort1.Close()
  26. End Sub
  28. Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
  29. picLed.Visible = False
  30. SerialPort1.Open()
  31. SerialPort1.Write(“0”)
  32. SerialPort1.Close()
  33. End Sub
  34. 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!

Also Check:

Updated: 14th July 2017 — 10:45 am
Arduino Projects Online © 2017 Frontier Theme