21, Feb, 2019

Arduino and VB.NET Serial port Data logger


Arduino and VB.NET Serial port Data logger


  1. Create a new windows form application in VB Express
  2. Insert
    1. 1 Combo Box (comPort_ComboBox)
    2. 2 Button (connect_BTN, clear_BTN for connect &  clear)
    3. 1 Serial port controller (SerialPort1)
    4. 1 Timer (Timer1)
    5. 1 Label (Timer_LBL)
    6. 1 Rich Text Box (RichTextBox1)
  3. Copy and paste VB codes (shown below) thanks to martyn currey
  4. Upload the arduino codes to your arduino (shown below)
  5. Connect Arduino Digital IO 13 pin to LED +ve, Ultrasonic’s Echo Pin to Arduino Digital IO 7 and Ultrasonic’s  Trig Pin to Arduino Digital IO 8
  6. Build and Run the VB Program
  7. Check the accuracy by placing your hands on Ultrasonic Reader

Arduino Code

Connect  Arduino Digital IO 13 pin to LED +ve, Ultrasonic’s Echo Pin to Arduino Digital IO 7 and Ultrasonic’s  Trig Pin to Arduino Digital IO 8

#define echoPin 7 // Echo Pin

#define trigPin 8 // Trigger Pin

#define loadpin 13 // Trigger Pin

int jij = 0;

int maximumRange = 200; // Maximum range needed

int minimumRange = 0; // Minimum range needed

float duration, distance; // Duration used to calculate distance

void setup() {

Serial.begin(9600); // set serial speed

pinMode(trigPin, OUTPUT);

 pinMode(echoPin, INPUT);

 pinMode(loadpin, OUTPUT); // Use LED indicator (if required)

digitalWrite(loadpin, 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

digitalWrite(loadpin, HIGH); // turn on LED


//jij = 1;


else if (val == 0) // test for command 0 then turn off LED


digitalWrite(loadpin, LOW); // turn off LED




else // if not one of above command, do nothing


//val = val;



Serial.flush(); // clear serial port

jij = 3;


void deflstart()


   digitalWrite(trigPin, LOW);


 digitalWrite(trigPin, HIGH);


  digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

  //Calculate the distance (in cm) based on the speed of sound.

 distance = duration/5.81;

  if (distance >= maximumRange || distance <= minimumRange){

 /* Send a negative number to computer and Turn LED ON

 to indicate “out of range” */


 //digitalWrite(LEDPin, HIGH);


 else {


// digitalWrite(LEDPin, LOW);


  //Delay 50ms before next reading.



void deflstop()





Imports System

Imports System.IO.Ports

Imports System.Threading

Public Class Form1

    Dim comPORT As String

    Dim receivedData As String = “”

    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



    End Sub

    Private Sub comPort_ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged

        If (comPort_ComboBox.SelectedItem <> “”) Then

            comPORT = comPort_ComboBox.SelectedItem

        End If

    End Sub

    Private Sub connect_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles connect_BTN.Click

        If (connect_BTN.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


                connect_BTN.Text = “Dis-connect”

                Timer1.Enabled = True

                Timer_LBL.Text = “Timer: ON”


                MsgBox(“Select a COM port first”)

            End If



            connect_BTN.Text = “Connect”

            Timer1.Enabled = False

            Timer_LBL.Text = “Timer: OFF”

        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick


        receivedData = ReceiveSerialData()

        RichTextBox1.Text &= receivedData

    End Sub

    Function ReceiveSerialData() As String

        Dim Incoming As String


            Incoming = SerialPort1.ReadExisting()

            If Incoming Is Nothing Then

                Return “nothing” & vbCrLf


                Return Incoming

            End If

        Catch ex As TimeoutException

            Return “Error: Serial Port read timed out.”

        End Try

    End Function

    Private Sub clear_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clear_BTN.Click

        RichTextBox1.Text = “”

        receivedData = 0


        Timer1.Enabled = False

    End Sub

End Class

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>