Tags: , , , ,

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

  • Computer
  • Arduino UNO
  • USB cable
  • Bluetooth module HC-05 ZS-040
  • Dupont cables M/F

 

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.

  • VCC power supply. Typically hooked up to 5V pin of the Arduino.
  • GND ground. Typically hooked up to GND pin of the Arduino
  • RX reception pin. Typically hooked up to transmission pin (TX) of the Arduino
  • TX transmission pin. Typically hooked up to reception pin (RX) of the Arduino
  • State is HIGH when the module is connected
  • Key ou EN must be set to HIGH (3.3V) to enter configuration mode and must be disconnected to enter communication mode.

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 :

  • We use pin 2 and 3 of Arduino Uno. Depending on the microcontroller, some pins may not support serial communication. Especially, Arduino Mega, Micro and Leonardo. Check the documentation.
  • Since the Bluetooth module is using 3.3V logic, it is advised to add a voltage divider to reduce the 5V voltage from Arduino.

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:

  • To test communication,enter AT in the serial monitor of the Arduino IDE. If everything is setup correctly it should display OK.
  • To modify the module name, enter AT+NAME=<Param>. The module should answer OK (Defaut HC-05, Ex: To change the name to BTM1 enter AT+NAME=BTM1).
  • To modify the PIN code, enter AT+PSWD=<Param> . The module should answer OK(Default 1234 Ex: To change the PIN to 0000 enter AT+PSWD=0000).
  • AT+ROLE=<Param> to midy the role of the module as slave or master (Default 0, Ex: to change the role as master enter AT+ROLE=1, as slave enter AT+ROLE=0).
  • To modify the baudrate, enter AT+UART=<Param1>,<Param2>,<Param3> with Param1, 2 and 3 serial communication parameters: baudrate, stop bit and parity bit respectively (By default,set to  9600,0,0. Ex: to modify the baudrate to 115200 enter AT+UART=115200,0,0).

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.

  • AT returns OK
  • AT+NAME=HC05-Slave
  • AT+UART=9600,0,0
  • AT+ROLE=0
  • Enter AT+ADDR to obtain the module address (ex: +ADDR:98d3:32:21450e)

 

Master Configuration

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

  • AT returns OK
  • AT+NAME=HC05-Master
  • AT+UART=9600,0,0
  • AT+ROLE=1
  • The slave module address must be enter in the master module to allows it to appair: AT+BIND=98d3,32,21450e (replace dots “:” by coma “,”)

 

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:

  • SoftwareSerial hc05(Rx,Tx) to define the pins of the serial port
  • hc05.begin() to define the baudrate (value should be the same as your module)
  • hc05.available() to test if data are available in the buffer of the module
  • hc05.read() to read data one byte at a time
  • hc05.print() to send a string in ASCII form
  • hc05.write() to send data one byte at a time

 

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

Drive your robot remotelly with your smartphone

Send temperature measurement to your smartphone

 

Source

Communicate with Arduino

Software Serial Example

Bluetooth Module HC-05

Create an application using App Inventor 2