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.5
// 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
| 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.
Note that there is an older version of the HC-05 (+VERSION:2.0-20100601).
To bring this into AT command mode, you need to push down the button before you power up the module.
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
// 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=38400,0,0 OK set to 38400, 1 stop bit, no parity
// AT+NAME? +NAME:HC-05 get the name
// 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 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 (Master/Slave) module is 38400 in command mode and 9600 in data mode.
- The max baud rate you can use is 38400. Anything higher will get garbage.
- 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(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
|
AT+NAME?
| +NAME:HC-05
| Get module name
|
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=38400,0,0
| OK
| Set baud to 38400, 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 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 is correct.
|
// Control a LED wirelessly using Bluetooth
#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX
int led = 13;
int val;
int onFlag;
void setup() {
pinMode(led, OUTPUT);
BTSerial.begin(9600); // baud rate for the Bluetooth
onFlag = -1;
BTSerial.println("Press 1=on; 0=off");
}
void loop() {
if(BTSerial.available()) { // check if there's any incoming data on the Bluetooth
val = BTSerial.read(); // if there is then read in the data
if(onFlag == 0) { // if LED is currently off and command is to turn it on
if(val == '1') {
digitalWrite(led, HIGH);
BTSerial.println("LED on");
onFlag = 1;
BTSerial.println("Press 0 to turn off LED");
}
} else if(onFlag == 1) { // if LED is currently on and command is to turn it off
if(val == '0') {
digitalWrite(led, LOW);
BTSerial.println("LED off");
onFlag = 0;
BTSerial.println("Press 1 to turn on LED");
}
} else {
onFlag = 0;
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
| Connect the Bluetooth module to your Windows PC. The following instructions are for Windows 7. Click here for Windows 11.
|
10a
| Make sure that the Bluetooth on your Windows PC is turned on.
|
10b
| Open up the Control Panel in Windows.
- Select View Devices and Printers
- If your Bluetooth module appears in the list of installed devices, then skip the following and go to step 12. Otherwise go to step 10.
|
|
10c
| In the Devices and Printers window
Select Add a Device
Select your Bluetooth module RobotsForFun-BT when it shows up
Click Next
|
10d
| When it asks for the passcode pin, enter 1234 for the default passcode if you have not changed it to something else in step 5.
|
|
10e
| Right-click on your Bluetooth device icon and select Properties. Under the Hardware tab, note the COM port number that the device is connected to. You'll need this COM port number in the next step.
|
|
11
| Download and run PuTTY.
In the configuration screen,
- Select Serial
- Type in the correct COM port number (obtained from step 14)
- 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 stop blinking and will stay on, and the PuTTy terminal will open.
|
|
12
| In the PuTTY terminal, type in a 1 to turn on the LED on the Arduino, and type in a 0 to turn it off.
Observe the LED on the Arduino to see that it is responding correctly.
|
|
|
|
| |