How to configure the HC05 Bluetooth Module using AT Commands

HC06 Blurtooth Plugin
Description
Learn how to work with the HC-05 Bluetooth module and evive, including how to configure Bluetooth, use AT commands, and upload an Arduino IDE sketch to set up your device.

Introduction

Bluetooth is a form of short-range, low-power wireless communication technology that allows devices such as Smartphones, laptops, and other devices to transmit and receive data wirelessly over a short distance. The purpose of using Bluetooth is to replace cables that connect devices and also make communication between them safe and secure.

Bluetooth was invented by the telecoms vendor Ericsson in 1994 It uses a 2.4GHz frequency band, and it is the same as some other wireless technologies, such as cordless phones and WiFi routers. It creates a 10-meter radius wireless network, known as a personal area network (PAN) or piconet, which can network between two to eight devices. This short-range network allows you to perform activities like sending a page to your printer which might be in a different room than yours, playing music from the playlist of your mobile to speakers, home automation etc..

The HC05 Bluetooth Module

The HC-05 module is an easy-to-use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. The serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) with a data transfer rate of 3Mbps on a complete 2.4GHz radio transceiver and baseband.

 

evive has dedicated Bluetooth (HC05) headers which require you to just connect the HC05 Bluetooth module.

HC06 Blurtooth Plugin

Serial Communication

Bluetooth can be interfaced using Serial communication. Serial Communication transfers data one bit at a time. The speed with which it transmits the data is called the baud rate, which is expressed in bits per second (bps).

Widely used baud rates are:

  • 9600
  • 38400
  • 115200

In serial Communication, there are two wires, namely Tx (Transmitter) and Rx (Receiver). They are always cross-connected (Tx —> Rx and Rx —> Tx).

To set up the Bluetooth, you must transmit a set of commands known as the AT commands.

AT commands for any device can be found in the datasheet of a device. AT commands are English words that are most likely UPPERCASE letters and they respond in OK, Error, or Answer.

 Example

In this example, you will use the HC-05 Bluetooth 2.0 module. You will set up the Bluetooth device by changing its Name, Password, and Baud-rate.

Set Bluetooth in AT mode

  • Upload the code to evive (DO NOT connect Bluetooth).
  • Turn off evive ( Remove the cable or turn the power switch OFF).
  • Plug Hc-05 in evive.
  • Press and hold the push button on the Hc-05 module.
  • Switch evive ON.
  • Release the push button now if the led on Hc-05 starts blinking with a time difference of approx 2 seconds this means that Hc-05 successfully went into AT mode.
  • In AT mode Bluetooth always communicates at a baud of 38400 and with each command it is mandatory to send ‘\r’ and ‘\n’ while using the HC-05 module. For example, if you want to send the command “AT+NAME=evive” then it should be sent like “AT+NAME=evive\r\n”. This will be clear from the example given below.

Working of code

  • As mentioned in the point above that Bluetooth always communicates at baud rate of 38400 in AT mode hence baud of communication between evive and Bluetooth is set to 38400 in command Serial3.begin(38400);”
  • Once the Bluetooth goes into AT mode it gets AT commands from evive to change the name, password, and baud rate of evive. AT Command for displaying the address of Bluetooth is also given.
  • As soon as the code is uploaded open the Serial monitor and set the baud rate of the serial monitor to the value written in Serial.begin().
  • evive continuously sends one type of AT command until it gets the response from Hc-05. As evive gets a proper response it sends the next command. For each command sent to Hc-05 if it responds successfully, its response is printed on the Serial monitor.
evive Notes Icon
Note: The baud set by AT+UART=115200,0,0 is the baud at which Bluetooth communicates with evive in pairing mode. And also AT+UART command only changes the baud of Bluetooth for communication in pairing mode. Bluetooth always works at a baud of 38400 in AT mode.
  • As soon as Bluetooth gets “AT+INIT\r\n” command from the code it exits from AT mode and goes into pairing mode.
  • Pin-13 also turns on. This suggests that Bluetooth has been configured successfully as per the commands given in the code. When Bluetooth is in pairing mode the LED of Bluetooth blinks fast showing that it is ready to get paired.
