3.3.1 Bluetooth Command Settings Knowledge
The embedded Bluetooth serial communication module allows us to break free from the constraints of cables and achieve wireless serial communication within a limited range, with two working modes: command response mode and automatic connection mode. In automatic connection mode, the module can be divided into three roles: Master, Slave, and Loopback. When the module is in automatic connection mode, it will automatically connect according to the pre-set data transmission method; when the module is in command response mode, it can execute all the following AT commands. Users can send various AT instructions to the module to set control parameters or issue control commands. The common HC-05 Bluetooth module belongs to the integrated type, while the HC-06 belongs to the slave module type. Their commonly used AT commands include testing communication, renaming, changing baud rate, and changing pairing password. By controlling the external pin (PIO11) input level, the module’s working status can be dynamically switched. After switching to command response mode, AT instructions must be set from the TXD and RXD signal pins and cannot be set through the Bluetooth channel. The device sending AT instructions can be various types of microcontrollers such as Arduino, or computers via serial ports like CP2102 USB to TTL serial modules.
Special Notes:
1 Both master and slave modules cannot switch working modes; they can only operate in a single working mode (master or slave).
2 Master modules cannot pair and connect with each other, nor can they pair with other Bluetooth devices such as computers or mobile phones.
3 Slave modules can pair with Bluetooth-enabled computers or some Bluetooth-enabled mobile phones, but slave modules cannot connect with each other.
4 Both master and slave modules can directly connect to various TTL level serial microcontrollers (5V microcontrollers should be connected in series with a 1K resistor).
5 Each Bluetooth module has a corresponding data manual (specification) and AT instruction set. If you are not familiar with it, please ask the supplier for it. This can help you quickly grasp the usage methods.
3.3.2 USB to TTL Serial Module Setting Bluetooth Commands
3.3.2.1 USB to TTL Serial Module
Digital devices like Bluetooth modules communicate using TTL levels, while modern computers mainly communicate with other devices via USB. A USB to TTL serial module is needed to simulate a serial port communication between the two.
Figure 3.3.1 USBtoTTLSerial Module
USB to TTL serial modules generally use CH340, PL2303, CP2102, FT232R chips, and require Windows drivers. It is recommended to use the FT232R > CP2102 > PL2303 chip. The left image of Figure 3.3.1 shows a module using the CP2102 chip, while the right image can switch between TTL, RS232, and RS485 levels based on settings. Generally, the USB interface receives power, with +3.3V (<40mA) and +5V pins providing power output, TXD as the data sending pin, RXD as the data receiving pin, and GND as the grounding pin. The TTL signal level is 3.3V, where a positive logic level of 3.3V indicates a logical one, and a 0V level indicates a logical zero. The maximum current output is 500mA, which can power microcontrollers, Bluetooth modules, and other devices.
The onboard status indicator light and the send/receive indicator light; after correctly installing the driver and generating a virtual serial port, the status indicator light will remain on, and the send/receive indicator light will blink during communication, with brightness decreasing at higher baud rates.
3.3.2.2 Circuit Connection
Figure 3.3.2 USB to TTL Serial Module Connection Schematic with Bluetooth Module
USB to TTL Module |
Bluetooth Module |
VCC |
VCC |
GND |
GND |
RXD |
TXD |
TXD |
RXD |
After installing the appropriate driver, plug the USB to serial module into the computer’s USB port, open the control panel, and enter the device manager interface. Expand the ports (COM and LPT), and you can see that the CP2102 driver has been installed, occupying port number COM12. This port number will be used later when using serial debugging software.
Figure 3.3.3 Device Manager Corresponding Serial Port Number
3.3.2.3 Bluetooth Module HC-05 Circuit Diagram and Master Role Query
By referring to Figure 3.2.2, Figure 3.3.3, and Table 3.3.1, we can better understand the key terms involved in various literature and the various states of the Bluetooth module.
Figure 3.3.3 Circuit Diagram of HC-05 and Compatible Modules
Pin Definitions Used by the Serial Module
Table3.3.1 Bluetooth HC-05 Pin Number Reference Table
Number |
Pin Name |
Number |
Pin Name |
1 |
TX |
20 |
USB D+ |
2 |
RX |
21 |
GND |
3 |
CTS |
22 |
GND |
4 |
RTS |
23 |
PI00 |
5 |
PCM CLK |
24 |
PI01 |
6 |
PCM OUT |
25 |
PI02 |
7 |
PCM IN |
26 |
PI03 |
8 |
PCM SYNC |
27 |
PI04 |
9 |
AI00 |
28 |
PI05 |
10 |
AI01 |
29 |
PI06 |
11 |
RESET |
30 |
PI07 |
12 |
3.3V |
31 |
PI08 |
13 |
GND |
32 |
PI09 |
14 |
NC |
33 |
PI010 |
15 |
USB D- |
34 |
PI011 |
16 |
CSB |
||
17 |
MOSI |
||
18 |
MISO |
||
19 |
CLK |
1、PIO8 connects to LED, indicating the module’s working status. The module flashes after powering on, with different flashing intervals for different states. In pairing state, it flashes quickly for 1 second, twice; in successful pairing state, it flashes twice, once every 2 seconds.
2 PIO9 connects to LED, indicating successful connection. The LED remains on after successful Bluetooth serial matching.
3 PIO11 is the module status switching pin, where high level indicates AT command response working state, and low level or floating indicates the normal Bluetooth working state. After entering command response working state, the LED indicator will blink slowly, once every second. This pin is usually related to the AT mark and KEY mark.
4 The module already has a reset circuit, and power cycling will complete the reset.
Steps to Set as Master Module
1 PIO11 set high, as shown in Figure 3.3.3, press the button in Figure 3.2.2 or connect the AT pin to high level in Figure 3.2.4 or switch the AT switch to On in Figure 3.2.5.
2 Plug the USB to TTL serial module connected to the Bluetooth HC-05 module into the computer’s USB port, commonly known as powering on, which puts the module into AT command response state.
3 Using HyperTerminal or other serial tools, set the baud rate to 38400, data bits to 8, stop bits to 1, no parity, no flow control.
4 Use serial debugging assistant software to send the character “AT+ROLE=1\r\n”, and successfully return “OK\r\n”, where \r\n indicates carriage return and line feed.
5 Set PIO11 low, power off and then power on again, the module will be the master module, automatically searching for slave modules and establishing connections.
3.3.2.4 Serial Debugging Assistant SSCOM Testing Bluetooth AT Commands
There are many serial debugging tools, such as Serialport, SecureCRT, AccessPort, putty, SSCOM (DingDing Serial Debugging Assistant), HyperTerminal, UartAssist, NetAssist, PC Serial Assistant. Although SecureCRT is a good software, its all-English interface is not suited for local conditions. In contrast, the domestic DingDing Serial Debugging Assistant appears frequently in major forums and is widely popular for its practical functionality and convenient operation.
Double-click SSCOM.exe, as shown in Figure 3.3.4, click the button at the location of the button to get a similar interface as in Figure 3.3.4.
Select several commands from the Bluetooth module’s accompanying AT instruction set and input them one by one into the right string area of Figure 3.3.4.
Set to the actual connected serial port number like COM12, to 38400, check , and click . After checking , the SSCOM serial debugging software will send the first to the sixteenth AT instructions in a loop every 2 seconds, and the feedback information will be shown in the left area of Figure 3.3.4.
After closing the SSCOM software, be sure to save the backup configuration file sscom.ini. This file stores the corresponding test AT instructions from the right area of Figure 3.3.4.
Figure 3.3.4 SSCOM Testing Bluetooth Module Commands
Most Bluetooth HC-05 AT commands only require PIO11 to be set high once for settings, but several commands like AT+NAME? require it to be held high continuously for the command to be effective. Therefore, to be safe, it is advisable to keep pressing the button during the testing period to keep PIO11 in a high state.
AT commands are case-insensitive and end with carriage return and line feed characters\r\n, so be sure to check .
3.3.2.4 Bluetooth Testing Software
Figure 3.3.6 Bluetooth Testing Software Interface
Generally, Bluetooth manufacturers or merchants will also provide Bluetooth module testing software for users to conveniently test. Under the circuit connection state shown in Figure 3.3.2, using this software allows you to obtain or update Bluetooth module information and conveniently observe the effects of various Bluetooth AT command settings.
3.3.2.5 Bluetooth Module HC-05 AT Command Error Code Explanation
Table 3.3.2 Bluetooth HC-05 AT Command Error Code Explanation |
|
Error code return format —ERROR:(error_code) |
|
error_code(hexadecimal number) |
Note |
0 |
AT command error |
1 |
Instruction result is the default value |
2 |
PSKEY write error |
3 |
Device name too long (exceeds 32 bytes) |
4 |
Device name length is zero |
5 |
Bluetooth address: NAP too long |
6 |
Bluetooth address: UAP too long |
7 |
Bluetooth address: LAP too long |
8 |
PIO number mask length is zero |
9 |
No PIO numbers |
A |
Device class length is zero |
B |
Device class number too long |
C |
Query access code length is zero |
D |
Query access code number too long |
E |
Invalid query access code |
F |
Pairing code length is zero |
10 |
Pairing code too long (exceeds 16 bytes) |
11 |
Module role invalid |
12 |
Baud rate invalid |
13 |
Stop bit invalid |
14 |
Parity bit invalid |
15 |
Pairing list does not contain authenticated devices |
16 |
SPP library not initialized |
17 |
SPP library re-initialized |
18 |
Invalid query mode |
19 |
Query timeout too large |
1A |
Bluetooth address is zero |
1B |
Invalid security mode |
1C |
Invalid encryption mode |
3.3.3 Arduino Program Setting Bluetooth Commands
Using Arduino can also set Bluetooth commands programmatically.
For the Bluetooth module shown in Figure 3.2.4, connect Bluetooth to Arduino according to RXD-TX, TXD-RX, VCC-VCC, GND-GND, AT-2. After burning the program shown in Table 3.3.3 to the Arduino main control board, press the reset button on the Arduino to let the program run once. When the onboard LED light blinks with a 500ms interval, it indicates successful setup.
Table 3.3.3 Arduino_bluetooth_set.ino |
|
1 |
#define AT 2 |
2 |
#define board_led 13 |
3 |
void setup(){ |
4 |
pinMode(board_led,OUTPUT); |
5 |
pinMode(AT,OUTPUT); |
6 |
digitalWrite(AT,HIGH); |
7 |
Serial.begin(38400); // Should match the baud rate of your module |
8 |
delay(1000); |
9 |
Serial.println(“AT”); |
10 |
delay(1000); |
11 |
Serial.println(“AT+NAME=Master”); // Name the module |
12 |
delay(1000); |
13 |
Serial.println(“AT+ROLE=0”); // Set master/slave mode: 0 slave, 1 master |
14 |
delay(1000); |
15 |
Serial.println(“AT+PSWD=1234”); // Set pairing password, e.g., 1234 |
16 |
delay(1000); |
17 |
Serial.println(“AT+UART=9600,0,0”); // Set baud rate 9600 , stop bits 1 , no parity |
18 |
delay(100); |
19 |
Serial.println(“AT+RMAAD”);// Clear pairing list |
20 |
} |
21 |
void loop(){ |
22 |
digitalWrite(board_led, HIGH); |
23 |
delay(500); |
24 |
digitalWrite(board_led, LOW); |
25 |
delay(500); |
26 |
} |
For the HC-05 module shown in Figure 3.2.2, connections can be made according to Figure 3.2.7, but be sure to press the button on the HC-05 before connecting the USB cable between Arduino and the computer. It is best to keep pressing this button after powering on because some Bluetooth AT commands require this to function correctly. After burning the program shown in Table 3.3.4 to the Arduino main control board, you can observe that the onboard LED light on the Arduino main control board will light up. This indicates that the Bluetooth module is successfully communicating with the Arduino based on the instructions.
Table 3.3.4 Arduino_bluetooth_hc05_set.ino |
|
1 |
void setup() { |
2 |
Serial.begin(38400); |
3 |
pinMode(13, OUTPUT); |
4 |
digitalWrite(13, LOW); |
5 |
delay(2000); |
6 |
} |
7 |
void loop() { |
8 |
Serial.print(“AT”); |
9 |
Serial.print(char(13)); |
10 |
Serial.print(char(10)); // Serial.print(“AT\r\n”); equivalent to the functionality of the previous three lines of code |
11 |
delay(100); |
12 |
while(Serial.available()>0){ |
13 |
if(char(Serial.read())==’O’) digitalWrite(13, HIGH); |
14 |
} |
15 |
} |
3.3.4 Bluetooth Module HC-05 Master-Slave Interconnection
The Bluetooth module HC-05 is a master-slave integrated module. Using two HC-05 modules, you can try pairing connections. Under the premise of connecting the corresponding Bluetooth module with the USB to TTL serial module, as shown in Table 3.3.5, set one Bluetooth module HC-05 as the master, and set the other Bluetooth module HC-05 as the slave. After setting the corresponding password and baud rate AT instructions, they will connect automatically.
Table 3.3.5 Set Bluetooth Master |
|
AT+NAME=LLKC-BTMaster |
Bluetooth master name is LLKC-BTMaster |
AT+ROLE=1 |
Bluetooth mode is master mode |
AT+CMODE=1 |
Bluetooth connection mode is any address connection mode |
AT+PSWD=LLKC |
Bluetooth pairing password is LLKC |
AT+UART=9600,0,0 |
Bluetooth communication serial baud rate is 9600 , stop bits 1 , no parity |
AT+RMAAD |
Clear pairing list |
Table 3.3.6 Set Bluetooth Slave |
|
AT+NAME=LLKC-BTSlave |
Bluetooth name is LLKC-BTSlave |
AT+ROLE=0 |
Bluetooth mode is slave mode |
AT+CMODE=1 |
Bluetooth connection mode is any address connection mode |
AT+PSWD= LLKC |
Bluetooth pairing password is LLKC |
AT+UART=9600,0,0 |
Bluetooth communication serial baud rate is 9600 , stop bits 1 , no parity |
AT+RMAAD |
Clear pairing list |
If there is no effect during the setup process of the Bluetooth master and slave, you can consider using the command AT+ORGL to restore factory settings.
During the setup process, do not input in Chinese, as this may cause errors and will not return the corresponding instructions.
After completing the setup of the Bluetooth master and slave, power off and then power on again, and the two Bluetooth modules will automatically connect and pair, with the status light showing double flashes, indicating one flash twice, once every two seconds.
Leave a Comment
Your email address will not be published. Required fields are marked *