In this tutorial we will learn how to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command. AT Command is used for that. This also explains how to activate the AT Command.
You can watch the following video or read the written tutorial below.
Overview
In some cases, we need to change the default status of HC-05 Bluetooth module. For instance, device name, module role, module password, and serial parameter number are examples of parameters that may need to be modified during the granting process. Besides that, there is much more. Here, we utilize an Arduino board, connect the HC-05 Bluetooth module to it, and make the necessary changes through AT Commands based on the serial monitor running on the Arduino IDE. Additionally, this article explains how to configure two HC-05 modules as Master and Slave using AT Commands.
Components Needed
Before we begin, make sure you have gathered all the necessary components. The purchase links are in the description of my YouTube video.
- Arduino Board (I am using Arduino UNO)
- HC-05 Bluetooth Module (Two modules are required to be configured as Master and Slave)
- Jumper Wires.
Identifying Your HC-05 Bluetooth Module and Pinout
Identifying your HC-05 Bluetooth module and understanding its pinout is crucial for successful configuration. The HC-05 module typically features a label specifying its type, such as “ZS-040” or “linvor.” Examining the module’s markings aids in determining its specific variant. The pinout consists of key pins, including VCC for power, GND for ground, TXD for transmit data, RXD for receive data, EN or Key for enabling AT mode, and STATE for indicating the module’s current state. Ensuring the correct identification and connection of these pins is essential for interfacing the HC-05 with external devices or an Arduino board. This knowledge is foundational for subsequent steps in the configuration process, enabling a seamless setup of the Bluetooth module for diverse applications.
Front view
The HC-05 Bluetooth Module has a micro push button on the front.
Here, I use only the TXD, RXD, VCC, and GND pins.
Connecting the HC-05 Bluetooth module to the Arduino board.
Connecting the HC-05 Bluetooth module to the Arduino board. re, I use only the TXD, RXD, VCC, and GND pins. Bluetooth module TXD digital pin 2 and RXD digital pin 3 are connected to make it easy to upload the code. If the Arduino RX and Tx are connected, it is not possible to upload the code while the Bluetooth module is connected.
In this way, connect the Arduino board and the Bluetooth module by jumper wires.
- Connect the VCC (power) pin of the Bluetooth module to the 5V pin on the Arduino.
- Connect the GND (ground) pin of the Bluetooth module to the GND pin on the Arduino.
- Connect the TXD pin of the Bluetooth module to digital pin D2 on the Arduino.
- Connect the RXD pin of the Bluetooth module to digital pin D3 on the Arduino.
Uploading the Arduino code
First, copy and paste the following Arduino code into the Arduino IDE. Before uploading the code, select Port and Board.
// www.robotlk.com , Robot Lk YouTube Channel
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // TX=2, RX=3 BLUETOOTH MODULE
void setup() {
Serial.begin(9600);
Serial.println("Enter AT commands:");
mySerial.begin(38400);
}
void loop()
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
Code language: PHP (php)
Set the Bluetooth module to At command mode
Once the Arduino code has been successfully uploaded, proceed to set the HC-05 Bluetooth module to AT command mode. Ensure that the Arduino board and the Bluetooth module are connected as outlined in Step 2. Disconnect the USB cable, then press the micro push button on the Bluetooth module. Reconnect the USB cable to power the system. The activation of AT command mode is indicated by the Bluetooth module’s LED flashing every two seconds. This step is crucial for initiating communication between the Arduino board and the Bluetooth module, allowing for the execution of AT Commands via the serial monitor in the Arduino IDE to customize parameters such as device name, role, password, and serial settings, among others.
When the at-command mode is activated, the LED on the Bluetooth module is flashing every two seconds
Setting up the Arduino IDE appropriately for the AT command.
To set up the Arduino IDE appropriately for AT commands, begin by opening the Serial Monitor within the Arduino IDE. This can be accomplished by clicking on the Serial Monitor button at the top right corner of the IDE or by accessing “Serial Monitor” in the “Tools” menu, with the keyboard shortcut being Ctrl+Shift+M. It is essential to adjust the baud rate of the COM port to 9600, which can be done by navigating to and selecting 9600, then clicking . Given that the Bluetooth module communicates at a baud rate of 9600, synchronize the Serial Monitor’s baud rate accordingly by choosing 9600 at the bottom right corner. Additionally, set “No Line Ending” to “Both NL & CR” beside the baud rate. Once these settings are configured accurately, the Serial Monitor is ready for AT command execution, allowing users to interact with the HC-05 Bluetooth module.
When the Serial Monitor is open, initiate the execution of AT commands by entering “AT” and pressing enter. Verify the successful execution by checking for the “OK” response in the Serial Monitor. The appearance of the “OK” message indicates that the Bluetooth module is functioning correctly with AT commands. This step serves as a crucial confirmation that the module is responsive to AT commands, paving the way for further customization and configuration of parameters to suit specific requirements in the Arduino project.
Change the baud rate of the COM port to 9600. To do this, follow this path: <This PC> <Manage> <Device Manager> <Ports> <USB Serial 340> <Properties> <Port Settings> <Bits per Second> <Select 9600> <OK>.
The Bluetooth module is currently communicating at a baud rate of 9600. Therefore, change the baud rate to 9600 at the bottom right corner of the Serial Monitor. Also, change “No Line Ending” to “Both NL & CR,” found just beside the baud rate.
If all of that is done correctly, the AT command can be executed accurately.
When the Serial Monitor is opened, it initially displays the output of entering the AT command.
First, enter “AT” and hit enter. If the message “OK” is received, the Bluetooth module will work properly with the AT command.
HC-05 Bluetooth Module – AT Command Set
Use the PDF file with complete details about AT Command from the download button below.
How to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command
Slave Bluetooth Module
To configure the HC-05 Bluetooth module as a slave, begin by connecting the module to the Arduino board following the instructions outlined in step 2. Ensure that the Bluetooth module is physically connected to the appropriate pins on the Arduino. Once connected, proceed to follow steps 4 and 5 as previously mentioned in the tutorial, which involve setting up the Arduino IDE and initiating the AT command mode. Open the Serial Monitor and enter the command “AT” to confirm the Bluetooth module’s responsiveness to AT commands.
Next, execute the following AT commands in sequence: “AT+UART=9600,0,0” and “AT+ROLE=0.” The first command sets the baud rate to 9600 and configures the module for no parity and 1 stop bit. The second command designates the Bluetooth module as a slave by setting its role to 0. Confirm the successful execution of these commands by entering “AT+ADDR?” to retrieve the Bluetooth module’s address. The obtained address, labeled as +ADDR, signifies the unique identifier of the slave Bluetooth module, crucial for subsequent pairing with the master module.
Copy the address obtained, which is typically in the format “0000:00:000000.” This address is essential for establishing a binding with the master Bluetooth module. The master Bluetooth module must recognize and communicate with the slave using this address. In this example, the address is illustrated as “0020:10:080027.” Finally, mark the Bluetooth module as a slave, ensuring that the necessary configurations have been successfully applied. This comprehensive process ensures the accurate setup of the HC-05 Bluetooth module as a slave, facilitating seamless communication within a Bluetooth network.
- AT
- AT+UART=9600,0,0
- AT+ROLE=0
- AT+ADDR?
Role = 0 means that the Bluetooth module is a slave Bluetooth module.
Copy the address of the slave Bluetooth. It should then bind to the master Bluetooth module.
+ADDR : 0000:00:000000
The address of the Bluetooth module I used is 0020:10:080027
After, mark the Bluetooth module as slave
Master Bluetooth Module
To configure the HC-05 Bluetooth module as a master, start by connecting the module to the Arduino board following the instructions in step 2. Once physically connected, proceed with the steps outlined in the tutorial, including steps 4 and 5, to set up the Arduino IDE and initiate the AT command mode. Open the Serial Monitor and confirm the Bluetooth module’s responsiveness to AT commands by entering “AT.”
Following this, execute the specified AT commands in sequence: “AT+UART=9600,0,0” and “AT+ROLE=1.” The first command sets the baud rate to 9600 and configures the module for no parity and 1 stop bit. The second command designates the Bluetooth module as a master by setting its role to 1. After setting the role, use the “AT+BIND=” command to establish a binding with the address of the slave Bluetooth module. For instance, if the address of the slave module is “12:20:450,” the binding command should be formatted as “AT+BIND=0012,20,000450.” This process ensures that the master Bluetooth module recognizes and communicates with the slave module using its unique address.
Once these steps are completed, mark the Bluetooth module as a master, confirming that the necessary configurations have been successfully applied. This comprehensive process ensures the accurate setup of the HC-05 Bluetooth module as a master, enabling effective communication and control over connected slave modules within a Bluetooth network.
- AT
- AT+UART=9600,0,0
- AT+ROLE=1
- AT+BIND=Copy to Slave Bluetooth Module address
Role = 1 means that the Bluetooth module is a Master Bluetooth module.
If the address of the slave module 12:20:450 is like this, you should bind it like this
AT+BIND=0012,20,000450
mark the Bluetooth module as Master.
How to check if the master and slave Bluetooth module bind to each other.
To check if the master and slave Bluetooth modules have successfully bound to each other, provide 5V power to the VCC pins of both modules and connect the GND pins to the Arduino GND pin. Simultaneously power both modules and if the binding process is successful, the LEDs on both the master and slave Bluetooth modules will start flashing simultaneously after a short delay. This synchronous flashing indicates that the two Bluetooth modules have established a connection and are effectively communicating with each other. This visual confirmation ensures that the master and slave modules are bound and ready for seamless communication within the Bluetooth network, confirming the successful configuration of the HC-05 modules for their intended roles.
If this is properly bound, when both the master and slave Bluetooth modules are powered at the same time, after some time the LEDs of the two Bluetooth modules will flashing simultaneously.
Conclusion
In conclusion, this tutorial provides a comprehensive guide on configuring the HC-05 Bluetooth module as both a master and a slave using AT commands. By connecting the Bluetooth modules to an Arduino board and executing specific AT commands through the Arduino IDE’s Serial Monitor, users can customize various parameters such as baud rates, device roles, and addresses. The process involves careful wiring, uploading Arduino code, activating AT command mode, and executing specific AT commands for master and slave configurations. The tutorial emphasizes the significance of checking successful bindings between master and slave modules by observing synchronized LED flashing. Overall, this guide equips users with the knowledge and steps needed to effectively set up and configure HC-05 Bluetooth modules for versatile applications in Arduino projects.