How To Configure and Pair Two HC-05 Bluetooth Modules as Master and Slave | AT Commands

In this Arduino Tutorial we will learn how to configure and pair two HC-05 Bluetooth Modules as Master and Slave devices.You can watch the following video or read the written tutorial below.

Overview 


In my previous two tutorials we already learned how to connect the HC-05 Bluetooth Module to the Arduino and make a communication between an Android smartphone and the Arduino. In those tutorials we used the HC-05 Bluetooth module with its default configuration, as a slave device.

Configuring the HC-05 Bluetooth Module – AT Commands


For this tutorial we need to configure both modules. In order to do that we need to switch to AT Command Mode and here’s how we will do that. First we need connect the Bluetooth module to the Arduino as the circuit schematics explained in the previous tutorials. What we need to do additionally is to connect the “EN” pin of the Bluetooth module to 5 volts and also switch the TX and RX pins at the Arduino Board.

How To Configure and Pair Two HC-05 Bluetooth Module as Master and Slave AT Commands

So the RX pin of the Arduino needs to be connected to the RX pin of the Bluetooth module, through the voltage divider, and the TX pin of the Arduino to the TX pin of the Bluetooth module. Now while holding the small button over the “EN” pin we need to power the module and that’s how we will enter the command mode. If the Bluetooth module led is flashing every 2 seconds that means that we have successfully entered in the AT command mode.

After this we need to upload an empty sketch to the Arduino but don’t forget to disconnect the RX and TX lines while uploading. Then we need to run the Serial Monitor and there select “Both NL and CR”, as well as, “38400 baud” rate which is the default baud rate of the Bluetooth module. Now we are ready to send commands and their format is as following.

AT Commands Format

All commands start with “AT”, followed by the “+” sign, then a <Parameter Name> and they end either with the “?” sign which returns the current value of the parameter or the “=” sign when we want to enter a new value for that parameter.

Slave Configuration


So for example, if we type just “AT” which is a test command we should get back the message “OK”. Then if we type “AT+UART?” we should get back the massage that shows the default baud rate which is 38400. Then if we type “AT+ROLE?” we will get back a massage “+ROLE=0” which means that the Bluetooth device is in slave mode. If we type “AT+ADDR?” we will get back the address of the Bluetooth module and it should looks something like this: 98d3:34:905d3f.

Slave Configuration HC-05 Bluetooth Module Arduino

Now we need to write down this address as we will need it when configuring the master device. Actually that’s all we need when configuring the slave device, to get its address, although we can change many different parameters like its name, baud rate, pairing password and so on, but we won’t do that for this example.

Master Configuration


Ok now let’s move on and configure the other Bluetooth module as a master device. First we will check the baud rate to make sure it’s the same 38400 as the slave device. Then by typing “AT+ROLE=1” we will set the Bluetooth module as a master device. After this using the “AT+CMODE=0” we will set the connect mode to “fixed address” and using the “AT+BIND=” command we will set the address of the slave device that we previously wrote down.

Master Configuration HC-05 Bluetooth Module Arduino

Note here that when writing the address we need to use commas instead of colons. Also note that we could have skipped the previous step if we entered “1” instead of “0” at the “AT+CMODE” command, which makes the master to connect to any device in its transmission range but that’s less secure configuration. Here you can find a complete list of commands and parameters: HC-05 AT Commands List

Nevertheless, that’s all we need for a basic configuration of the Bluetooth modules to work as a master and slave devices and now if we reconnect them in normal, data mode, and re-power the modules, in a matter of seconds the master will connect to the slave. Both modules will start flashing every 2 seconds indicating a successful connection.


Communcation Between Two HC-05 Bluetooth Module Example


Ok so now we are ready make the practical example for this tutorial. Here’s the circuit schematics. We will use a potentiometer, at the master, to control a servo motor at the slave. And vice versa, we will use a push button, at the slave, to control a LED at the master.

Communcation Between Two HC-05 Bluetooth Module Circuit Schematics

You can get the components needed for this Arduino tutorial from any of the sites below:

*Please note: These are affiliate links. I may make a commission if you buy the components through these links.
I would appreciate your support in this way!

Arduino Source Codes


Description: So first we need to define the pins and some variables needed for the program. In the setup section, at the master, we set the LED pin as output and set it low right away, as well as, start the serial communication at 38400 baud rate. Similar, at the slave, we set the button pin as input, define the servo to which pin is connected and start the serial communication with the same baud rate.

In the loop section, in both code, with the Serial.available() function we will check whether there is available data in the serial port to be read and using the Serial.read() function we will read and store the data into the “state” variable. So if the master receive the character ‘1’ which is sent from the slave when the button state is high, or the button is pressed, the LED will be on. Else if the character is ‘0’ the LED will be off.

As for the servo motor control, first at the master, we read the potentiometer value and map it into a suitable range for the servo from 0 to 255. This value is sent to the slave which use it to rotate the servo motor accordingly. That’s all we need and here’s the demonstration of the example.

Master Code:

/*
 * How to configure and pair two HC-05 Bluetooth Modules
 * by Dejan Nedelkovski, www.HowToMechatronics.com
 * 
 *                 == MASTER CODE ==
 */