evive Notes Icon
Note: After Bluetooth gets “AT+INIT\r\n” command if the led still blinks in the same pattern as in AT mode it means that Bluetooth is still in AT mode and not in pairing mode. In that case, just unplug Hc-05 and plug it again in evive. But this time without pressing the push button of Bluetooth.
  • Actually “AT+INIT\r\n” is for getting Bluetooth in pairing mode. But this command works on only some firmware versions of Bluetooth.

Arduino IDE Sketch

Below is the Arduino IDE sketch:

/*
  evive Bluetooth Setup

  An example of using the evive to Setup HC-05 Bluetooth.
  This example changes some configurations like name, baud rate, password of 
  HC-05 module.
  In order to change all this parameters it is necessary to make sure that HC-05
  is in AT mode.
  There are two ways of getting Bluetooth in ATmode:
  
  1) For getting HC-05 in AT mode press the push button on HC-05 before powering it and keep 
  it in pushed state even after powering it. If the led of bluetooth starts blinking after time 
  duration of two seconds then bluetooth is in AT mode. Release the button after that.

  2) One can also connect 5V to EN/KEY pin of HC-05 to bring it in AT mode.
  
  The LED on HC-05 starts blinking after every 
  2 seconds approx, this blink is different from the blink of led in pairing mode.  
  
  Explore more at:   https://thestempedia.com/tutorials/bluetooth-setup/

*/
#include<evive.h>
bool response = 0;
String response_data = "";
void setup() {
  // initialize Serial3 communication of bluetooth which is on Serial3 3:
  // bluetooth setup is done at 38400 baud with New line and Carriage Return
  Serial3.begin(38400);              //Baud for communication in ATmode
  Serial.begin(250000);
  // initialize the LED pin as an output:
  pinMode(LED_BUILTIN, OUTPUT);
  while (!(response))
  {
    Serial3.print("AT+ADDR\r\n");
    response_data = Serial3.readString();
    if (response_data != '\0')                
    {
    Serial.println("AT+ADDR");
    Serial.println(response_data);
    response++;
    }
  }
  response = 0;
  while (!(response))
  {
    Serial3.print("AT+UART=115200,0,0\r\n");
    response_data = Serial3.readString();
    if (response_data == "OK\r\n")
    {
     Serial.println("AT+UART=115200,0,0");
     Serial.println(response_data);
     response++;
    }
  }
  response = 0;
  while (!(response))
  {
    Serial3.print("AT+NAME=evive\r\n");
    response_data = Serial3.readString();
    if (response_data == "OK\r\n")
     {
      Serial.println("AT+NAME=evive");
      Serial.println(response_data);
      response++;
     }
  }
  response = 0;
  while (!(response))
  {
    Serial3.print("AT+PSWD=\"0000\"\r\n");
    response_data = Serial3.readString();
    if (response_data == "OK\r\n")
    {
      Serial.println("AT+PSWD=\"0000\"");
      Serial.println(response_data);
      response++;
    }
  }
  response = 0;
  //To initialize pairing mode of HC-05
  Serial3.print("AT+INIT\r\n");     //when HC-05 responds to this command sucessfully the led on HC-05 starts blinking in speed suggesting that bluetooth is in pairing mode.
  Serial.println("AT+INIT");
  digitalWrite(LED_BUILTIN, HIGH);
}

void loop() {

  // put your main code here, to run repeatedly:

}

You can enter your own AT commands by uploading the code given below. After uploading the code open the serial monitor and set “No line ending” to “Both NL and CR” in the serial monitor. Then start giving your desired AT command.

#include <evive.h>
void setup() {
Serial.begin(115200);
Serial.println("Enter AT commands:");
Serial3.begin(38400);
}

void loop()
{
if (Serial3.available())
Serial.write(Serial3.read());
if (Serial.available())
Serial3.write(Serial.read());
}

Conclusion

In this lesson, you learned about the HC-05 Bluetooth module and how to configure it for use with evive. You also learned about serial communication and how to use AT commands to configure Bluetooth. You then completed an example Arduino IDE sketch to set up the Bluetooth device by changing its Name, Password, and Baud-rate. Finally, you learned how to enter your own AT commands by uploading a code to the evive.

Table of Contents