Notes On Using The HC-06 Bluetooth Module
A couple weeks ago, on a whim, I added to add a Bluetooth module to my Amazon cart. I think I paid about $7 for it. I chose the HC-06, because it sounded better than the HC-05. I was wrong, but probably made the right choice. More on that later.
The module came with no documentation, but it was easy to find the default values via web searches.
- Name: HC-06
- Password: 1234
- Baud: 9600
I powered up the module. A single red LED started blinking.
I searched for it with my PC. I saw a new device called HC-06. I was able to pair with the default password. The LED did not change. It continued to blink. I checked my settings and saw this.
I was pleasantly surprised because I thought there was going to be some deep Bluetooth programming involved. A virtual COM is perfect. I tried opening COM12 using a Serial Terminal. It took a couple seconds, but the LED changed to constant red and the Serial Terminal was happily waiting for me to send stuff. Without anything connected to the TX and RX pins, sending stuff was not going to do anything. I was eager to test with Grbl so that meant changing to 115200 baud.
Changing the default values
You change values on the HC-06 using an AT command set. These are sent to the HC-06 on the RX and TX pins at the current baud rate. The HC-06 is in the mode to accept the commands when it is not paired and connected (blinking red LED). You cannot use the AT commands over Bluetooth.
There are a lot of AT commands, but the ones we will use are here…
- AT (it will simply respond “OK”. A good test to see if you are in AT command mode)
- AT+NAMExxx (Example AT+NAMEFRED will set the name to FRED)
- AT+PINnnnn (nnnn is a 4 digit password. Example AT+PIN1288 will set the password to 1288)
- AT+BAUDn (where n is code number for the baud as follows. Example AT+BAUD8 will set the baud to 115200)
- 1 = 1200
- 2 = 2400
- 3 = 4800
- 4 = 9600
- 5 = 19200
- 6=38400
- 7=57600
- 8=115200
- 9=230400
- A=460800
- B=921600
- C=1382400
The commands are sent without a carriage return or line feed and must be sent within a second. If you are using a serial terminal, make sure it is not sending each character as you type it or you are probably not going to get the whole command out within the second. Most terminals have a mode to send the a whole line. The Arduino IDE Serial Monitor works this way. Make sure the terminal settings are correct.
Using an Arduino to setup the HC-06
We need some hardware to talk to the HC-06 using 3.3V TTL. An Arduino will easily do that. Here is a diagram and sketch to do this using the Arduino Serial Monitor.
We will be setting up 2 serial links. One link will be from the PC to the Arduino to send the commands from the keyboard over USB. This will use the hardware RX and TX pins. We also need a serial connection from the Arduino the HC-06. We will use a software serial port for this and can use any remaining pins to do this.
This is the hardware diagram. I show an UNO, but virtually any hardware (Nano, Mega, etc) will work. The HC-06 is a 3.3V device so we need to level shift the Arduino 5V Tx signal down to 3.3V. The diagram uses a resistor divider to do this. The Arduino should have no trouble reading the 3.3V Tx signal from the HC-06, so we don’t need to shift that.
BTW: A lot of people don’t bother to shift the 5v down to 3.3V and it seems to not break anything, at least in the short term 🙂
Here is the sketch I use. By default the HC-06 is set to 9600 baud. If you change via the sketch, you will need to edit the line “hcSerial.begin(9600);” to current baud rate. This sketch strips out carriage returns and line feeds, so you can leave those on in the Serial Monitor.
#include <SoftwareSerial.h> SoftwareSerial hcSerial(11, 12); // RX, TX String fromPC = ""; void setup() { Serial.begin(9600); // hardware serial for the USB-PC hcSerial.begin(9600); // software serial Arduino to HC-06 (9600 is default) // print instructions Serial.println("HC-06 AT Command Programming"); Serial.println(" -- Command Reference ---"); Serial.println("AT (simply checks connection)"); Serial.println("AT+VERSION (sends the firmware verison)"); Serial.println("AT+NAMExxxxx (to change name to xxxxx"); Serial.println("AT+PINnnnn (to change password to 4 digit nnnn"); Serial.println("AT+BAUDn (to change to baud rate #1"); Serial.println(" BAUD1 = 1200"); Serial.println(" BAUD2 = 2400"); Serial.println(" BAUD3 = 4800"); Serial.println(" BAUD4 = 9600"); Serial.println(" BAUD5 = 19200"); Serial.println(" BAUD6 = 38400"); Serial.println(" BAUD7 = 57600"); Serial.println(" BAUD8 = 115200"); } void loop() { // Read from HC-06 if (hcSerial.available()) { while(hcSerial.available()) { // While there is more to be read, keep reading. Serial.print((char)hcSerial.read()); } } // Read from PC if (Serial.available()){ delay(10); // fromPC = (char)Serial.read(); if (fromPC == "r" || fromPC == "n") { // don't send carriage returns to HC-06 Serial.println(); // echo it back to the PC } else { hcSerial.print(fromPC); // show the HC-06 responce Serial.print(fromPC); // echo it back to the PC } } }
Using the HC-06 as a virtual serial port
If you want to use it for Grbl, you will need to change it to 115200 baud with “AT+BAUD8”. You can now connect it to your controller Tx and Rx pins. Tx on the HC-06 would go to Rx on the controller.
You then need to pair the HC-06 with the PC using the normal Bluetooth features. It will create 2 virtual COM ports. You need to select the “outgoing” port. No all programs that use normal COM port can use these ports. When you connect in the program the blinking red light will turn steady red when connected. It take a second or 2.
The Reboot Issue.
Normally Arduinos reboot when you connect. This is due to a trick with the DTR signal. The HC-06 cannot do this trick, so a Bluetooth connection does not reboot the Arduino. This should not be a problem, but some firmwares, like Grbl, send some identifying information at boot. Some senders look for this. They open the COM port and wait for that information. If they don’t see it, they think something is wrong and give an error.
Some senders don’t care and some senders have been using another trick. If they don’t see the information they send a $I command to ask for it. Ideally all sender will adopt this over time if they want to play with Grbl over Bluetooth and Wi-fi.
The HC-05
There is another device called the HC-05. It is actually more advanced than the HC-06. It can be a master or a slave device. This makes setup a little more complicated, so it was good that I chose the HC-06. The one thing I want to try is there might be a way to fix the boot issue. The HC-05 might be able to send a connected signal to force that reboot.
If you want to be notified of future blog posts, please subscribe.