Recover Misconfigured HC-06 Bluetooth Module

About: Located in Budapest, Hungary. MSC in Computer Programming. Electronics, 3D Printing, Robotics enthusiast.

Intro: Recover Misconfigured HC-06 Bluetooth Module

Out of pure maximalism, I configured my HC-06 Bluetooth (slave) module to a baud rate of 1,382,400 baud with the AT+BAUDC command. Ever since the Arduino connected to it was not able to use the module with SoftwareSerial library. I tried to restore the baud rate with Arduino's hardware serial (pins 0 & 1) with no luck.

I also tried to Google the topic without finding a passable solution. Maybe using a computer's built in serial port could have been a solution (with 12V to 3V3 logic level shifting), but my computer does not have this obsolete port, so I had to come up with another solution.

Step 1: What You Will Need

  • Arudino/Genuino board with the default Atmel ATMEGA328P-PU MCU (@16MHz).
  • A HC-06 Bluetooth module listening on 1,382,400 baud
  • Basic arduino IDE from https://www.arduino.cc

Step 2: The Solution

Please note that this Instructable and the solution is made for 1,382,400 baud (AT+BAUDC) scenario. The solution will not work for any other baud rates. To handle other cases please refer to steps starting from Step 3.

The solution is really simple.

  1. Connect HC-06's VCC pin to 5V pin of Arduino.
  2. Connect HC-06's GND pin to GND pin of Arduino.
  3. Connect HC-06's RXD pin to pin 2 of Arduino.
  4. Leave HC-06's TXD pin unconnected (or connect to pin 8).
  5. Upload hc06reset.ino sketch.
  6. The program will set HC-06 into 115,200 baud mode (AT+BAUD8).
  7. Use your recovered HC-06 module as before.

Step 3: Behind the Scenes...

The SoftwareSerial library that comes with Arduino IDE is capable of transmitting at most 115,200 bits/second, so it is not fast enough to communicate on the desired 1,382,400 baud rate. Given that a default Arduino board runs on 16MHz, the theoretical uncompressed maximum bitrate is 16,000,000 bits/sec. We're good so far!

Based on my understanding of SoftwareSerial.cpp, the seial communication is done by setting an output pin High (=1) or Low (=0) with respect to a delay (that comes from the baud rate) between the changes.

  • The output pin is high by default (means no data), then
  • a Start bit is transmitted (which pulls the pin low), then
  • 8 bits of data transmitted from LSB to MSB, (+5V when the bit 1 and 0 otherwise) then
  • a Stop bit is transmitted (which pulls the pin high)

This way 1 byte is transmitted using10 bits.

The message we have to send is AT+BAUD8 (without \n, \r at the end). This command sets the HC-06 back to 115,200 baud rate which can be handled by regular libraries.

