Pair two HC-05 Bluetooth modules and share data between them

From the previous project, you have already learned how to configure the control parameters of a Bluetooth Module. In this project, it will be demonstrated how two Bluetooth modules can be paired and allow data sharing between them. To pair two Bluetooth modules, one of them needs to be configured for the Master function and the other for the Slave function. Furthermore, both modules must be in request response working mode so that AT commands can be passed to them. The baud rate of the two modules also needs to be the same, and SPP and INQ must be started on both modules. To start sharing data between modules, both modules must have the communication mode enabled by configuring the CMODE parameter set to 1.
In this project, two Bluetooth modules are used. The Bluetooth modules interface with different Arduino Pro Mini boards that are connected to different computers running the Arduino Serial Monitor application. Any hyperterminal application can be used to pass AT commands and data to be shared between modules. The Arduino sketch used in this project is designed to exchange commands and data seamlessly. Arduino code is written in the Arduino IDE and written to boards using the AVR Dude.

Required components –

1. Arduino Pro Mini – 2
2. HC-05 Bluetooth Module – 2
3. Desktop or laptop computer – 2
4. USB cable – 2

Required software tools –

1. ArduinoIDE
2. Any Hyper Terminal Desktop application such as Arduino Serial Monitor

Circuit Connections –

Protótipo de emparelhamento Bluetooth baseado em Arduino
Figure 1: Arduino-based Bluetooth pairing prototype
Arduino Pro Mini boards manage the exchange of data between the desktop computer's serial application and the respective Bluetooth module. The circuit on both master and slave sides is assembled in the same manner as follows –
Power supply – The circuit is powered by the battery that directly powers the Arduino board and the Bluetooth module.
HC-05 Bluetooth Module – The HC-05 Bluetooth module is a serial port protocol module. Operates in the 2.4 GHz ISM band with V2.0 + EDR (Enhanced Data Rate). It can work in Master and Slave modes. The Bluetooth module has six pins – Enable, VCC, Ground, Transmit Data (TxD), Receive Data (RxD) and State. The Enable and State pins are unused and therefore not connected to the circuit. The VCC and Ground pins are connected to common VCC and Ground. The TxD and RxD pins of the module are connected to pins 3 and 2 of the Arduino Pro Mini respectively. These connections are summarized in the table below –
Tabela listando conexões de circuito entre o módulo Bluetooth HC-05 e o Arduino Pro Mini
Fig. 2: Table listing the circuit connections between the HC-05 Bluetooth module and the Arduino Pro Mini
In addition to these connections, the 34th pin of the module is connected to pin 9 of the Arduino Pro Mini.
Desktop Computer – The computer is connected to the Arduino board via a USB cable and transfers serial data to the board via virtual serial communication using a hyperterminal application.

How the project works –

Both modules receive AT commands entered by the hyperterminal application user on their respective computers. Commands are read serially via virtual serial communication and passed to Bluetooth modules. The Bluetooth modules respond to AT commands and the responses are again read serially and transferred to the desktop application.
First, one of the modules needs to be configured for Master role for which the following steps need to be performed –
Step 1: Enter AT command mode.
Step 2: Start SPP.
Step 3: Check available devices.
Step 4: If the address is available in the list, pair it.
Step 5: After pairing, establish a communication link.
Step 6: Submit data to share.
Imagem do emparelhamento Bluetooth baseado em Arduino
Fig. 3: Image of Arduino-based Bluetooth pairing
These steps can be implemented by running the AT commands and changing the following configuration parameters –
Tabela listando comandos AT do Módulo Bluetooth HC-05
Fig. 4: Table listing AT commands of the HC-05 Bluetooth Module
The other module needs to be configured as a slave. To configure the other module as a slave, the following steps need to be performed –
Step 1: Enter AT command mode.
Step 2: Start SPP.
Step 3: Check available devices.
Step 4: Check paired status or not.
Step 5: After establishing the communication link Receive the data from the master and if necessary, send the data to share.
Imagem do módulo Bluetooth escravo baseado em Arduino
Fig. 5: Image of the Arduino-based Bluetooth Slave Module
These steps can be implemented by running the AT commands and changing the following configuration parameters –
Tabela listando comandos AT e seus parâmetros de configuração
Fig. 6: Table listing AT commands and their configuration parameters
Therefore, to pair the two modules, the following configuration parameters must be changed and noted.
– Both modules must be in AT command mode.
– The name, password and address of the modules must be uniquely defined and well known. The baud rate of both modules must be the same, set to 9600, 0, 0 in the project demo.
– SPP mode must be initialized on both modules using the AT+STATE command.
– One of the modules must be defined as master and the other must be defined as slave. To configure a module for master role, AT+ROLE must be set to 1 in one of the modules. The default function a is a slave with a value of 0 that does not need to be changed in the other module.
– Communication mode must be enabled on both modules by setting CMODE to 1.
– In the master module, the query mode must be set to 0, 5, 5, where the first parameter is the query access mode which, if set to 0, configures the default_query mode and if set to 1, configures the query_rssi mode, the second parameter is the number of Bluetooth device response allowed, and the third parameter is the maximum limited query time in seconds.
– The module configured as a slave must be free to which the RMAAD command must be passed. Now the slave module will be free to pair with the master module.
– The master module must be configured to query the devices available for pairing by configuring the query mode and if the slave address is shown as available, the slave module name must be checked by the RNAM command.
– If the name of the available slave device is the same as the configured slave module, it can be paired by sending the command AT+PAIR= . After successful pairing with the slave module, the master module will respond with OK. Can the pairing status be checked by passing AT+STATE? command in both modules.
– Can the connection between modules be defined by passing AT+LINK? command.
– If both modules send an OK response, the modules are ready to share data.
– If the two modules are not paired, the initialization of SPP and INQ on the slave may have been neglected or the slave module may not be in AT mode. The baud rate of both modules may not be the same or a wrong PIN may have been entered. There may also be a possibility that the slave module was not configured for the slave function and set the function parameter to 1 by mistake. These settings should have been checked before attempting to pair the devices.
So configure both modules with proper settings, start pairing and try to share some data.