#define ledPin 9

int state = 0;
int potValue = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
 if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
 }
 // Controlling the LED
 if (state == '1') {
  digitalWrite(ledPin, HIGH); // LED ON
  state = 0;
 }
 else if (state == '0') {
  digitalWrite(ledPin, LOW); // LED ON
  state = 0;
 }
 // Reading the potentiometer
 potValue = analogRead(A0);
 int potValueMapped = map(potValue, 0, 1023, 0, 255);
 Serial.write(potValueMapped); // Sends potValue to servo motor
 delay(10);
}

Slave Code:

/*
 * How to configure and pair two HC-05 Bluetooth Modules
 * by Dejan Nedelkovski, www.HowToMechatronics.com
 * 
 *                 == SLAVE CODE ==
 */
 
#include <Servo.h>
#define button 8

Servo myServo;
int state = 20;
int buttonState = 0;

void setup() {
  pinMode(button, INPUT);
  myServo.attach(9);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
 if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
 }
 // Controlling the servo motor
 myServo.write(state);
 delay(10);
 
 // Reading the button
 buttonState = digitalRead(button);
 if (buttonState == HIGH) {
   Serial.write('1'); // Sends '1' to the master to turn on LED
 }
 else {
   Serial.write('0');
 }  
}

That’s all and if you have any problems, feel free to ask for help in the comments section below.

31 Responses

  1. Stephen

    Hi I was wondering if you could help me, I am trying to do something really simple with HC-05 but it has become very difficult. I want to send AT commands to the HC-05 from a running Uno sketch. I have HC-05 pins #32 & #34 connected to the Uno so I can control the HC-05 power and AT-state by the Uno. Now I use the Uno to change the state of the HC-05 to AT Command mode, and then send a command by serial.print to the HC-05. But, the HC-05 does not react or respond. I am not sure if not responding, or if I can’t read the response. I am reading the response (debugging) by passing it to the serial monitor. Could you possibly look at my code to help me make this work? Thanks.

    Reply
    • Dejan Nedelkovski

      Sorry but cannot say anything about this because I have’t tried making such a communication and I don’t have such a code. And sorry but I cannot look or do custom codes when someone ask that from me because it takes quite a time and have a lot other work to do.

      Reply
      • Stephen

        Ok, how about any information as how to set up a sleep mode on the hc-05, I think there is the Sniff command but I havent found any documentation for it.

        Thanks

  2. Justin Straver

    Tnx Dejan, Great tutorial! For me it worked without the voltage dividers and at a baudrate of 9600.

    Reply
  3. James

    Hello,

    GREAT job on this write up!! Fantastic!

    I wanted to know, can a bluetooth phone connect to the master if the master HC-05 is already paired with the other HC-05?

    Reply
  4. John Tan Chin Yen

    How about if I use a HC-05 master and control a HC-6 slave?

    Reply
  5. Tehzib Sadaf

    can you tell me the Bluetooth range of this project? i mean from how far away i will be able to command the servo with the POT?

    Reply
  6. Michal

    Hi, first of all great tutorial :). I have one question. Is it possible to connect for example 1 mster and 5 slaves? If yes how to do that? Should I just write couple of adresses in AT+BIND ?

    Reply
  7. noman yousaf

    hi
    I just started searching the way to use the bluetooth module 2 days before. but when i watched your tutorial video, i learnt almost every thing, i wanted to know, just in few minutes.
    this is great and very easy to learn tutorial boss.
    i have some questions.
    if i configure 1 HC-05 as master and other HC-05 as slave on 9600 baud rate, can i use both for simple digital data communication by using both with 2 separate MCUs ?
    will these both work full duplex mode ? means if master send any data string to slave so can slave also send any data back to master?
    Thanks to help me in advance

    Reply
      • noman yousaf

        thanks sir
        please guide if i configure 1 HC05 as master and other as slave by AT commands and set 9600 buadrate then leave the key sw open circuited. then both connect to normal MCUs like pic or avr or 8951, will these communicate to each other?

  8. William

    How to control two Servo two directions?
    With two HC-05 & arduino.
    Because this is only one Servo & one directions.

    I don’t know how to control two Servo.
    It bothers me >_<

    Reply
  9. Carlos

    Hello Dejan, excellent tutorial, I did it using a hc 05 as master and slave as hc 06, and without the voltage dividers at a baud rate of 9600. Now I want to do this with two servos and two pots. it’s possible?. Could you help me with the code?

    Reply
  10. Mathew Rawson

    Hi i keep getting an error 7 when binding to slave to the master and can’t find how to fix it.

    Reply
  11. Snehal

    Thankyou!
    I am having one problem though.
    When I pass data from one Arduino to another using two HC05’s, I am getting a two second delay. When I vary the POT, after two seconds(approx), the servo motor rotates.

    How should i decrease this delay?

    Reply
      • Snehal

        Well, I just modified the receiver code a little. I declared ‘state’ as String, since it is taking value from Serial.read(), and hence it will be in String. I then used toInt() function to convert it into an integer value.

  12. Luis

    Thank that helpe me a lot, In the college I have to do a PICONET and this example help me.

    Reply

Leave a Reply

Your email address will not be published.