Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create a Bluetooth HC-06 Module With Arduino

DZone's Guide to

Create a Bluetooth HC-06 Module With Arduino

Learn how to wire and program a module to connect to Bluetooth so you can send and receive data.

· IoT Zone
Free Resource

A little bit ago I grabbed a cheap HC-06 Bluetooth transceiver for $6 on Amazon for my electronics project. It was fairly simple to set up but I did run into a few hitches and a (lack of information to fix them), so I’m going to detail some of my experience for you so you can hopefully avoid the same pitfalls.

Here’s a cheap one for $6.50 on Amazon. Note that there are many sellers on Amazon and Ebay selling HC-06s as HC-05s so if you can’t get AT commands to work or only basic ones work then you probably don’t have a HC-05 module.

Step #1: Wiring it Up

The first step, of course, is to wire up the Bluetooth leads to your Arduino pins. RX goes to your Arduino TX and TX goes to your Arduino’s RX — remember, they’re opposite because the Bluetooth chip is sending on TX, so the Arduino receives that on a RX pin.

Arduino Uno pins
RX TX
0 1
Arduino Mega pins
RX TX
19 18
17 16
15 14

VCC/3.3V goes to 3.3V — not 5V: Using 5V is likely to damage your Bluetooth chip, but it could probably stand it for a brief moment if you do accidentally connect it to 5V.

Once it’s connected and you turn on your Arduino, an LED on the Bluetooth board should start blinking.

Arduino Bluetooth Module connections

Step #2: Setup Code and AT Command Configuration

Next, we need to write some code so we can use this thing. Bluetooth devices can be configured with these various AT commands and SoftwareSerial, a standard Arduino library takes care of communication for us quite neatly.

Note the cheaper Bluetooth boards like the one I’m using are usually HC-06, not HC-05. HC-05 has more AT commands — and also some different ones — that will not work for yours. HC-06 will also not work if you include new line characters. There’s a bunch of example code out there that includes newline characters and also unsupported AT commands that will not work at all for your HC-06 firmware.

So below is some simple setup code that initializes the Bluetooth device and tells it to change its broadcast name to My-Sweet project. Note the delays in sending the AT config commands. In a HC-05 user guide I ran into, the delays are stated as necessary, and it appeared to be flakier without them.

#include <SPI.h>
#include <EEPROM.h>
#include <TouchScreen.h>
#define BLUETOOTH_RX 10
#define BLUETOOTH_TX 11

SoftwareSerial BT(BLUETOOTH_RX, BLUETOOTH_TX);

void setup(void) {
    // int passed here should match your Bluetooth's set baud rate.
    // Default is almost always 9600
    BT.begin(9600);
    delay(500);

    BT.print("AT");
    delay(500);

    BT.print("AT+VERSION");
    delay(500);

    // renames your BT device name to My-Sweet-Project
    String nameCommand = "AT+NAME" + "My-Sweet-Project";
    BT.print(nameCommand);
    delay(500);

    if (BT.available() > 0)
        Serial.println(BT.readString());
}


Here’s a table of HC-06 AT commands (which is quite limited compared to HC-05)

AT Responds with the connection status, also seemed like you had to run it first to get other AT commands to work
AT+VERSION Responds with device version information
AT+NAME Changes the broadcast name of the device, name can be up to 31 chars. ex.: AT+NAMEYOUR-NAME-HERE
AT+PIN Changes pairing password for the device, 4 bits max. ex.: AT+PIN0420
AT+BAUD Sets the baud rate input is 1-8 which corresponds to baud rates seen in the next table. ex.: AT+BAUD4


AT+BAUD flags and corresponding baud rate
1 1200
2 2400
3 4800
4 9600
5 19200
6 38400
7 57600
8 115200


Remember, HC-06 AT commands have no line endings at the end and no spaces between the command name and the input. It's AT+NAMEYOUR_NAME, not AT+NAME YOUR_NAME. If you see a command like AT+ORGL or AT+UART, the code you have is for HC-05.

Also note that the higher baud rates are unlikely to work well enough for these cheap devices and are not necessary for many types of projects. Furthermore, the cheap ones have a tendency of getting stuck on the baud rate if you try to change it and won’t let you change the baud rate back to the original. I got mine stuck this way when testing out AT+BAUD, and I also initially forgot to change the software serial baud rate to the new one. Remember to change it if you do use AT+BAUD.

bluetooth-board-2

Step #3: Sending Data Back and Forth

Next, we should test out sending and receiving data. I suggest grabbing BlueTerm from the Android Play Store. It’s a good ad-free app for sending and receiving raw Bluetooth data and is good for debugging. After you connect your device to a phone or something else, the LED should stop blinking and just remain steady. Note: It will only do this if connected in an app, not if it’s just simply paired.

So to send data, you just call print on your SoftwareSerial object — perhaps in your main loop:

void loop(void) {
    BT.print("Hello Bluetooth!");
    BT.flush();
    delay(2000);
}


If you have your device paired with BlueTerm correctly, you should start seeing, “Hello Bluetooth!” repeated every 2 seconds.

To receive, use code like that below and type in a message in BlueTerm or other Bluetooth app:

void loop(void) {
    BT.print("Hello Bluetooth!");
    BT.flush();
    delay(2000);

    if (BT.available() > 0) {
        Serial.println("Message recieved!");
        Serial.println(BT.readString());
    }
}


Other Notes

For a more complete implementation, you can take a look at my Bluetooth project on GitHub here. The project includes methods for sending data in JSON format with the Bluetooth transceiver in the BluetoothUIController.cpp file and also includes a companion Android app that receives those JSON messages and can send commands back down. 

Thank you for reading!

Topics:
bluetooth ,arduino ,iot hardware ,iot

Published at DZone with permission of Maddie Abboud. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}