Setting up a HC-06 Bluetooth Module With Arduino

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

Here’s a cheap one for $6.50 on Amazon. Note 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

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 since 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 of Arduino 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 here’s some simple setup code that initializes the Bluetooth device and tells it to change it’s broadcast name to My-Sweet project. Note the delays in sending the AT config commands, in various manuals and examples the delays are stated as necessary.

#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 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 the higher baud rates are unlikely to work well enough for these cheap devices and are not necessary for many types of projects. Also 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.

bluetooth-board-2

Step #3 – Sending data back and forth

Next you 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 the 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());
	}
}

Final notes

For a more complete implementation you can take a look at my Bluetooth project here. The project includes methods for sending data in JSON with the Bluetooth transceiver and also a Android app that receives those JSON messages. The bluetooth Arduino C++ file is named ‘BluetoothUIController.cpp’.

Thank you for reading!

Only cool people share. You do wanna be cool right?Share on Reddit0Share on Facebook0Share on StumbleUpon0Tweet about this on Twitter0

Leave a Reply

Time limit is exhausted. Please reload the CAPTCHA.