How To Configure and Pair Two HC
How To Configure and Pair Two HC
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.
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.
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.
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.
You can get the components needed for this Arduino tutorial from any
of the sites below:
HC-05 Bluetooth Module
……………. Amazon / Banggood / AliExpress
Arduino Board
…………………………… Amazon / Banggood / AliExpress
Servo Motor…………………………….
…. Amazon / Banggood / AliExpress
Potentiometer……………..………..……. Amazon / Banggood /
AliExpress
3x 220 Ohms
resistors………………… Amazon / Banggood / AliExpress
Breadboard and Jump Wires
………. Amazon / Banggood / AliExpress
Disclosure: These are affiliate links. As an Amazon Associate I earn from
qualifying purchases.
Master Code:
/*
* == MASTER CODE ==
*/
#define ledPin 9
int state = 0;
int potValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
if (state == '1') {
state = 0;
state = 0;
potValue = analogRead(A0);
delay(10);
/*
* == SLAVE CODE ==
*/
#include <Servo.h>
#define button 8
Servo myServo;
int buttonState = 0;
void setup() {
pinMode(button, INPUT);
myServo.attach(9);
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
myServo.write(state);
delay(10);
buttonState = digitalRead(button);
if (buttonState == HIGH) {
else {
Serial.write('0');
Dejan. (2016, April 15). How To Configure and Pair Two HC-05 Bluetooth Modules as
Master and Slave | AT Commands. HowToMechatronics.
https://howtomechatronics.com/tutorials/arduino/how-to-configure-pair-two-hc-05-
bluetooth-module-master-slave-commands/