Site icon AranaCorp

Arduino and Bluetooth module HC-05

4.5
(2)

Arduino can communicate with other devices via Bluetooth using the module HC-05 (master/slave). It enables the Arduino to connect and exchange data with other devices such as Smartphone, computer or other microcontrollers.Bluetooth communication can be used to control a robot remotely, Display and store data on your computer or on your smartphone, for instance.

Prerequisite: Arduino Serial communication

Material

Module HC-05 overview

The Bluetooth module HC-05 has 6 pins, 2 for power, 2 to establish connection, 1 to enter configuration mode and the other to know the connection state.

The particularity of the HC-05 is that it can be used as a slave module (equivalent to HC-06) and as a master module which means that it can pair itself to another device on its own.

Schematics for configuration


WARNING :

Configuration of the module HC-05

Configuring the module HC-05 can be interesting to verify that it is working, hooked up correctly and to modify its parameters such as its name (useful when your are using several modules), PIN code and communication speed (baudrate). Especially if you want to use it as a master module. To allow configuration, the module HC-05 should be powered but not paired and the KEY/EN pin should be set to HIGH before module is switched on. When in configuration mode the LED will blink two seconds every two seconds.

Open the Serial monitor and select the end line option “New line” and a baudrate of 9600.

Note that HC-05 enter configuration mode with a baudrate of 38400bps.

Configuration code

The following code allows you to modify and check the parameters using the serial monitor and AT commands.

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
#define baudrate 38400

String msg;

SoftwareSerial hc05(rxPin ,txPin);

void setup(){
  pinMode(rxPin,INPUT);
  pinMode(txPin,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("ENTER AT Commands:");
  hc05.begin(baudrate);
}

void loop(){
    readSerialPort();
    if(msg!="") hc05.println(msg);
    
    if (hc05.available()>0){
      Serial.write(hc05.read());
    }
}

void readSerialPort(){
  msg="";
 while (Serial.available()) {
   delay(10);  
   if (Serial.available() >0) {
     char c = Serial.read();  //gets one byte from serial buffer
     msg += c; //makes the string readString
   }
 }
}

AT Commands

Compared to the module HC-06, the HC-05 can be used as a master module and you can also check the saved parameters using the AT commands.

In general, typing the command AT+<command>? will prompt the saved parameter (ex: AT+PSWD? will display the module PIN code). If you enter AT+<command>=<Param>, you can set the parameter value(ex: AT+PWSD=0000 to modify the PIN code to 0000).

Here are some of the AT commands:

Other AT commands exist for the Bluetooth module HC-05 that you can find following the link.

CAUTION: Different versions of the module HC-05 exist and the configuration procedure may vary. Be sure to check the module label and firmware version before use. For this tutorial, we used a module HC-05 labelled ZS-040 and firmware version 2.0-20100601.

N.B.: If module is not responding check wiring, cables conductivity and baudrate.

Do not hesitate to leave a comment or to contact us if you have issues configuring your HC-05 module.

Slave Configuration

To set the module as a slave, you can change the name as AT+NAME=HC05-Slave and choose the communication parameters and the PIN code that you want. You’ll need to make sure that master and slave as the same communication parameters.

For the slave module, you can also use a HC-06 module

Master Configuration

To set the module as master, you need to change the role and set the same communication parameter as the slave module.

Schematics for communication

In communication mode, the Key/EN pin is not connected.

Communication Code

When both modules are configured, upload the following codes in to Arduino boards connected to your modules. Switch on the modules to enter communication mode. The master module should connect to the slave module and the LED should blink every second.

We use the same library SoftwareSerial.h as for the HC-06 module:

Master Code

#include <SoftwareSerial.h>
SoftwareSerial ArduinoSlave(2,3);
String answer;
String msg;
void setup(){
Serial.begin(9600);
Serial.println("ENTER Commands:");
ArduinoSlave.begin(9600);
}
void loop(){
//Read command from monitor
readSerialPort();
//Read answer from slave
while (ArduinoSlave.available()) {
delay(10);  
if (ArduinoSlave.available() >0) {
char c = ArduinoSlave.read();  //gets one byte from serial buffer
answer += c; //makes the string readString
}
}
//Send data to slave
if(msg!=""){
Serial.print("Master sent : ");
Serial.println(msg);
ArduinoSlave.print(msg);
msg="";
}
//Send answer to monitor
if(answer!=""){
Serial.print("Slave recieved : ");
Serial.println(answer);
answer="";
}
}
void readSerialPort(){
while (Serial.available()) {
delay(10);  
if (Serial.available() >0) {
char c = Serial.read();  //gets one byte from serial buffer
msg += c; //makes the string readString
}
}
Serial.flush();
}

Slave Code

#include <SoftwareSerial.h>
SoftwareSerial ArduinoMaster(2,3);
String msg;
void setup(){
Serial.begin(9600);
ArduinoMaster.begin(9600);    
}
void loop(){
readSerialPort();
// Send answer to master
if(msg!=""){
Serial.print("Master sent : " );
Serial.println(msg);
ArduinoMaster.print(msg);
msg=""; 
}
}
void readSerialPort(){
while (ArduinoMaster.available()) {
delay(10); 
if (ArduinoMaster.available() >0) {
char c = ArduinoMaster.read();  //gets one byte from serial buffer
msg += c; //makes the string readString
}
}
ArduinoMaster.flush();
}

If you open both serial monitors and if you enter a command in the master board monitor, you should see the following results. The master board send a command which is recieved and sent back by the slave board. The communications works correctluy in both directions.

If you have issues configuring or pairing your Bluetooth modules, do not hesitate to contact us or to leave a comment.

Application

Source

Find other examples and tutorials in our Automatic code generator
Code Architect

How useful was this post?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

Exit mobile version