In this tutorial, I will be going over how to interface the HC-5 Bluetooth module to an Arduino board.
The HC-05 module is very straightforward and simple to use. It can be configured as a primary or secondary device. It has 6 pins for connections.
This device can be connected as a software serial port for any microprocessor. For this post, I will be use an Arduino nano.
The version I bought from eBay has a push button that has to be pressed during the configuration of the device.
For the FTDI board, make sure the jumper is set to 3.3V
Usually, the state pin allows the control of the AT mode. AT mode is used to configure the device. For the versions with the button, the state pin doesn’t need to be connected.
SoftwareThe Arduino code needed to talk to the HC-05 module is shown below:
#include <SoftwareSerial.h>
#define bt_tx_pin 7 // connect to hc-05 rx pin
#define bt_rx_pin 8 // connect to hc-05 tx pin
#define bt_en_pin 9 // connect to hc-05 en pin
#define bt_state_pin 10 // connect to hc-05 state pin
SoftwareSerial bt_serial(bt_rx_pin, bt_tx_pin);
void setup()
{
pinMode(bt_rx_pin, INPUT);
pinMode(bt_tx_pin, OUTPUT);
pinMode(bt_en_pin, OUTPUT);
pinMode(bt_state_pin, OUTPUT);
Serial.begin(9600);
bt_serial.begin(38400);
}
void loop()
{
if (bt_serial.available())
{
Serial.write(bt_serial.read());
}
if (Serial.available())
{
bt_serial.write(Serial.read());
}
}
Only a handful of AT commands are needed to configure the module as either a primary
or a secondary.
A link to all the AT commands are linked below:
In the event you encounter an error when sending an AT command, refer to this link:
ConfigurationAfter connecting up the module to the Arduino and loading the program, the module should be blinking rapidly.
Place in AT Mode
- Remove the GND pin connection
- Press and hold the button near the state pin
- Connect the GND pin back to the Arduino
- It should now to blinking every second
- Open up the terminal
Once you’re in the terminal, type
AT
and it should response back with
OK
Make sure that each command has a termination of \r\n
Get device address
One device will be the primary and the other will be the secondary device. Use the AT+ADDR?
command to find the address of each module
1st Device: Primary
>>> AT+ADDR?
+ADDR:14:1:141194
OK
2nd Device: Secondary
>>> AT+ADDR?
+ADDR:98D3:41:F5CC0B
OK
Configure SecondaryDevice
Hold on to the button while sending these commands
>>> AT+ORGL
OK
>>> AT+UART=38400,0,0
OK
>>> AT+RMAAD
OK
>>> AT+ROLE=0
OK
>>> AT+CMODE=0
OK
Let go of the button and type in AT+RESET
this will reboot the device. At this point, the device should be blinking rapidly
Next we configure the Primary device
Configure PrimaryDevice
Hold on to the button while sending these commands
>>> AT+ORGL
OK
>>> AT+UART=38400,0,0
OK
>>> AT+RMAAD
OK
>>> AT+ROLE=1
OK
>>> AT+CMODE=0
OK
>>> AT+BIND="98D3,41,F5CC0B"
OK
Let go of the button and type in AT+RESET
this will reboot the device. After a few seconds, the two devices should pair up. This will be indicated by unison repeated blinking every 1 second.
Connect the pair of Arduino/HC-05 to two different computers if possible, you can also connect them to the same computer. Open up a terminal of each of the Arduino boards, I use CoolTerm. It should look something like this. When you type on the left terminal, it will show up on the right one and vice versa.
That should be it. Now you have Arduino devices that can communicate with each other over Bluetooth.
Comments