There is a slight difference between configuring the HC-06 and the HC-05 modules
|
Configuring the HC-06 (Slave Only) Bluetooth module
|
| In this section you will learn how to configure the HC-06 (Slave Only) Bluetooth module.
|
1
| Making the connections
Bluetooth pins
| Arduino pins
| Description
|
TX
| 2 (RX)
| Receive
|
RX
| 3 (TX)
| Transmit
|
VCC
| 5V
| Power
|
GND
| GND
| Ground
|
|
2
| Create a new program in your Arduino IDE and copy the code on the right.
|
// This program allows you to communicate and configure
// the HC-06 (Slave Only) Bluetooth module using the
// Arduino serial monitor
//
// Connections
// HC-06 (Slave Only) Arduino
// TX 2 (RX)
// RX 3 (TX)
// VCC 5V
// GND GND
// Serial Monitor settings should be No line ending and 9600 baud
// AT commands to type in the monitor Response
// for the HT-06 (Slave Only)
// AT OK it's working
// AT+VERSION linvorV1.8
// AT+BAUD4 for 9600 OK9600
// AT+BAUD6 for 38400 OK38400
// AT+BAUD8 for 115200 OK115200
// AT+NAMERobotsForFun-BT OKsetname set the name
// AT+PIN1234 OKsetpin set the password
//
// commands are case sensitive
// Click on the Send button to send the command to the Bluetooth
// The response will print in the monitor
// c 2015 Enoch Hwang
#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX
void setup() {
// If you do not get the initial OK, it is because the connection and/or the baud is wrong
// If you see garbage, it is because the baud is wrong
// The default baud rate for the HC-06 (Slave only) Bluetooth module is 9600
Serial.begin(9600); // baud rate for the Serial monitor
BTSerial.begin(9600); // // The default baud rate is 9600
Serial.println("Enter AT commands:");
// if connection and baud are correct then should get OK response
BTSerial.write("AT");
}
void loop() {
if (BTSerial.available()){
Serial.write(BTSerial.read());
}
if (Serial.available())
BTSerial.write(Serial.read());
}
|
3
| Upload and run the program.
|
4
| Open the Serial Monitor. The monitor settings should be:
You should get an initial OK on the Serial Monitor.
- If you do not get the initial OK, it is because the connection and/or the baud rate setting is wrong.
- If you see garbage, it is because the baud rate is wrong.
- The default baud rate for the HC-06 (Slave Only) module is 9600.
- If you do not see the initial OK, or if you enter AT and it does not reply with OK then
change the baud rate for the line BTSerial.begin(9600) to BTSerial.begin(38400).
If it still does not work then change to another baud rate.
|
|
5
|
The following is a summary of some of the available AT commands.
The commands are CASE sensitive.
After typing the command in the Serial Monitor, you need to
click the Send button to send the command to the Bluetooth module.
The response will be printed in the monitor.
AT commands
| Response
| Comment
|
AT
| OK
|
|
AT+VERSION
| linvorV1.8
| Get version number
|
AT+BAUD4
| OK9600
| Set baud rate to 9600
|
AT+BAUD6
| OK38400
| Set baud rate to 38400
|
AT+BAUD8
| OK115200
| Set baud rate to 115200
|
AT+NAMERobotsForFun-BT
| OKsetname
| Change Bluetooth module name to RobotsForFun-BT. Max 20 characters.
|
AT+PIN1234
| OKsetpin
| Change password to 1234
|
|
|
Configuring the HC-05 (Master/Slave) Bluetooth module
|
| In this section you will learn how to configure the HC-05 (Master/Slave) Bluetooth module.
There are two versions of the HC-05:
+VERSION:4.0-20190815 with the gold check mark is the newer version
+VERSION:2.0-20100601 without the gold check mark is the older version.
The description below is for the newer version.
|
1
| Making the connections
Bluetooth pins
| Arduino pins
| Description
|
TX
| 2 (RX)
| Receive
|
RX
| 3 (TX)
| Transmit
|
KEY/EN
| 5V
| This connection is only for the new version with the gold check mark. HIGH = AT command mode LOW (default) = data mode
|
VCC
| 5V
| Power
|
GND
| GND
| Ground
|
After powering up the module the status led should blink slowly (4 second intervals) telling you that it is now in AT command mode.
For the older version (without the gold checkmark) of the HC-05 (+VERSION:2.0-20100601),
you need to push down the button before you power up the module to bring the module into AT command mode.
The AT commands also work a bit different from what is listed below.
|
2
| Create a new program in your Arduino IDE and copy the code on the right.
|
// This program allows you to communicate and configure
// the HC-05 (Master/Slave) Bluetooth module using the
// Arduino serial monitor
//
// Connections
// HC-05 Master/Slave Arduino
// TX 2 (RX)
// RX 3 (TX)
// KEY/EN 5V (only for the new version with the gold check mark)
// VCC 5V
// GND GND
// Serial Monitor settings should be Both NL & CR and 9600 baud
// AT commands to type in the monitor Response
// for the HT-05 (Master/Slave)
// AT OK
// AT+VERSION? +VERSION:4.0-20190815
// AT+UART? +UART:9600,0,0 default 9600, 1 stop bit, no parity
// AT+UART=9600,0,0 OK set to 9600, 1 stop bit, no parity
// AT+NAME? +NAME:HC-05 get the name (new version only)
// AT+NAME=RobotsForFun-BT +NAME:RobotsForFun-BT set the name
// AT+PSWD? +PSWD:1234 get the password
// AT+PSWD=1234 OK set the password
// invalid command ERROR:[0]
//
// commands are case sensitive
// Click on the Send button to send the command to the Bluetooth
// The response will print in the monitor
//
// c 2015 Enoch Hwang
#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX
void setup() {
// If you do not get the initial OK, it is because the connection and/or the baud is wrong
// If you see garbage, it is because the baud is wrong
// The default baud rate for the HC-05 (Master/Slave) Bluetooth module is 38400
Serial.begin(9600); // baud rate for the Serial monitor
BTSerial.begin(38400); // The default baud rate is 38400
Serial.println("Enter AT commands:");
// if the connection and the baud rate are correct then should get OK response on the monitor
BTSerial.write("AT\r\n");
delay(500);
BTSerial.write("AT+VERSION?\r\n");
}
void loop() {
if (BTSerial.available()){
Serial.write(BTSerial.read());
}
if (Serial.available())
BTSerial.write(Serial.read());
}
|
3
| Upload and run the program.
|
4
| Open the Serial Monitor. The monitor settings should be:
Type AT and click Send in the Serial Monitor. You should get an OK response.
- If you do not get an OK response, it is because the connection and/or the baud rate setting is wrong.
- If you see garbage, it is because the baud rate is wrong.
- Try each of the following AT query commands such as AT+VERSION?.
- The default baud rate for the HC-06 (Master/Slave) module is 38400 in command mode and 9600 in data mode. The Serial Monitor baud rate should be set at 9600.
- The max baud rate you can use is 38400. Anything higher will get garbage.
- If you do not see the OK, or if you enter AT and it does not reply with OK then change the baud rate for the line BTSerial.begin(38400) to BTSerial.begin(9600). If it still does not work then change to another baud rate.
|
|
5
|
The following is a summary of some of the available AT commands.
The commands are CASE sensitive.
After typing the command in the Serial Monitor, you need to
click the Send button to send the command to the Bluetooth module.
The response will be printed in the monitor.
AT commands
| Response
| Comment
|
AT
| OK
|
|
AT+VERSION?
| +VERSION:4.0-20190815
| Get version number (new version)
|
| +VERSION:2.0-20100601
| (old version)
|
AT+NAME?
| +NAME:HC-05
| Get module name (new version only)
|
AT+NAME=RobotsForFun-BT
| +NAME:RobotsForFun-BT
| Set module name to RobotsForFun-BT. Max 20 characters.
|
AT+UART?
| +UART:38400,0,0
| Get baud
|
AT+UART=9600,0,0
| OK
| Set baud to 9600, 1 stop bit, no parity
|
AT+PSWD?
| +PSWD:1234
| Get password
|
AT+PSWD=1234
| OK
| Set password to 1234
|
AT+ROLE?
| +ROLE:0
| Get role. 0=slave (default); 1=master
|
AT+ROLE=1
| OK
| Set role to master
|
|
|
Using Bluetooth to Control a LED
|
| In this section you will learn how to control a LED wirelessly using the Bluetooth module.
We will connect the PC wirelessly using Bluetooth to the Bluetooth module that is connected to the Arduino.
The PC Bluetooth is the master and the Arduino Bluetooth is the slave.
On the PC side, we will run PuTTy, a terminal monitor program, to send commands wirelessly to the Arduino.
The Arduino will execute the on/off commands received from the Bluetooth module to turn on or off the built-in LED.
|
|
6
| Setup
If you are using the HC-06, just power it up.
If you are using the HC-05 (Master/Slave) then you need to first set its role to slave with the command AT+ROLE=0 and then power it up into data mode (i.e. not AT command mode).
The PC or your mobile phone is always the master, so your HC-05 must be in the slave mode, otherwise it will not show up in the Bluetooth scan list.
|
7
| Create a new program in your Arduino IDE and copy the code on the right.
Make sure that the baud rate specified in the BTSerial.begin(9600) line matches the actual baud rate of your bluetooth module. If it is not 9600 then change the value to match.
|
// Control a LED wirelessly using Bluetooth
#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX
const int led = 13;
void setup() {
pinMode(led, OUTPUT);
BTSerial.begin(9600); // baud rate for the Bluetooth
BTSerial.println("Press 1=on; 0=off");
}
void loop() {
if(BTSerial.available()) { // check if there's any incoming data on the Bluetooth
char value = BTSerial.read(); // if there is then read in the data
if (value == '1') { // turn on led
digitalWrite(led, HIGH);
BTSerial.println("LED on");
} else if (value == '0') { // turn off led
digitalWrite(led, LOW);
BTSerial.println("LED off");
} else { // invalid character
BTSerial.println("Press 1=on; 0=off");
}
}
}
|
8
| Upload the program to the Arduino.
|
9
| You may want to power up your Bluetooth module and Arduino as a standalone, i.e. not using the USB cable connected to your PC but connected to a battery. This way, you will know for sure that the communication is through the wireless Bluetooth. The LED on the Bluetooth module should blink.
|
10
| Pair the Bluetooth module to your Windows PC. (Note that this is just pairing and not connecting the module with your computer.) The following instructions are for Windows 11. Click here for Windows 7.
|
10a
| Make sure that the Bluetooth on your Windows PC is turned on.
|
10b
| In the Search, type Bluetooth and select Bluetooth and other devices settings
|
10c
| If the Bluetooth module name is already listed in the Other Devices then you can go to step 10h.
|
10d
| Click on Add Devices.
|
|
10e
| In the Add a device window, click Bluetooth.
|
|
10f
| It will take a few seconds to scan for nearby Bluetooth signals. Select the name of your Bluetooth module (RobotsForFun-BT) and type 1234 for the pin.
|
|
10g
| You will see the word Connected for a few seconds and then it will change to Not connected.
This is correct because you are only pairing and NOT connecting the Bluetooth module with your computer.
Click Done.
|
|
10h
| Back in the Devices window, click on More Bluetooth settings.
|
|
10i
| In the Bluetooth Settings window, click on the COM Ports tab and note down the COM port number for the Outgoing RobotsForFun-BT line. This COM port number will be used in step 11.
|
|
11
| Download and run PuTTY.
PuTTY is a terminal app that you can use to connect and communicate with the Bluetooth module.
In the configuration screen,
- Select Serial
- Type in the correct COM port number (obtained from step 10i above)
- Leave the speed at the default 9600. The speed must match the baud rate that you configured your Bluetooth module to (from steps 4 & 5), and also must match the speed in the BTSerial.begin(9600) line in your Sketch (from step 7).
- Click Open to start the serial terminal.
When the connection is successfully made, the LED on the Bluetooth module will either stay on or do a double flash, and the PuTTy terminal will open.
|
|
12
| In the PuTTY terminal, type in a 1 followed by Enter to turn on the LED on the Arduino, and type in a 0 and Enter to turn it off.
Observe the LED on the Arduino to see that it is responding correctly.
|
|
|
|
|