Image may be NSFW.
Clik here to view.This is something that really interests us, connecting your Microcontrollers up to some sort of GUI in order to control it. This small project was originally just connected using a USB Cable (but that was boring!).
We have connected up a HC-06 Bluetooth module to an Arduino and are able to control it from a VB.NET application.
What’s required?
1x Arduino Uno
1x HC-06/HC-05 Bluetooth Module – £4 – £5 online.
Visual Studio (or anything to compile .NET applications)
What can this be used for?
- Controlling any Arduino based applications wirelessly.
- Sending/Receiving data from the Arduino.
- ….anything where you don’t want wires.
Once paired this will create a virtual serial connection between the Master/Slave device – This means you can program your Arduino sketches in the exact same way you would if you were using a USB cable. The .NET application we wrote allows us to Send and Receive this serial information in an external application.
Connecting it up
As the Bluetooth module uses 3.3v logic for RX (and Arduino uses 5v) we need it to make sure we can communicate with it. We have used two resistors 1.2KOhms and a 2.2KOhms to make a voltage divider. This will be used on the Arduino TX pin connected to the Bluetooth RX.
The other 3 pins are straightforward, 5v, TX (to Arduino RX) and Ground. This should be all that is needed to communicate with the Bluetooth module. It should look something like the following:
Image may be NSFW.
Clik here to view.
Let’s get started
Firstly we’ll be creating a basic Arduino sketch that outputs Serial information. It doesn’t matter what information you output; it could be text, numbers or a combination of both.
We have inserted | as a delimiter; for easy use in .NET we terminate each “piece of information” with a | character. For example if you wanted to send weather data you could use “t=15|hu=70|pr=40″ and the .NET application will split the string up into separate variables so you can display it however you want.
We are going to be using the following sketch for testing the connection:
The Arduino Sketch
int led = 13; boolean ledStatus = false; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); } void loop() { if (Serial.available()) { int inChar = getChar(); if (inChar == 'D') { Serial.println("This is some test data|"); } else if(inChar == 'L') { ledStatus = !ledStatus; if (ledStatus) { Serial.prntln("Turning light On.|"); digitalWrite(led, HIGH); } else { Serial.prntln("Turning light On.|"); digitalWrite(led, LOW); } } else { Serial.println("Unknown command received.|"); } } } char getChar() { while(Serial.available() == 0); return((char)Serial.read()); }
What this code will do is check for a couple of characters that have been sent to it via Serial – If it detects a ‘D’ then it will output a string of test data. If it receives an ‘L’ then it will toggle the on-board LED on and off. Any other character it receives will be met with an unknown command message.
Note: The Arduino we tested would not upload sketches properly with TX/RX connected. Make sure to disconnect these when uploading new sketches.
Now you have the sketch on your Arduino and have the Bluetooth module connected. You need to connect to this from your PC/Laptop. If you’re using Windows 7 just open up the Bluetooth device manage and “Add new Device” – All being well you should see this device listed. Connect and pair this device using the default passcode of 1234.
Now you’re connected you can test the connection directly; we used PuTTy to open a connection to the COM port that the Bluetooth module was assigned. We quickly tested that the sketch works, see image below:
Image may be NSFW.
Clik here to view.
If you see the above then you know the Bluetooth module is working and that your Arduino is sending/receiving data properly. The next step will be to make this happen in VB.NET. It works the exact same way, we’ll open a connection to the COM port, match up the baud rate and send/receive data.
The .NET application code
Imports System.IO Imports System.IO.Ports Imports System.Threading Public Class Form1 Dim buffer As String Delegate Sub myMethodDelegate(ByVal [text] As String) Dim bD1 As New myMethodDelegate(AddressOf process) Dim WithEvents SerialPort As New IO.Ports.SerialPort Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed If SerialPort.IsOpen() Then SerialPort.Close() End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load GetSerialPortNames() End Sub Sub GetSerialPortNames() For Each sp As String In My.Computer.Ports.SerialPortNames lstPorts.Items.Add(sp) Next End Sub Sub SendSerialData(ByVal Port As String, ByVal data As String) If (SerialPort.IsOpen) Then SerialPort.Write(data) Else MsgBox("Not connected to Port.") End If End Sub Sub process(ByVal myString As String) buffer = buffer + myString Dim str As String str = buffer If InStr(str, "|") Then Dim words As String() = str.Split(New Char() {"|"}) buffer = "" Dim word As String For Each word In words If (word.Length > 0) Then lstConsole.Items.Add(word) End If Next End If End Sub Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived Dim str As String = SerialPort.ReadExisting() Invoke(bD1, str) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If lstPorts.SelectedIndex <> -1 Then Try If SerialPort.IsOpen Then SerialPort.Close() Button2.Text = "Connect" Else SerialPort.PortName = lstPorts.SelectedItem.ToString SerialPort.BaudRate = 9600 SerialPort.DataBits = 8 SerialPort.Parity = Parity.None SerialPort.StopBits = StopBits.One SerialPort.Handshake = Handshake.None SerialPort.Encoding = System.Text.Encoding.Default SerialPort.Open() Button2.Text = "Disconnect" End If Catch ex As Exception MsgBox(ex.Message) End Try Else MsgBox("Please choose a serial port", vbInformation, "Serial Port") End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Application.Exit() If SerialPort.IsOpen Then SerialPort.Close() End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (lstPorts.SelectedIndex <> -1) Then SendSerialData(lstPorts.SelectedItem.ToString, "L") End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If (lstPorts.SelectedIndex <> -1) Then SendSerialData(lstPorts.SelectedItem.ToString, "D") End If End Sub End Class
What this application does is connect to a COM port of your choosing and then send a character serially to that port. It also listens for incoming data and appends it in the list box.
You can download the MS Visual Studio 2010/2013 project files by clicking here: ArduinoCOM. The application should look like the following once loaded:
Image may be NSFW.
Clik here to view.
Please note that this is a test application. You will need to do your own error checking and additional programming, this is just a basic example of how it can be done.
All being well you’ve just added Bluetooth functionality to your Arduino. It’s a reliable, cheap way of removing the constraint of wires when connecting it up to a computer. This also allows endless limits with regards to control, some obvious uses for this would be a data logger, robot/rc car control, making a controller/joystick etc. the list is endless.
If you think we’ve missed any steps here; or have any comments, questions or suggestions then please feel free to leave us a message in the comment section below or send us a message using the contact page.
That’s all for now!