Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Note: Pin 13 is mostly preferred for testing purpose because most Arduinos have a built-in LED
attached to pin 13 on the board, the LED is optional.
You can see the readings from Serial Monitor. It can be accessed by pressing the
shortcut ‘Ctrl+Shift+M’or you can access it from Tools > serial monitor as shown below.
The programme code is given below :
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print(sensorValue); // print ADC value of analog reading
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:
delay(sensorValue);
}
You can vary the analog reading by adjusting the potentiometer. This tutorial only helps you to build a
basic knowledge about ADC usage in ARDUINO. In spite of this, there are many applications for ADC
pins.
C# visual studio file
Martyn Currey
Mostly Arduino stuff
Main menu
Skip to primary content
Skip to secondary content
Home
Arduino Bluetooth Control
Bluetooth Control Panel
About
X
by BWPlayer
Post navigation
← PreviousNext →
Visual Studio Express is a striped down version of the larger packages and has some major
limitations. Visual Studio 2013 Community, on the other hand, is a full featured IDE and
development system free to use for students, open source contributors and small development teams.
It includes several languages but for now I am only interested in Visual Basic.
void setup()
{
pinMode(LEDpin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println("1234");
digitalWrite(LEDpin,HIGH);
delay(100);
digitalWrite(LEDpin,LOW);
delay(900);
}
To test that the sketch is working you can open the serial monitor:
Imports System
Imports System.IO.Ports
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
Timer1.Enabled = True
Timer_LBL.Text = "Timer: ON"
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
connect_BTN.Text = "Connect"
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
End If
End Sub
End Function
End Class
When the program is first run, the Form1_Load() subroutine populates the COM port combo box /
drop down list with the available COM ports. The program then waits for the user to pick one.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Timer1.Enabled = False
comPORT = ""
For Each sp As String In My.Computer.Ports.SerialPortNames
comPort_ComboBox.Items.Add(sp)
Next
End Sub
When the user selects a COM port, the value is copied to the variable comPORT. This is not really
necessary as the selected value can be read from the combo box but I like to keep this kind of data in
easy to use variables.
Private Sub comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Ha
comPort_ComboBox.SelectedIndexChanged
If (comPort_ComboBox.SelectedItem <> "") Then
comPORT = comPort_ComboBox.SelectedItem
End If
End Sub
Sub connect_BTN_Click() triggers when the user clicks on the Connect button. The first thing the
routine does is determine if the user is connecting or dis-connecting. The same button is used for
both.
If connecting, and comPORT is not empty, then the serial port properties are set, the serial port is
opened and the timer is started. To show that the timer is active the timer label is updated to “Timer:
ON”.
If comPORT is empty a message is displayed telling the user to select a COM port first.
If dis-connecting, the serial port is closed, the timer is stopped and the timer label is updated to
“Timer: OFF”.
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
Timer1.Enabled = True
Timer_LBL.Text = "Timer: ON"
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
connect_BTN.Text = "Connect"
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
End If
End Sub
A timer is used to check for incoming data. The timer is set to trigger every 500ms or half a second
and when triggered it calls the Timer1_Tick() routine. For this example 500ms is fast enough. For
more complex tasks the timing may need to be adjusted.
Timer1_Tick() calls a second subroutine that checks to see if there is any serial data and if there is it
then copies the incoming data to the receivedData variable.
receivedData is then added to the textbox.
Function ReceiveSerialData() As String
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
If Incoming Is Nothing Then
Return "nothing" & vbCrLf
Else
Return Incoming
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
End Function
The final subroutine, Sub clear_BTN_Click(), simply resets the contents of the text box.
Private Sub clear_BTN_Click(sender As Object, e As EventArgs) Handles
clear_BTN.Click
RichTextBox1.Text = ""
End Sub
Trouble Shooting
If you are not receiving data in the VB program but the Arduinos serial monitor works then on the
serial port within VB, set “DtrEnable = true” and “RtsEnable = true”. Thank you Banause for the tip.
This seems to be required for the Arduino Leonardo.
This is a very simple example that I used to learn the basics on making a serial connection between
the Arduino and a computer. As such the code can be made much better.
– The COM port is left open all the time and for more complex applications it may be better to open
and close the port as required.
– The application simply displays what ever data is received. There is no error checking. To make it
more reliable and to ensure you have all the data it would be better if the data was enclosed in start
and end tags and then parsed.
For more information on serial communication with the Arduino and using start and end makers take
a look at Robin2’s Serial Input Basics on the Arduino forum.
Download
Download the Visual Basic project files and the Arduino sketch
ArduinoVisualBasicSerial.zip 87.82 KB
Download
You will need to have Visual Studio installed to use the VB project files.
Next Steps
In part 2 we take this further. And in part 3 we start to control an Arduino
This entry was posted in Arduino + Visual Basic and tagged serial by Martyn. Bookmark the permalink.
61 THOUGHTS ON “ARDUINO AND VISUAL BASIC PART 1: RECEIVING DATA FROM THE ARDUINO”
Great help. :D
I’m a newbie at this and I keep receiving this one error in VB..
Error 1 ‘comPort_ComboBox’ is not declared. It may be inaccessible due to its protection level.
I copied the entire code exactly as it is and I’m using visual basic express 2010.
Any idea on what I must be doing wrong?
Reply ↓
Try deleting the combo box and recreating it. Remember to give the new combo box the same name
“comPort_ComboBox”
Reply ↓
Thanks
Reply ↓
I’m too face this issue , I use VB 2013 and Arduino Leonardo board ,when I replace the board with
Mega ADK …there is no problem…
It works very good ..
Reply ↓
If the Arduino serial monitor is working then there is no reason the VB program should not work.
There are a few things worth remembering:
– You cannot have two connections to the same Arduino. If the serial monitor is open then VB
cannot use the COM port.
– Double check the baud rates.
– If you have changed any of the serial properties (data bits, parity, encoding, etc) change them back
to the defaults.
Reply ↓
I know my english inst that good, but i hope u can understand what i wanted to say :D
Reply ↓
Reply ↓
If the serial monitor is working then you should be able to connect through VB. Use the downloaded
programs rather than copy paste from the website. Does the VB program compile OK?
Only one connection to a COM port can be made at a time. If you open the serial monitor then you
block VB and vice a versa.
You can also try using a different serial terminal such as putty.
Reply ↓
6. Pingback: Arduino and Visual Basic Part 2: Receiving Data From the Arduino | Martyn
Currey
helo
I’m new to VB and I’m trying developing a VBA application to send and receive data to an RFID
reader and put the result into Microsoft Access.
pless help me?
Reply ↓
If not already a member, join the Arduino forum at http://forum.arduino.cc/ you will get help there.
Also, search for Arduino and RFID. There are many guides online.
Reply ↓
hello sir. may i know how to read multiple sensors reading and show in different textbox for each
reading? please sir
thanks
Reply ↓
Reply ↓
9. JR on April 24, 2016 at 7:18 pm said:
i dont understand this line ” If (comPORT “”) Then ” form code what does it mean?
Reply ↓
Reply ↓
I tried this code to get really fast into listening to the arduino, while submitting the values from
AnalogIn (A0). But reading the whole values is sometimes to much data. Remember, you read the
whole buffer once without disposing the buffer.
from
[code]
Incoming = SerialPort1.ReadExisting()
[/code]
to
[code]
Incoming = SerialPort1.ReadLine()
SerialPort1.DiscardInBuffer()
[/code]
Now it runs pretty good and replies actual data.
(Hope this blog reads the code tags right. If not, just ignore the tags.)
Reply ↓
Reply ↓
Reply ↓
Reply ↓
13. Chuck Hayes on July 14, 2016 at 6:40 pm said:
I am trying to receive a signal if a limit switch is enabled. All I need is to know how to make a
Boolean true in VB.NET. If the switch is closed send a signal either through USB or serial to the
computer. I am pretty good at VB. I am not really sure what Adruino board to buy or how to talk to
the board. Any suggestions would be greatly appreciated.
Thanks,
Chuck
Reply ↓
Start by looking at the main boards; Uno, Mega, Nano. The Uno and Nano are very similar (mainly a
different size). The Mega adds extra hardware (additional hardware serial ports) and functions but is
bigger and more expensive.
When making permanent projects I tend to use Nanos. They are small and easy to work with.
Reply ↓
very nice tutorial with detail explanation, Thank you and keep update..!
Reply ↓
hello my english is not very well…..thx for this post it helps me to understand more visual basic
because im a beginner. i have a projekt with an arduino uno and an xbee shield with an xbee module
on the pc side i have also an xbee now i woul like to receive the data on the pc i get the commands
from the print function from the arduino but the problem is that they are only for a short time
displayed on the textbox can you help me please that the data is stored in the textbox
Reply ↓
It may be worth while having a look at part 2 and part 3. These show how to use different commands.
Reply ↓
17. juju on January 13, 2017 at 10:18 am said:
Hello Martyn,
First of all, thank you very much for your tutorial which is one of the few and complete on the
internet. You are also one of the few people who answer questions. I can not integrate a Bluetooth
connection with your example that will allow me to have a wireless connection to manage a relay
card. Could you advise me to connect a relay with bluetooth and vb.net?
Thank you very much for your help and continue to make tuto that is great
Reply ↓
To control a set of relays you can adapt the example in part 3. Simply use the same number of
buttons as you have relays.
Reply ↓
Thank you! You helped me so much. I’ve understand it and have modified it. So then.
Reply ↓
19. Mustafa Abdalwahid noori on February 7, 2017 at 7:58 am said:
i try to be use the rfid tag as password .. but i get not found when i write same code it worked !
If (TextBox1.Text = ” 212 79 21 219″) Then
Form3.Show()
Else
MsgBox(“not found”)
End If
Reply ↓
so oddly I had one board (pro micro) that didn’t read, and another (uno) that did, so no idea why your
hint with setting the serialport.rtsenable and serialport.dtrenable to true worked,, but i had been
struggling with that for a long time. Thanks for that tip.
Reply ↓
Hello,
When i run my program I get error : AAccess denied to the port ‘COM3′. What can iI do?
Reply ↓
Reply ↓
Hi Martyn,
Great site and very good explanations. Thank you for sharing.
Please continue your great work.
/Henrik
Reply ↓
Hey Guys!
For example
Reply ↓
Thank you for your article, which is exactly what i am looking for!
Question: is there any way to avoiding COM select? if the Form only work with Arduino, Can i make
it automatically connect with Arduino Board?
Reply ↓
The form will work with anything that uses usb for serial communication. This can be another
microprocessor or something else.
Reply ↓
First I would like to thank you for this helpful tutorial, I would like to inquire or rather consult my
experience while studying this program, I have followed all the stages that have been described, the
program can be run well without error warning appears, all COM Can be detected on combobox, but
when I have selected one COM in Combobox, the program is not running properly, it always appears
msgBox that I have to choose Comport first, but I have chosen one of them, is it because I use
VB2010 so there is little difference of setting , I hope you can give me help, thank you :)
Reply ↓
SerialPort1.Open()
connect_BTN.Text = “Dis-connect”
Timer1.Enabled = True
Timer_LBL.Text = “Timer: ON”
Else
MsgBox(“Select a COM port first”)
End If
If this is not working test the value of comPORT after you click the connect button. See what value it
has.
You can use the textbox to display the value of comPORT or any other messages:
RichTextBox1.Text &= “my message” & vbCrLf
Reply ↓
great work..
please make any projects using arduino rfid and visual basic
Reply ↓
It’s always a nOK and when 1111 comes out of the serial??
Reply ↓
Hi there,
Completely new to visual basic; when i use this code my visual basic wants me to declare
“serialport1″ as it cannot be found (for the connect_BTN_Click sub)
Anthony
Reply ↓
o Martynon August 22, 2017 at 12:39 pm said:
Are you using the files from the download or are you copy/pasting?
If copy/paste download the example files.
Reply ↓
Reply ↓
Hi Martyn,
when I try to compile the VB program I get 2 mistakes-
1) BC30506 Handles clause requires a WithEvents variable defined in the containing type or one of
its base types. Relates to this line -Private Sub ComPort_ComboBox_SelectedIndexChanged(sender
As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
2) BC30451 ‘comPort_ComboBox’ is not declared. It may be inaccessible due to its protection level.
Relates to all lines with comPort_ComboBox
I am using Microsoft Visual Studio 2017. Can u help with these mistakes please?!
Reply ↓
o Martynon September 14, 2017 at 5:03 am said:
Hi,k
unfortunately I cannot help with this. I only have VS 2013 and the code compiles fine with this
version.
Reply ↓
Reply ↓
Reply ↓
I have same article which guide to make communication serial between Arduino and Visual C++.
Any reader interested, please visit our blogspot:
http://engineer2you.blogspot.com/2016/12/arduino-serial-communication-visual.html
Reply ↓
33. Opart on January 31, 2018 at 7:44 am said:
Dear Martyn,
First I want to say Thank you very much for your sharing. Now I having problem only output data.
For Example ,
my message
31/01/18,14:39:28,0067.1
31/01/18,14:39:28,0067.1
31/01/18,14:39:28,0067.1
my message
31/01/18,14:39:28,0067.1
31/01/18,14:39:29,0067.1
my message
31/01/18,14:39:29,0067.1
31/01/18,14:39:29,0067.1
31/01/18my message <<<<<< so weird , It takes about 2-4 sec to appear one.
,14:39:29,0067.1
31/01/18,14:39:29,0067.1
31/01/18,14:39:30,0067.1
Reply ↓
I want to display data in bar chart. Arduino will send data and it will be displayed on bar chart. Like
spectrum of frequency.
Request you to help
Reply ↓
Reply ↓
Thank you very much for the article. Especially for the troubleshooting comment about RTS and
DTR, Would never be able to figure out myself why with the same sketch UNO R3 is responding and
Pro Micro is not. While the serial monitor shows no problem for both. Now by setting them Enable,
the Pro Micro works fine, and VB stopped reading from UNO R3 …
Reply ↓
Hi,
I am working on Azure IoT project using MXchip AZ3166 board. I installed visual basic studio and
Arduino. I installed Azure IoT workbench Dev kit in visual basic. I configured Aurdino path in it.
When I compiled a sample projects from Azure IoT workbench I am encountering a error stating that
install Arduino or add Arduino path and another error unable to find Aurdino path.
Kindly anyone suggest me some solutions to solve this error. Thank you.
Sri Krishna
Reply ↓
Reply ↓
Reply ↓
C ### visual part
Imports System
Imports System.IO.Ports
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
Timer1.Enabled = True
Timer_LBL.Text = "Timer: ON"
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
connect_BTN.Text = "Connect"
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
End If
End Sub
End Function
End Class
1