After my first post using a Bluetooth module, things have evolved a bit. The challenge with these Bluetooth modules is: they look the same, but having different firmware. I did not fully realize that until I have ordered another bluetooth module from dx.com:
That module comes already on a carrier, so I assumed I can use the same driver as for my other module. I was wrong :-(.
HC-05 or HC-06
My earlier module which I received from another source (without an adapter, see this post) has a different firmware on it, known as HC-05, while my DX.com module has a HC-06 firmware. To be clear: the modules are the same, but the software/firmware on it is different, and the firmware uses the pins differently too 😦
💡 Check out this post which explains how to re-program the firmware of the device with firmware programming adapter: http://byron76.blogspot.ch/2011/09/hc05-firmware.html
The HC-05 has the ‘full’ firmware on it: many AT commands, and can be both master and slave module. The HC-06 firmware on the other hand only can be a slave device, with very limited AT commands.
Or in other words:
- The HC-05 module can build a connection to other modules. E.g. a Robot being a master and connecting to slave bluetooth module. Or in slave mode to make a wireless bridge to a notebook.
- The HC-06 module only can be a slave. This makes it only useful for say connecting a notebook as a master to a robot with a slave module e.g. for a wireless serial bridge.
For most use cases the HC-06 is enough, as typically I want to have a wireless UART connection to my devices from my notebook.
JY-MCU V1.5 Module
Below is an image of the JY-MCU HC-06 (JY-MCU V1.5) module. The module came with a 4-pin header, and I have added the pins for STATE and KEY, and removed the plastic around the module to get access to the pins:
Pins
On the bottom side there are labels for the signal direction and voltage levels:
- KEY: according to the data sheet, I need to pull-up this pin while power-on-reset of the module to enforce AT mode. I have not been able to verify this yet. I have been told that some modules have this pin not connected at all?
- VCC is indicated in the range of 3.6V-6V. The module worked for me both with 3.3V and 5V.
- GND: Ground
- TXD: serial output of the module, to be connected to RX of the microcontroller. Note that this signal is using 3.3V logic level
- RXD: serial input of the module, to be connected to the TX of the microcontroller. Note that this signal is using 3.3V logic levels.
- STATE: connected to LED2 (Pin32) of the module, but no meaning? At least on my module the pin was always low, regardless if paired or not.
Different AT commands
On the HC-05 module, I send “AT\r\n” to the device, and then it responds with “OK\r\n”.
But on the HC-06, the protocol is different 😦 I need to send “AT” (without the new-line characters), and I receive “OK” (without the new-line characters).
The logic analyzer shows this behaviour too: AT command sent to the device:
OK response from the device with no “\r\n” at the end:
The missing “\r\n” is present for all commands of the HC-06 firmware. As as this is not enough, there are very few command possible. The table below shows all the HC-06 firmware commands with the response:
Command | Response | Comment |
---|---|---|
AT | OK | Used to verify communication |
AT+VERSION | OKlinvorV1.8 | The firmware version (version might depend on firmware) |
AT+NAMExyz | OKsetname | Sets the module name to “xyz” |
AT+PIN1234 | OKsetPIN | Sets the module PIN to 1234 |
AT+BAUD1 | OK1200 | Sets the baud rate to 1200 |
AT+BAUD2 | OK2400 | Sets the baud rate to 2400 |
AT+BAUD3 | OK4800 | Sets the baud rate to 4800 |
AT+BAUD4 | OK9600 | Sets the baud rate to 9600 |
AT+BAUD5 | OK19200 | Sets the baud rate to 19200 |
AT+BAUD6 | OK38400 | Sets the baud rate to 38400 |
AT+BAUD7 | OK57600 | Sets the baud rate to 57600 |
AT+BAUD8 | OK115200 | Sets the baud rate to 115200 |
AT+BAUD9 | OK230400 | Sets the baud rate to 230400 |
AT+BAUDA | OK460800 | Sets the baud rate to 460800 |
AT+BAUDB | OK921600 | Sets the baud rate to 921600 |
AT+BAUDC | OK1382400 | Sets the baud rate to 1382400 |
That’s it.
Firmware Timing
As this is not enough, my driver did not work even with the new commands implemented. The HC-05 firmware as sending a response back in less than 300 ms, while the HC-06 firmware needs more than 500 ms until there is a response:
So for this I had to introduce a user configurable delay in the component.
Processor Expert Component
With this knowledge, the Processor Expert Bluetooth component has been updated to support both the HC-05 and HC-06 firmware:
- Firmware to select between HC-05 and HC-06
- Configurable Response Time if the module needs longer for commands
- Optional State and CMD pins
If the HC-05 firmware is selected, then the component automatically disables the functionality methods not present/supported in the firmware (grayed out methods):
Command Line Interface
The Processor Expert component features an optional command line interface:
With this, I can change the pairing pin, device name or baud, beside of sending AT commands or sending a string over the wireless bridge.
💡 Changing the pairing/name/baud will be effective after resetting the device. Keep in mind if you change the baud, this will change the baud as well between the module and the microcontroller.
The ‘status’ command issues an AT command to the device to see if it responds, plus shows the firmware version:
❗ Status and AT commands can only be used if the device is not paired yet (means: while the red LED is blinking).
Connecting to the Bluetooth Module
The Bluetooth module runs the SPP (Serial Protocol over Bluetooth) protocol. So any device supporting SPP can connect to it. On a PC this looks like a virtual COM port. I show here the steps for Windows (running Windows 7).
❗ It seems that Apple (iPhone, iPAD, etc) does *not* support SPP, so connecting with an iPhone is not possible. Android (which I did not try) should work, or any PC machine with Bluetooth.
Before connecting, make sure the module is powered and ready to pair. The red LED on the module indicates the status:
- blinking: ready to pair
- steady on: paired
From the Device Manager, select ‘Add a Device’:
Then the new device should show up:
💡 the name of the device shows here for me ‘blue1’, as I have named it as such. But it might show up for you as ‘linvor’ (default) or ‘other’.
Select the device and press ‘Next’. In the next dialog select ‘Enter the device’s pairing code’:
The default pairing code is 1234:
Pressing next, and device drivers will be installed:
Then the device is ready to use:
And the confirmation dialog shows up:
COM Port used by Device
Checking the properties on the newly added device shows that it supports SPP. And it shows the virtual COM port used:
❗ Note that if I check the COM ports in the device manager, then I see that actually two COM ports have been added. Only the one shown above with the SPP protocol will work. It is unclear to me why there is a second port?
Connecting to the Wireless Bluetooth Bridge
Using that COM port shown for the SPP service, I can connect with a terminal program on the host PC to my board. Basically this gives me a wireless bridge over Bluetooth to my board. So from my PC I can open a terminal window and type in some commands, which are parsed by the Shell on the FRDM board, and it responds back to the terminal on the PC:
❗ Make sure you use the COM port used for the SPP service, and that it matches the baud settings of the communication between the microcontroller and the Bluetooth module. I’m using above the default of 9600 baud. It is possible to change/increase the baud as explained above, as 9600 is not very fast. Only be sure that you not exceed the baud to a value which cannot be handled by your PC. It should work ok up to a baud of 115200.
Once connected, the red LED on the Bluetooth module is always on.
While connected, the module is in ‘transparent’ mode, and does not accept AT commands. Below is an example where I try to send an AT command from the microcontroller while the Bluetooth module is connected to the host PC:
Instead, what I send to the UART ends up transparently on the host PC:
Wireless Bridge
Everything I send to the virtual COM port ends up on the Bluetooth module, which then sends the commands to the microcontroller using the RX and TX connection between the microcontroller and the module. With this, it is very easy to send/receive commands using the Processor Expert Shell component, and the implementation are just a few lines:
/** * \file * \brief This is the implementation module for the shell * \author Erich Styger * * This interface file is used for a console and terminal. * That way we can interact with the target and change settings using a shell implementation. */ #include "Shell.h" #include "CLS1.h" #include "LEDR.h" #include "LEDG.h" #include "LEDB.h" #include "BT1.h" static const CLS1_ParseCommandCallback CmdParserTable[] = { CLS1_ParseCommand, #if LEDR_PARSE_COMMAND_ENABLED LEDR_ParseCommand, #endif #if LEDG_PARSE_COMMAND_ENABLED LEDG_ParseCommand, #endif #if LEDB_PARSE_COMMAND_ENABLED LEDB_ParseCommand, #endif #if BT1_PARSE_COMMAND_ENABLED BT1_ParseCommand, #endif NULL /* sentinel */ }; /* Bluetooth stdio */ static CLS1_ConstStdIOType BT_stdio = { (CLS1_StdIO_In_FctType)BT1_StdIOReadChar, /* stdin */ (CLS1_StdIO_OutErr_FctType)BT1_StdIOSendChar, /* stdout */ (CLS1_StdIO_OutErr_FctType)BT1_StdIOSendChar, /* stderr */ BT1_StdIOKeyPressed /* if input is not empty */ }; void SHELL_Run(void) { unsigned char buf[32]; unsigned char bTbuf[32]; buf[0]='\0'; bTbuf[0]='\0'; CLS1_ParseWithCommandTable((unsigned char*)CLS1_CMD_HELP, CLS1_GetStdio(), CmdParserTable); for(;;) { (void)CLS1_ReadAndParseWithCommandTable(buf, sizeof(buf), CLS1_GetStdio(), CmdParserTable); (void)CLS1_ReadAndParseWithCommandTable(bTbuf, sizeof(bTbuf), &BT_stdio, CmdParserTable); } }
Unbinding and Trouble Shooting
In case there are issues with connecting to the module, it is necessary to unbind and re-bind (connect) to the module. It happened to me sometimes I’m able to connect once, but then not any more. In that case the following steps help:
- Close any terminal program potentially connected to the Bluetooth virtual COM port.
- Unpower the Bluetooth module so it is not visible any more to the PC.
- Right click on the device in the Windows Device manager (or Devices and Printer group) and select ‘Remove Device’:
- Re-power the module: the red LED shall be blinking as not connected.
- Search for the device in the device manager (as above), and connect again to the device with a pairing pin.
- Connect to the module using the COM port specified for the SPP service.
That way I was always able to recover connection to my module. See as well this post which helped me to solve my problem.
Sources
All the component sources discussed are available on GitHub. Additionally, the FRDM-KL25Z Bluetooth example project has been updated to support both the HC-05 and HC-06 modules.
Happy Bluetoothing 🙂
Drar Erich,
I learned from you a lot, thank you.
I have problem with using HC06 with K64F, I don’t know how to print the receiving data on the ConsoleIO.
I used components such as; AsynchroSerial, ConsoleIO, utility, CriticalSection and shell.
Sorry, I have no experience in C language, I don’t know how to write a cod for calling the received data and printed on the screen! !
I followed your tutorials and I can print any text on the screen.
Could you please give me hands?
Thank you
Regards
Sam
LikeLike
Hi Sam,
I’m affraid that you need some C experience to do something like what you are asking for. Like with anything, it needs patience and practice, so don’t give up learning C/C++!
To print any received character from the Bluetooth module, you can do this in a simple loop like this:
{
uin8t_t ch;
for(;;) {
if(BT_RecvChar(&ch)==ERR_OK) {
(void)AS1_SendChar(ch);
}
}
}
where AS1 is your console/terminal channel.
I hope this helps,
Erich
LikeLike
Hi
I am currently using the HC-06 but need to have iOS compatibility. This requires BLE as supported by the newer HM-10 and HM-11. Does anyone know if the HC-06 SMD module can be replaced by the HM-10 or HM-11 without the need for PCB changes ie. drop in pin-for pin compatible and same size?
Thanks in advance
LikeLike
Hi Sean,
I have not used any of the HM-10 or HM-11 modules. But what I have used with iOS are these modules from Adafruit: https://mcuoneclipse.com/tag/ble/
LikeLike
Pingback: Připojení loggeru ke smartphonu - Aeroklub Holíč
Pingback: Vixen Super Polaris GoTo – dings...!
Pingback: Vixen Super Polaris GoTo – dings...!
Hi! I’m doing Bluetooth connection from Android to Arduino. I wanted to know uf the android part os the same for HC-05 and HC-06. I’m just sending data
LikeLike
Yes, they will look the same from that perspective.
LikeLike
Hello Mr Styger,
we successfully implemented your code and we are able to change some settings of our HC-05 module. However, not all AT Commands are working. For example we can get the current UART parameters with AT+UART but we are not able to set them. AT+UART=38400,0,0 returns:
“Response not OK
Failed or unknown command : BT1 cmd AT+UART=38400,0,0”
It seems rather random which commands are working and which aren’t.
Another strange thing we noticed is that we can set the name of the device to only 4 characters. If we try 5, we get the same error as above. We tested it with two different firmwares (V2.0 and V2.1).
Is there a possibility that there’s a buffer overflow?
Or could it be that the firmware is limiting our permission to change certain settings?
Thanks for your help. It is greatly appreciated.
With best regards,
Nico
LikeLike
I recommend you hook up a logic analyzer to the Rx and Tx lines to see what the module responds. The other thought I have is that indeed it might be depending on the firmware of the modules you have.
LikeLike
Hi
Is it possible to set response timeout less than 650ms in HC-06?
I want to make the response faster.
And another question is “Processor Expert Component” a free software? where can I download?
Thank you
Regards
Andrew
LikeLike
Hi Andrew,
yes, you can specify any time you want. But be aware: making the timout faster won’t give you a faster HC-06.
And yes, Processor Expert is free-of-charge software. It is integrated for example in Kinetis Design Studio (eclipse based): http://www.nxp.com/kds
LikeLike
Hi
I have the HC06 and Arduino Uno, The plan is to use this as a slave device to an Android phone and do the proximity check. So the HC06 will pool every 500mS for the paired device and up on master in the range the slave will trigger a digital output.
Have you came across such an application?
Thanks for any reply
Anand
LikeLike
You will have to initiate a pairing from the Android phone. If this is done, then yes, you can detect that the device is paired on the Arduino: either you check the LED status with a pin or you probe the HC06 if it is still in command mode or not.
Good luck!
LikeLike
regards! I need help with a project that I have in execution, which is to make an alarm with a motion sensor which when activated will send me a message to my android phone, but what I have is a HC-06 … with this??? And if it is or not I could explain. . I am not very good that we say with the logic and programming in C
LikeLike
Best if you use a microcontroller connected to the HC-06 over UART. Sense the motion with the microcontroller, then send a message over UART to the bluetooth module which is bonded to the Android phone. On the Android phone you need a UART/COM connection, write an app or script which reads the incoming messages and checks for that alarm message.
LikeLike
You want to tell me that the HC-06 can use it as a master in a microcontroller ??? … because in fact I have a FREESCALE KL46Z that could use. Although it is new and I am only learning to use it.
LikeLike
No, the HC-06 can only be slave. You have to a have a look a the HC-05, see https://mcuoneclipse.com/2013/06/19/using-the-hc-06-bluetooth-module/
LikeLike
Pingback: Bluetooth Robot – Intelligent Technology
sir i bought a hc 06 bluetooth module. after power up i can’t find bluetooth signal from my mobile phone or pc and it aslo doesn’t reply to any at command
LikeLike
there are many different HC-06 modules, and they can have different firmware. For the AT commands, you should check the signals with a logic analyzer first.
LikeLike
Hello
How i can make use of Programmable input/output in HC-05 or HC-06 for activate or deactivate a single LED????
LikeLike
Hi Juan,
the modules I have do not have that capability. You would need a moduld which have this capability, or emulate it with a micrcontroller attached to the Rx/Tx line.
LikeLike
Hi Erich,
Great site. I added bluetooth to one of my old RC chargers and found this page while doing some research on HC-05 vs HC-06 modules. Kudos on a great site you’ve put together!
Your old Metrowerks / Freescale colleague!
Berardino
LikeLike
Hi Berardino,
glad to hear you found it 🙂 and thanks for your feedback! Wow, how fast time is passing by, and I miss my that Metrowerks culture…
Erich
LikeLike
Yes, time does fly by fast! Memories thankfully last forever 🙂
LikeLike