HC05Recovery

The recently enhanced setBaud() method in the HC05 Library makes it easy to set a configuration that does not work with an Arduino. If you are using the Arduino Software Serial ports, it is particularly easy to get locked out of your HC-05.

Should that happen to you, all is not lost! The new version of the HC05 Library also includes an example sketch that makes it easy to recover communications with the HC-05.

The recovery method is possible because there is a second type of command mode available. This recovery mode puts the HC-05 in command mode and forces the HC-05 serial settings to 38400 Baud, no parity, and one stop bit (38400N1). Access to the recovery mode requires control of the power to the HC-05 or access to the 3.3v RESET pin (#11).

This exercise will enter recovery mode by switching the power to the HC05, but if you have access to the 3.3v RESET pin then you can use it instead.

Background

The HC-05 Bluetooth to Serial module is a low cost, widely available (search ebay or Amazon) device for creating serial data connections.

The HC05 library Arduino library was written to make it easier to to work with the HC-05. The library handles the command/data mode switching and provides blocking for write() calls if there is no connection. You should refer to the Using the HC-05 Bluetooth Library exercise for more information on the library and on the HC-05 modules.

Steve Bird contacted me to suggest extending the HC05 library to configure the parity and stop bit settings. He provided sample code to show me how it could be done and then helped test the results. We quickly learned that the Arduino Software Serial port supports fewer baud settings than the HC-05 and the software serial port supports only one stop bit and no parity settings.  It was easy to lose communications between the HC-05 and the Arduino. Digging into the datasheets we found another command mode (which I refer to as recovery mode) that always uses 38400N1 serial settings.

Setup

To use the recovery example a pin on the Arduino will be used to control the power to the HC-05 module. The image at the top of the exercise shows one method. The warning in the image is important. The current that can be supplied from pin of the 5v Arduino UNO is just adequate to power the HC-05 as long the current draw from other pins is minimal. Additionally, if you are using something other than the UNO version of the Arduino, particularly 3v models, a signal pin may not provide enough current.

Figure 2, below, shows a  better way to control power to the HC-05. If your recovery needs are just one-off, then you can probably use the connections in the top image, otherwise Figure 2 is a better choice.


Figure 2: Improved power switching.

Procedure

Big Warning! In order to demonstrate the recovery sketch, this first step will set the HC-05 serial settings to something that is not compatible with the Arduino Software Serial port. The recovery sketch has worked for me, but if something goes wrong you might end up with an unusable HC-05. Follow this procedure AT YOUR OWN RISK!

Step 1: Load and run the attached script to mess up your HC-05. Use the Arduino Serial Monitor to view the output from the sketch. I will be something like this:

findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... Found.


WARNING WARNING WARNING!!!
WARNING WARNING WARNING!!!
WARNING WARNING WARNING!!!
Setting the HC05 to an unusable serial configuration
in 10 seconds...
in 9 seconds...
in 8 seconds...
in 7 seconds...
in 6 seconds...
in 5 seconds...
in 4 seconds...
in 3 seconds...
in 2 seconds...
in 1 seconds... NOW!
AT+UART=9600,1,2
OK
AT+RESET
OK
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... x
Trying 57600... x
Trying 115200... x

No connection
rate = 0
Could not find the HC05 baud setting.

This sketch worked as designed. Now use the HC05
recover sketch from the HC05 Library Examples.

Step 2: Load the recovery sketch from the HC-05 Library Examples and run it. The results should look something like this:

---------- Recovery ------------
Starting recovery in 3 seconds.
AT+UART?
+UART:9600,1,2
OK
AT+UART=19200,0,0
OK
AT+RESET
OK
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... Found.

Recovery successful. HC05 serial settings = 19200N

Analysis

So what happened?

The first sketch sets the HC-05 serial parameters to 9600 Baud, two stop bits, and even parity. It then attempts to find the HC-05 serial settings using findBaud(). That fails because the software serial port does not support two stop bits or even parity.

The recover sketch uses recovery mode to set the HC-05 to 19200 Baud, one stop bit, and no parity bits.

The HC-05 recovery mode is entered using the cmdMode2Start() function. Recovery mode (aka cmdMode2) is entered in three steps:

  1. Turn the HC-05 power off.
  2. Set the cmdPin (aka KEY) high.
  3. Turn the HC-05 power back on.
The  cmdMode2Start() function also sets the Arduino serial port to 38400 Baud, so when it returns the Arduino is ready to send commands to the HC-05.

Next the recover sketch corrects the HC-05 serial settings and then resets the HC-05 and exits recovery mode by setting the cmdPin low (using the cmdMode2End() function).

To wrap it up, the recover sketch uses findBaud() to determine if the recovery succeeded.

There are a number of delays in the process which were determined empirically. They may need to be adjusted.

Follow-up

Contact me about this exercise by commenting on my Google+ post or by opening an issue at the GitHub repository for the library.

Comments