Programming guide –

The Arduino sketch manages data communication between the computer and the Bluetooth module. After pairing, the Bluetooth modules will be able to share data. On the master device, to enable virtual serial communication, SoftwareSerial.h needs to be imported. A Software serial object is instantiated and mapped to pins 2 and 3 of the Arduino. A variable to store the data to be shared is declared.
#include 0
SoftwareSerial BTserial(2, 3); //RX Texas
character c = ' ';
The setup function is called and the baud rate for communicating with the PC is set to 9600 bits per second. Some initial messages are sent serially to the hyperterminal application and the software serial port baud rate is set to 38,400 bits per second for communication with the Bluetooth module.
The loop function is called in which data from the slave module is checked for availability and, if available, is read from and written to the serial port. Similarly, if any computer data is checked for availability and if available, it will be stored in the variable and passed to the Master module for transmission.
This completes the Arduino Sketch for the Master side.
On the slave side, again SoftwareSerial.h needs to be imported. A Software serial object is instantiated and mapped to pins 2 and 3 of the Arduino.
The setup function is called and the baud rate for communicating with the PC is set to 9600 bits per second. Some initial messages are sent serially to the hyperterminal application and the software serial port baud rate is set to 38,400 bits per second for communication with the Bluetooth module.
The loop function is called in which data from the master module is checked for availability and, if available, is read from and written to the serial port. Similarly, if any data from the computer is checked for availability and if available, is passed to the slave module for transmission.
This completes the Arduino Sketch for the Slave side. You can get the complete programming details in the code section.

Project source code

###

 //Program to
 #include 0

 SoftwareSerial BTserial(2, 3); // RX TX

 char c=" ";

 void setup

 {

 Serial.begin(9600);

 Serial.println("__________Engineers Garage______________");
 
Serial.println("--------------Master------------------");

 // HC-05 default serial speed for AT mode is 38400

 BTserial.begin(38400);

 }

 void loop

 {

 // Keep reading from HC-05 and send to Arduino Serial Monitor

 if (BTserial.available )

 {

 c = BTserial.read;

 Serial.write(c);

 }

 // Keep reading from Arduino Serial Monitor and send to HC-05

 if (Serial.available )

 {

 c = Serial.read;

 BTserial.write(c);

 }

 }

 #include

 SoftwareSerial BTSerial(2, 3); // RX TX

 void setup

 {

 Serial.begin(9600);

 Serial.println("__________Engineers Garage______________");

 Serial.println("--------------Slave------------------");

 // HC-05 default serial speed for AT mode is 38400

 BTserial.begin(38400);

 }

 void loop

 {

 // Keep reading from HC-06 and send to Arduino Serial Monitor

 if (BTSerial.available)

 Serial.write(BTSerial.read);

 // Keep reading from Arduino Serial Monitor and send to HC-06

 if (Serial.available )

 BTSerial.write(Serial.read );

 }

###

Project video

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.