The recently enhanced 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.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.BackgroundThe 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 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. SetupFigure 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. ProcedureBig 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 AnalysisSo 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 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
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-upContact me about this exercise by commenting on my Google+ post or by opening an issue at the GitHub repository for the library. |