Coding Games on an ILI9341 SPI LCD Touchscreen with Arduino
3rd January 2021Using AT Commands to Set Up Your Bluetooth Module : HC-05 and HC-06
4th February 2021Adding Bluetooth to Your Arduino Projects
Sometimes we want to be able to control and monitor our electronics projects remotely. Maybe our device is somewhere that makes it hard to access it or maybe we just want a nice user-friendly interface on our mobile phone so we can see what’s happening and make changes. Being able to connect wirelessly opens up a whole range of exciting possibilities for your projects.
Types of Wireless Connection
There are a range of wireless technologies that we can use.
LoRa (Long Range) technology uses license free radio frequencies to create radio connections between devices. It offers low power consumption with a range of up to 10 km in open space. It is widely used when your project isn’t near a Wi-Fi connection and when you need to make a connection from a distance.
Wi-Fi should be very familiar to everyone. This makes use of a Wi-Fi hotspot which is connected to the Internet. We can connect project to the Wi-Fi hotspot and then access our electronics from anywhere in the world. A lot of the time will use this to connect our project to our home Wi-Fi and then control it from our computer.
Bluetooth is ideal for short range communication of up to 10 m. We can connect our project to any Bluetooth enabled device and then transfer data to and from it. This is the technology we are going to be using in this project.
Adding Bluetooth to an Arduino
Most Arduino boards don’t have a Bluetooth connection built-in. We need to add one using a Bluetooth module. If you do a search on Amazon or eBay you’ll find a range of devices for under £10. These are usually built around an HC-05, HC-06, or similar module which is usually mounted on a small breakout board that lets you easily plug it into your breadboard.
These Bluetooth modules basically allow you to extend a serial port on your Arduino over a Bluetooth connection to another device. As far as our Arduino is concerned we’ll simply be talking to a serial device and a Bluetooth module will then handle the wireless communication.
In this project I’ll be using an HC-06 based module which is a slave only device. This means that I can use a phone or similar to connect to my Arduino project, but that my Arduino project will be able to initiate a connection with other Bluetooth devices. If you need one Arduino to monitor and control a second Arduino over Bluetooth you’ll need to use an HC-05 based Bluetooth module.
Where to Get Your Bluetooth Modules
Amazon:
HC-05 Bluetooth module : https://amzn.to/3oyCqd5
HC-06 (slave only) : https://amzn.to/3oyCqd5
eBay:
HC-05 Bluetooth module : http://ebay.us/qODpaV
HC-06 (slave only) : http://ebay.us/Opu3Zs
Setting up Your Bluetooth Module
When you first receive your Bluetooth module it will have some default factory settings built into it. The main settings that concern us for the serial port speed and the devices broadcast name. Usually the default port speed is 9600 baud which is fine for short messages. The device broadcast name will vary between manufacturer but will be some generic string of characters.
We don’t need to change any of the settings to use the device but if you want a faster baud rate or want your device to broadcast a relevant name (e.g. MY_PROJECT) please have a look at this tutorial which will take you through using a serial port to connect the Bluetooth module to your computer so you can change settings.
Connecting Your Bluetooth Module to Arduino
The connections are actually very simple. We need to supply power to the Bluetooth module along with two data connections for the Tx and Rx signals.
GND should be connected to GND on the Arduino.
Vcc can usually connected to 5 V from the Arduino, but please check the specification on your Bluetooth module as internally they usually work at 3.3 V. This is usually the advantage of buying a device mounted on a breakout board as that usually contains a small voltage regulator.
The Rx connection on the Bluetooth module is where we will send data from the Arduino to the module. This will be connected to a transmit pin on the Arduino. Most Bluetooth modules work at 3.3 V logic so will need to use a level shifter (or a simple potential divider) to drive the signal. In the circuit below I’ve used a 2K2 and 5K7 resistors for the divider.
The Tx connection the Bluetooth module sends data from Bluetooth to the Arduino, so this needs to be connected to receive pin on Arduino. Again this will be a 3.3 V logic signal but this can usually be fed directly into the Arduino and it will work fine. If you do have problems then you might need to use a proper level shifting module to connect the signals.
Although the Arduino has a hardware serial port and you’ll see a couple of pins labelled Tx and Rx on the board they are not always the most convenient to use. If you’re using an Arduino Uno or Nano the serial port is also used for communication with your computer. If we connect devices to the hardware serial port it can mean that we have to unplug parts of our circuit before we can reprogram the Arduino.
For this reason I tend to use a software serial port which lets me use any two pins as the transmit and receive connections, leaving the hardware Tx and Rx pins free so that I can leave the Arduino connected to my PC as I develop the code. Once the project is up and running we can always move back to the hardware port if we want to.
Adding a Software Serial Port
Adding a software powered serial port is simply achieved by importing a very handy package that comes bundled with the Arduino IDE. At the top of our code listing we simply include the line,
#include <SoftwareSerial.h>
We then need to initialise the port and tell it which pins to use for the transmit and receive signals. In this example I’m using digital pin 9 as the Arduino is receive pin and 8 as the Arduino’s transmit pin. Don’t forget that the Arduino Rx in needs to be connected to the Bluetooth module Tx pin, and the Arduino Tx pin needs to be connected to the Bluetooth module Rx pin through a voltage divider.
const byte rxPin = 9; const byte txPin = 8; SoftwareSerial BTSerial(rxPin, txPin); // RX TX
After that we simply need to initialise the serial port and tell it what baud rate to use.
BTSerial.begin(9600);
Again, if you’ve altered the default baud rate you’ll need to put in the correct value.
After that your Arduino now has a working Bluetooth connection.
Having said that we are still not able to use it. The second half of the project is to connect a Bluetooth enabled device to the Arduino’s Bluetooth connection so that we can start sending and receiving data between the two.
Using a Bluetooth Terminal App to Test Our Connection
The easiest way to test that we got a working Bluetooth connection is to use a simple serial terminal app on our phone. If you search on your app store for a serial Bluetooth terminal you should find one that you can install for free.
To get this working you need to turn on the Arduino Bluetooth module and pair it with your phone. If you’re asked for a pairing code it’s usually 0000 or 1234. Once the two are connected you’ll be able to send and receive data from your Arduino sketch using the SoftwareSerial library methods.
The available() method tells you how many bytes of data are available for reading from the Bluetooth connection. As data is sent to the Arduino it is put in a buffer. You can use the read() method to read the next byte from the buffer which is then removed from the buffer. In this way the available () method always tells you how many bytes of unread data there are.
To send data over the Bluetooth connection use the write() or print() methods. write() allows you to send a single byte of data e.g. write(67) or a fixed string of data, e.g. write(“hello”). print() works the same as the normal Serial.print() command and allows you to send a string variable over the port, e.g. print(myMessage).
Making Use Of The Bluetooth Connection
Now that we’ve got a working Bluetooth connection and can send and receive data to mobile phone with all the building blocks we need to make a wireless project.
Whilst the Bluetooth serial terminal is great for testing is not a very user-friendly interface. We could really do with the proper mobile app. This does sound a bit daunting but don’t worry, there are some great, free software that lets us build user interfaces with just a few clicks.
But let’s cover this in the next tutorial.
Code for Bluetooth Terminal Test
#include "Arduino.h" #include <SoftwareSerial.h> const byte rxPin = 9; const byte txPin = 8; SoftwareSerial BTSerial(rxPin, txPin); // RX TX void setup() { // define pin modes for tx, rx: pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); BTSerial.begin(9600); Serial.begin(9600); } String messageBuffer = ""; String message = ""; void loop() { while (BTSerial.available() > 0) { char data = (char) BTSerial.read(); messageBuffer += data; if (data == ';'){ message = messageBuffer; messageBuffer = ""; Serial.print(message); // send to serial monitor message = "You sent " + message; BTSerial.print(message); // send back to bluetooth terminal } } }
Links
Amazon:
HC-05 Bluetooth module : https://amzn.to/3oyCqd5
HC-06 (slave only) : https://amzn.to/3oyCqd5
eBay:
HC-05 Bluetooth module : http://ebay.us/qODpaV
HC-06 (slave only) : http://ebay.us/Opu3Zs