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

84,531

47

15

Introduction: 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~

1 Person Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

15 Discussions

0
venegre
venegre

Question 5 weeks ago on Step 10

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...

0
rwellsz28
rwellsz28

6 months ago

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?

0
Sultannmh
Sultannmh

Question 7 months ago

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!

0
Minion08
Minion08

2 years ago

Hi,

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

0
aprada661
aprada661

Reply 8 months ago

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

0
aprada661
aprada661

8 months ago

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?

0
judas79
judas79

3 years ago

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?

0
aprada661
aprada661

Reply 8 months ago

You are right!

3
malexead
malexead

3 years ago

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?

0
richarddl42
richarddl42

Reply 8 months ago

Yes, the words as written mean it doesn't do either.

0
ABAVI4
ABAVI4

1 year ago

Superb!
Thanks for this simple and easy totorial

0
Arks rakesh
Arks rakesh

Tip 1 year ago on Introduction

How to pair hc05 to mobile, it is askingme pin definition , what is pin definition

0
RobotRodsR
RobotRodsR

3 years ago

Just the tutorial I was looking for.

0
B n
B n

3 years ago

hello

i trata with this project but no response?