In order to send bits with 1,382,400 bits/sec speed, for each bit we have 1/1,382,400 seconds time (that's roughly 723.38 ns) for each bit. Arduino runs on 16,000,000 Mhz, so each cycle lasts 1/16,000,000 seconds - that is 62.5 ns per cycle.

Using AVR assembly code we can use the OUT command to set output pin high or low and NOP to wait exactly one CPU cycle. Both commands eat up exactly 1 cpu cycle. This way the 723.38 ns bit time can be covered by 11 to 12 arduino instructions per transmitted bit. One thing to consider: the OUT command sets an entire byte at once, so we have to select a PORTx where this is not a problem. Using ATMEGA328P-PU for example PORTD (arduino pins 0-7) is perfect for this condition. After setting the bit, only the proper time has to elapse which is done by 10 to 11 NOPs and that's it.

You can find calculation details in the Excel file below. This file generated the required assemly instructions for the program. Only a few replaces had to be made after pasting the generated code.

Step 4: Further Reading/ Improvement Possibilites

  • Maybe a faster SoftwareSerial library can be made by using the technique described in the previous Step.
  • FedericoK2 made a great tool that generates the HC-06 recovery code for every possible bitrate. Access the site here: https://tools.krum.com.ar/save_your_hc-06/ Thank you FedericoK2

Share

    Recommendations

    • Audio Contest 2018

      Audio Contest 2018
    • Metalworking Contest

      Metalworking Contest
    • Tiny Home Contest

      Tiny Home Contest

    23 Discussions

    0
    None
    SATTAR2525

    6 weeks ago

    Hi, thank you very much for the solution. It works great. I also used auto-code generation tool. Bravo to that guy Fedrico too. I have specially signed up to share my version of solution. I was using Arduino Mega 2560.

    Problem:

    -I set HC06 baud to 1382400.. and dang no way to communicate at that rate. (Ideally, it should use that rate when paired, not in command mode actually).

    Solution:

    To use above solution with guys who have arduino Mega 2560 please use following variation beacuse PORTD does not work same way in mega.

    Configure Mega board to use PORTC and for that use PIN 34 on mega board (it is within PORTC in depth).

    we need to change addresses in code too in order to access PORTC of mega by following:

    Go to arduino, Use CTRL+F to bring up find and replace dialog box and replace all 0x0B with 0x08. And 0x0A with 0x07. Voila!!

    Rest of code and process remains same. Remember use pin 34 to transmit data to HC06.. Thanks . Happy savings!!

    0
    None
    ErkamO

    Question 3 months ago

    Hi, i'm currently trying to make 115200 baud hc06 to 9600 baud hc06
    How can i do?

    1 more answer
    0
    None
    OldSurferDude

    4 months ago

    Arrrgghhh! This looks like a really great solution, but it didn't work for me. I put a 'scope on pin2 and all I got was a 1.8s negative pulse (5v-->0v, 1.8s, 0v-->5v. I created a script using SoftwareSerial which I could step through the various BAUD rates and pin 2 as output. This script worked as expected, albeit, BAUDC was total trash (and A, B, & C weren't so good).

    The first picture is the reset code and the second is my code running BAUDC (10 bits/char, 8 char, 723.38 ns/bit = 57microS ... 'scope: 150 uSec = bad)

    Hardware: "OSEPP Uno R3 Plus" (ATmega328P)

    I suspect that I'm missing something. Any suggestions?

    hc06reset.pngbaud1.png
    8 replies
    0
    None
    DoctorYodaOldSurferDude

    Reply 4 months ago

    Sorry, I can not give any explanation why the code gave an 1.8s ground pulse on your scope. Can't even guess about that. The code must terminate after 57us, there are no loops in it and is all done in the setup() phase.

    For me it recovered my HC-06 module and it is still working.

    0
    None
    OldSurferDudeDoctorYoda

    Reply 4 months ago

    Thanks for the comments. I tried this:

    connected the Bluetooth RX to pin1 TX of the Arduino. Then ran the code: Serial.begin(1382400);Serial.print("AT+BAUD1"); Actually, there is a lot more code than this, trigger pulse, looping, stepping through each BAUD rate, etc.

    While I got the expected waveform, the part did not reconfigure. :( I suspect some of the other things I tried may have buggered the part. (Documentation implies that the HC-05 can be reset by pulling pin 24 high on power up, but there is no documentation that this will work on the HC-06.)

    I did have one thought, that I didn't try. I was attempting to go to 1200 BAUD and you went to 115200 (BAUD8). I think I'll try that and see what happens.

    Thanks

    0
    None
    DoctorYodaOldSurferDude

    Reply 4 months ago

    Are you sure that you set the HC-06 to 1382400 bits/sec? Maybe FredericoK2's website can help - see in comments below.

    0
    None
    OldSurferDudeDoctorYoda

    Reply 4 months ago

    I read the spec sheet, did some calculations and checked it out with the scope. Depending on whether the calculation is rounded or truncated, the BAUD will be 4% too fast or 14% too slow. But then, my scope showed that the 57μSecond period expected was 44.9μSec.

    I tested a hypothesis that my board clock was not 16MHz. It wasn't, but only about 1% off. I adjusted your code by taking out or adding nop's in the assumption that the Bluetooth clock was off. To no no avail.

    Thus was my day. Perhaps my experiments will inspire someone else.

    Thanks for your inspiration!

    0
    None
    DoctorYodaOldSurferDude

    Reply 4 months ago

    I think that the HardwareSerial (pin 0, pin 1) also is not capable to produce the 1382400 bits/sec speed you specified in Serial.begin(). On page 190 of this document: http://ww1.microchip.com/downloads/en/DeviceDoc/At... you can see that the specification only mentions 1M bits/sec.

    Before writing this instructable I also tried to use the HardwareSerial to reset the HC-06, but that did not work for me. I tried the pin 24 reset too, but that did not work neither.

    0
    None
    OldSurferDudeDoctorYoda

    Reply 4 months ago

    It appears that there is a "doubler" bit and it is capable of 2MHz. This is borne out when I 'scoped the output and it was as expected, though a little unstable.

    0
    None
    OldSurferDude

    4 months ago

    I just tried an Arduino Nano. The waveform looks good, but the board does not respond :( I think the HC06 is toast :

    1 reply
    0
    None
    DoctorYodaOldSurferDude

    Reply 4 months ago

    I always used my HC-06 from 5V source with 5V signal levels (with Arduino Uno). If you have the bare bluetooth module (left on the picture below) it is only 3V3 tolerant.

    HC06.png
    0
    None
    FedericoK2

    4 months ago

    Hi Doctor Yoda, I have made a PHP version of your excel to try to recover from other speeds. My question is if this part needs modification in other speeds.

    "cli" "\n\t" //disable interrupts
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"
    "nop" "\n\t"

    Please take a look at it and make me any sugestion you see

    https://tools.krum.com.ar/save_your_hc-06/

    Thanks

    3 replies
    0
    None
    DoctorYodaFedericoK2

    Reply 4 months ago

    Thank you for this great php script! I tried it and worked like a charm. Regarding your question: no, that part is not speed-dependent. Even the "nop"s are not necessary after the "cli". The first "out" can immediately follow the "cli".

    0
    None
    FedericoK2DoctorYoda

    Reply 4 months ago

    I did some beautification on the form, the table and the assembler code.
    Credited you in the assembler code too.
    Can you insert this page in your tutorial someware?
    Thanks !!

    0
    None
    DoctorYodaFedericoK2

    Reply 4 months ago

    I made a reference to your website in the Instructable. Thank you.

    0
    None
    FedericoK2

    4 months ago

    My oder question is how safe is to connect the 3.3v RX to an UNO 5v pins?

    1 reply
    0
    None
    DoctorYodaFedericoK2

    Reply 4 months ago

    I have always used my HC-06 module from Arduino 5V pins without any problems. Maybe the HC-06's pins 5V tolerant or a level shifing circuit is incorporated in the module protecting the TXD/RXD pins.