top of page

Arduino Controlled LED using Visual Basic

Updated: Nov 18, 2020

Creating a simple program in Visual Basic that allows the user to turn an LED On or Off on the Arduino. The LED is connected to Digital Pin 13 of the Arduino Uno. Visual Basic Program to create an application in the computer that allows the user to click some buttons to control the Arduino.


Before this I’ve always used the Serial Monitor of the Arduino IDE to communicate with the Arduino. Actually what the Serial Monitor does is basically reading or sending data through the Serial interface.

Currently, the only programming language that I’m familiar with and is able to create a GUI (Graphical User Interface) Program is Visual Basic.

I’ll be creating a simple program in Visual Basic 2010 that allows the user to turn an LED On or Off on the Arduino. The LED is connected to Digital Pin 13 of the Arduino Uno.

arduino Code:


void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

Serial.begin(9600);

}


// the loop function runs over and over again forever

void loop() {

int val;

if (Serial.available()){

delay(100);

while (Serial.available() > 0) {

val=Serial.read();


if(val=='1') { digitalWrite(13,HIGH); }

else if (val=='0') {digitalWrite(13,LOW);

}

}

}

}


 

The above code for the Arduino so that it can receive instructions from the computer.


Here enclosed the 1 and 0 with inverted commas because the value that I’ll be sending from the computer will actually be in ASCII.

Here is a screenshot of the program written in Visual Studio 2010 Express.

Visual Basic Code:

Design form:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Partial Class Form1

Inherits System.Windows.Forms.Form


'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

Try

If disposing AndAlso components IsNot Nothing Then

components.Dispose()

End If

Finally

MyBase.Dispose(disposing)

End Try

End Sub


'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer


'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container()

Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))

Me.SerialPort1 = New System.IO.Ports.SerialPort(Me.components)

Me.btnOn = New System.Windows.Forms.Button()

Me.btnOff = New System.Windows.Forms.Button()

Me.picOff = New System.Windows.Forms.PictureBox()

Me.Label1 = New System.Windows.Forms.Label()

Me.picOn = New System.Windows.Forms.PictureBox()

CType(Me.picOff, System.ComponentModel.ISupportInitialize).BeginInit()

CType(Me.picOn, System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'btnOn

'

Me.btnOn.Location = New System.Drawing.Point(41, 178)

Me.btnOn.Name = "btnOn"

Me.btnOn.Size = New System.Drawing.Size(56, 32)

Me.btnOn.TabIndex = 0

Me.btnOn.Text = "ON"

Me.btnOn.UseVisualStyleBackColor = True

'

'btnOff

'

Me.btnOff.Location = New System.Drawing.Point(123, 178)

Me.btnOff.Name = "btnOff"

Me.btnOff.Size = New System.Drawing.Size(56, 32)

Me.btnOff.TabIndex = 1

Me.btnOff.Text = "OFF"

Me.btnOff.UseVisualStyleBackColor = True

'

'picOff

'

Me.picOff.Image = CType(resources.GetObject("picOff.Image"), System.Drawing.Image)

Me.picOff.Location = New System.Drawing.Point(86, 67)

Me.picOff.Name = "picOff"

Me.picOff.Size = New System.Drawing.Size(46, 88)

Me.picOff.TabIndex = 2

Me.picOff.TabStop = False

'

'Label1

'

Me.Label1.AutoSize = True

Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label1.Location = New System.Drawing.Point(43, 28)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(133, 25)

Me.Label1.TabIndex = 3

Me.Label1.Text = "PC Sim LED"

'

'picOn

'

Me.picOn.BackColor = System.Drawing.Color.Transparent

Me.picOn.Image = CType(resources.GetObject("picOn.Image"), System.Drawing.Image)

Me.picOn.Location = New System.Drawing.Point(86, 67)

Me.picOn.Name = "picOn"

Me.picOn.Size = New System.Drawing.Size(46, 88)

Me.picOn.TabIndex = 4

Me.picOn.TabStop = False

Me.picOn.Visible = False

'

'Form1

'

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.BackColor = System.Drawing.Color.White

Me.ClientSize = New System.Drawing.Size(217, 236)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.btnOff)

Me.Controls.Add(Me.btnOn)

Me.Controls.Add(Me.picOn)

Me.Controls.Add(Me.picOff)

Me.Name = "Form1"

Me.Text = "PC Sim LED"

CType(Me.picOff, System.ComponentModel.ISupportInitialize).EndInit()

CType(Me.picOn, System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

Me.PerformLayout()


End Sub

Friend WithEvents SerialPort1 As System.IO.Ports.SerialPort

Friend WithEvents btnOn As System.Windows.Forms.Button

Friend WithEvents btnOff As System.Windows.Forms.Button

Friend WithEvents picOff As System.Windows.Forms.PictureBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents picOn As System.Windows.Forms.PictureBo

 


Visual basic code for Form1:

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 = "com3" '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 btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click

picOn.Visible = True

SerialPort1.Open()

SerialPort1.Write("1")

SerialPort1.Close()

End Sub


Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click

picOn.Visible = False

SerialPort1.Open()

SerialPort1.Write("0")

SerialPort1.Close()

End Sub

End Class



End Class

The serial port baud rate is set to 9600 and the Arduino Uno is using COM3. Visual Basic 2010 comes with the SerialPort function, so it’s pretty simple to program.

Demo:


Download code:


465 views0 comments

Comments


bottom of page