X

HC-06 Bluetooth module datasheet and configuration with Arduino

JY-MCU-Bluetooth-Module

In a previous post I shared my notes on  how to connect an Arduino to an Android phone using the the popular and cheap HC-06 Bluetooth module. In that example I used the Bluetooth module with its default settings.

That works fine, but some applications may require changing the communication speed (Baud rate), the pairing code, the module name etc. For example, I am trying to set-up a way to program my Arduino Uno and Arduino Pro Mini wirelessly, over Bluetooth. This requires changing the baud rate of the module from the default 9600 to 115200, or 57600, to match the default sketch upload speed for these Arduino boards.

Also, if things are not working, you may want to restore the settings back their defaults and start troubleshooting from there.

There are multiple versions of the module floating around, with different firmware and breakout boards, but the general functionality should match the HC-06 Bluetooth module datasheet.

Step 1: Hook up the HC-06 Bluetooth module to the Arduino

  • Connect the HC-06 Ground (GND) pin to ground (duh!).
  • Connect the HC-06 VCC pin to 5v.
  • Connect the HC-06 TX/TXD pin to Arduino digital pin 4.
  • Connect the HC-06 RX/RXD pin to Arduino digital pin 2.

It is recommended to use a level shifter, voltage regulator (or a voltage divider, like in my set-up below) to protect the Bluetooth module RX pin. It is designed for 3.3v operation, while the Arduino digital pins work on 5 volts. You do not need the LED on the Arduino pin 13 that I have on my set-up below.

Step 2: Upload the Arduino HC-06 configuration sketch

The Arduino sketch below will allow you to configure your HC-06 module using the Arduino IDE serial monitor. The Arduino will act as a middleman between the Bluetooth module and your computer. It will communicate with your PC over the built in serial connection through the USB cable, and with the HC-06 Bluetooth module over pins 4 and 2, using the Software Serial library.

The Software Serial library comes pre-installed with the latest version of the Arduino IDE. It has been developed to allow setting up serial communication on (almost any) digital pin of the Arduino, using software to replicate Arduino’s native serial support. See the SoftwareSerial library page for more details on its features and limitations.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 2); // RX, TX

String command = ""; // Stores response of the HC-06 Bluetooth device


void setup() {
  // Open serial communications:
  Serial.begin(9600);
  Serial.println("Type AT commands!");
  
  // The HC-06 defaults to 9600 according to the datasheet.
  mySerial.begin(9600);
}

void loop() {
  // Read device output if available.
  if (mySerial.available()) {
    while(mySerial.available()) { // While there is more to be read, keep reading.
      command += (char)mySerial.read();
    }
    
    Serial.println(command);
    command = ""; // No repeats
  }
  
  // Read user input if available.
  if (Serial.available()){
    delay(10); // The delay is necessary to get this working!
    mySerial.write(Serial.read());
  }
}

Credit: The code above is based on this article by user Ecno92

Step 3: HC-06 Bluetooth module configuration using AT commands

The HC-06 configurations options are covered in section 9 of the module’s datasheet.

The default settings are:

  • Name / ID: linvor
  • Baud rate: 9600
  • Pairing code / password: 1234
  • No parity check

Testing communication with the module:

  1. Open the Arduino Serial monitor. make sure you have selected the correct port and Baud rate of 9600
  2. You should see the text: Type AT commands! . If not, something is wrong and you need to re-check your set-up
  3. Type AT in the Arduino IDE Serial monitor input field and press the Send button. You should see the response: OK. Now you are ready to change the module’s settings!
  4. Type AT+VERSION without spaces in the Arduino IDE Serial monitor input field and press the Send button. You should get a response that will have the module name and version, like: OKlinvorV1.8
  5. One of the more useful attributes is the Bluetooth baud rate. You can set that by sending a command like AT+BAUD4, where the last number (4 in this case) is defining the rate as follows:

AT+BAUD1———1200
AT+BAUD2———2400
AT+BAUD3———4800
AT+BAUD4———9600 (Default)
AT+BAUD5———19200
AT+BAUD6———38400
AT+BAUD7———57600
AT+BAUD8———115200
AT+BAUD9———230400
AT+BAUDA———460800
AT+BAUDB———921600
AT+BAUDC———1382400

If you enter AT+BAUD4 you should receive a response OK9600. Do not set a rate above 115200, as you will not be able to communicate with the module through your Arduino at that speed.

Refer to section 9 of the module’s datasheet for all other available configuration options. In the video below I change the Baud rate of the Bluetooth module from 9600 to 115200.

Stan: