How to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command

140K5639

Intro: How to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command

This tutorial is going to teach you some basics on configuring two HC-05 Bluetooth Modules as Master and Slave respectively and also a simple tutorial to see how each HC-05 controls LEDs wirelessly.

STEP 1: Introduction

Description:

HC-05 Bluetooth Module is an easy-to-use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. Its communication is via serial communication which makes an easy way to interface with controller or PC. HC-05 Bluetooth module provides switching mode between master and slave mode which means it able to use neither receiving nor transmitting data.

Specifications:

  • Model: HC-05
  • Input Voltage: 5VDC
  • Communication Method: Serial Communication
  • can switch between master mode and slave mode
  • Transmission Distance: 20m ~ 30m in free space

STEP 2: Pin Definition

STEP 3: To Configure and Pair Two HC-05 As Master and Slave (Step 1)

Components that you need:

  1. One Arduino board... ( In this tutorial, we use Arduino Uno)
  2. Two Bluetooth Module HC-05
  3. A bunch of jumper wires
  4. USB Cable A-B Type
  5. Breadboard

STEP 4: To Configure and Pair Two HC-05 As Master and Slave (Step 2)

To configure HC-05, we will have to enter AT mode. But, how to do that? Let's check the steps below.

1. Firstly, connect the components as shown in the diagram above. From the diagram, the VCC of HC-05 is connected to the 3.3V of the Arduino Uno. If the VCC of HC-05 is connected directly to 5V of Arduino Uno, there is a high possibility that HC-05 will be damaged. But still if you insist to do so, DO IT AT YOUR OWN RISK.

2. Next, before connecting the Arduino board to the USB cable, REMOVE THE VCC WIRE FROM THE HC-05. Only after you have removed the VCC wire, connect the USB to the Arduino board.

3. Download the sample source code (file name: AT mode) attached below and upload it into your Arduino board.

4. After the uploading process is complete, then reconnect the VCC wire back to your HC-05.

5. Now you will see the LED on the HC-05 bluetooth module is blinking for every 2 seconds interval. This indicates that the bluetooth module has entered AT mode.

6. To give AT commands, open your Serial Monitor. For about 1 second, a sentence saying "Enter AT commands:" will pop up. But before that, remember to change "No Line Ending" to "Both NL & CR" and also fix the baud rate at 9600. You can refer to the diagram attached above.

7. To see whether everything is okay or not, enter "AT" and send. An "OK" will pop up on the Serial Monitor and this implies that no problems occur.

STEP 5: To Configure and Pair Two HC-05 As Master and Slave (Step 3)

Slave Configuration:

The required AT commands to set the configuration

  • AT+RMAAD (To clear any paired devices)
  • AT+ROLE=0 (To set it as slave)
  • AT+ADDR (To get the address of this HC-05, remember to jot the address down as it will be used during master configuration)
  • AT+UART=38400,0,0 (To fix the baud rate at 38400)

If the commands entered are replied with "OK" then it indicates that the settings mentioned have been customized.

You may also try out other AT commands such as how to change the name, the password, and so on. A full set of AT commands is attached below and kindly download it for your own use. Note that the password for both master and slave must be the same otherwise pairing will not be done.

STEP 6: To Configure and Pair Two HC-05 As Master and Slave (Step 4)

Master Configuration:

The required AT commands to set the configuration:

  • AT+RMAAD (To clear any paired devices)
  • AT+ROLE=1 (To set it as master)
  • AT+CMODE=0 (To connect the module to the specified Bluetooth address and this Bluetooth address can be specified by the binding command)
  • AT+BIND=xxxx,xx,xxxxxx (Now, type AT+BIND=98d3,34,906554 obviously with your respective address to the slave. Note the commas instead of colons given by the slave module.
  • AT+UART=38400,0,0 (To fix the baud rate at 38400)

If the commands entered are replied with "OK" then it indicates that the settings mentioned have been customized.

You may also try out other AT commands such as how to change the name, the password, and so on. A full set of AT commands is attached below and kindly download it for your own use. Note that the password for both master and slave must be the same otherwise pairing will not be done.

STEP 7: Sample Communication Between Master and Slave (Step 1)

With the two HC-05 configured earlier just now, a simple example can be carried out to see how each of them functions. In this example, both master and slave will be controlling LEDs by using buttons. Button A at master will be controlling LED3 and LED4 at slave whereas Button B at slave will control LED1 and LED2 at master. Now let's get started.

Components that you need:

  1. Two bluetooth modules, HC-05
  2. Two Arduino boards (In this example, we use Arduino Uno)
  3. A bunch of jumper wires
  4. Two push buttons (In this example, a two-pin push button is used)
  5. Four LEDs (What colour it emits does not matter)
  6. Two USB cable A-B type
  7. Two resistor of 220Ω
  8. Breadboard

STEP 8: Sample Communication Between Master and Slave (Step 2)

*Note that the above sample hardware installation is applicable to both master HC-05 and slave HC-05.

STEP 9: Sample Communication Between Master and Slave (Step 3)

Kindly download the sample source code attached below and upload them into Arduino.

this_is_master must be uploaded into the Arduino connected to master HC-05 while this_is_slave must be uploaded into the Arduino connected to slave HC-05.

STEP 10: Result


The result is displayed in video form. You can see how master and slave interact with each other in this video. Have FUN~

22 Comments

Enterring AT commands. Using the at_mode Sketch. In Serial monitor I keyin the command (e.g., AT+RMAAD) for the "Master" board. After keying in the command I press CTRL+ENTER. The master unit / sketch does not give me any reply. I readily admit that this is my first try at commanding/connecting the master/slave.

The same problem occurs with the SLAVE unit.
Tom Kane / Vancouver, BC, Canada
I want to do a project with 2 diffrent device which data is to receive in microcontroller with the help of 2 slaves and one master but i don't know how to identify the which device which data is received?
hello, i am not getting an ok answer when entering the AT+RMAAD an also the AT+BIND is not resulting to an OK answer, what should i do? thank you
To get the address of the Slave HC05, AT+ADDR needs to be typed with a question mark:
AT+ADDR?
I'm trying to pair 2 HC-05 modules but when I type in the serial monitor text bar, and press return nothing comes up. what should I do?
I dont know how much you know, or how you have your HC05s configured. You need to set everything up step by step. Make sure you have 2 HC05s. Both will need pin 34 to be brought high to enter at commands. Thats how you communicate in serial monitor. If you notice there is a(1) serial communication and a (2) BTserial communication.. in you arduino code the speeds can/may be different. I just slid a bread board wire in top of module between plastic to top pin 34, and with a voltage divider to 5v. (Be sure to label both 05s..)
If you properly connect 05 to arduino, pin 34 high, on a true 05 it will enter At mode. Remember At mode is different then communication mode. Light will blink slow in at mode, fast in communication, and i believe its solid when connected. Good luck!
I only figured this out yesterday. The tutorial i was following had 1 05, and for a slave they had a 06. It took a long time to realize that the 06 must enter at mode when powered on, because i could not get a ok back from my 05. I read many other forums , never found the answer but with everything i read it was enough to make me understand what was happening. Sometimes the tutorial is slightly different, attention to details is Key
bonjour à vous!!! je vous remercie pour ce tutoriel, je l'ai essayé et il fonctionne. cependant j'ai voulu inserer à l'intérieur du code esclave un temporisateur afin de permettre à ma led de prendre quelques seconde avant de s'eteindre mais il ne fonctionne pas
pouvez vous m'eclaircir la dessus? Merci...
I am new to this, sorry. I have just ordered 2 HC05s for a project. When you set the HC05 up as either master or slave and set up the specific pairing codes, does it stay that way when power is removed and restored?
I entered slave loop (loopback mode or AT+ROLE=2), and i cant seem to leave it! anything i input is just echoed back! I tried AT+RESET and AT+ORGL, nothing works :(, Please help!

Hi,

I wanted to send data from a GPS receiver to my android phone using sh-hc-08. Would that be possible?

It's easy. Your phone is master and HC05 is slave. To setup connection is very easy in Android devices... generally password is 1234 or 0000
After configuring master and slave, your circuit shows connection between Key pin and Arduino<9> port. Your Arduino code, don't use Arduino<9> port.
Is it required that connection?

I just looked at the master and slave code side by side and they seem identical except for the "Master" names:

const int ledPin = 8;
const int buttonPin = 2;

and, the slave names:

const int led = 8;
const int button = 2;

Am I missing something? Can the same .INO be used on both HC-05 modules?

Second paragraph of article says: "... switching mode between master and slave mode which means it able to use neither receiving nor transmitting data." Why would anyone want a module which allows neither receiving NOR transmitting?

Yes, the words as written mean it doesn't do either.
Superb!
Thanks for this simple and easy totorial
How to pair hc05 to mobile, it is askingme pin definition , what is pin definition
More Comments