VISUAL BASIC and ARDUINO : COM PORT SELECTION & ON OFF BUTTON
by computeraidedautomation in Circuits > Electronics
2985 Views, 8 Favorites, 0 Comments
VISUAL BASIC and ARDUINO : COM PORT SELECTION & ON OFF BUTTON
![2.JPG](/proxy/?url=https://content.instructables.com/FWB/4GVX/JR6ODLBC/FWB4GVXJR6ODLBC.jpg&filename=2.JPG)
Create an application with VB.NET to control the arduino
In this example, we are going to insert 2 buttons to control the LED via arduino
Start Your VB (I Am Using VB Express)
![22.JPG](/proxy/?url=https://content.instructables.com/FD4/ZL6V/JR6ODNDI/FD4ZL6VJR6ODNDI.jpg&filename=22.JPG)
Create a new Project and insert a form then add
1 combo-box
3 buttons
Code for the combobox
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If (ComboBox1.SelectedItem <> "") Then
comPORT = ComboBox1.SelectedItem
End If
End Sub
Code for Button 1 (Connect Button)
Private Sub Button1_Click(ByVal
sender As Object, ByVal e As EventArgs) Handles Button1.Click
If (Button1.Text = "Connect") Then
If (comPORT <> "") Then
SerialPort1.PortName = comPORT
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.ReadTimeout = 10000
Button1.Text = "Dis-connect"
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
Button1.Text = "Connect"
End If
End Sub
ON OFF Buttons
![2.JPG](/proxy/?url=https://content.instructables.com/FWB/4GVX/JR6ODLBC/FWB4GVXJR6ODLBC.jpg&filename=2.JPG)
Code for ON button
Private Sub Button2_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub
Code for OFF Button
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub
Save the project and build it
Setting Up the Arduino
![Arduinopin.png](/proxy/?url=https://content.instructables.com/FMJ/3NOK/JR6ODMVV/FMJ3NOKJR6ODMVV.png&filename=Arduinopin.png)
Run Arduino IDE and load below program in it
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
}
Connect PIN 13 to LED positive and other end ground
You Are Done!
![Arduino Visual Basic - Port Selection](/proxy/?url=https://content.instructables.com/F10/HFIO/JR6ODN0X/F10HFIOJR6ODN0X.jpg&filename=Arduino Visual Basic - Port Selection)
Run the VB project from either Debug/Release directory
Click Button 1 to turn LED ON and Button 2 to turn LED OFF
Download complete project from http://www.computeraidedautomation.com/