Detect and Program/Configure HC-06 Bluetooth Module With an Arduino Mega 2560 Sketch

by formerisan in Circuits > Arduino

4418 Views, 3 Favorites, 0 Comments

Detect and Program/Configure HC-06 Bluetooth Module With an Arduino Mega 2560 Sketch

Mega2580 based HC-06 Configurator.jpg
main sketch menu for HC-06 configurator.jpg

To complete this project, you must feel comfortable downloading the sketch, opening it within the Arduino IDE, compiling and uploading code to your Arduino Mega 2560, then communicating with the sketch via the Serial Monitor. In addition, you must feel comfortable placing components and wires on a breadboard.

This sketch is designed to work with HC-06 modules which support the official Rev. 2 AT command set.

Although the baud rate of HC-06 could go up to 1382400, this sketch only supports up to 115200 due to the limitation on Arduino IDE. (230400 was listed as an choice under Arduino IDE's Serial Monitor, but it was not an reliable baud rate to work with HC-06)

Critical Reminder: The HC-06 must NOT connect to any master device. So, please make sure that the red LED near the 4 pin jumper stays flashing.

The HC-06 is an inexpensive Bluetooth slave module. It comes with factory default settings:

  • Baud rate: 9600
  • Parity: None, i.e. 8N1 (for firmware version V1.5 and up)
  • Name: "HC-06"
  • PIN: "1234"

These parameters can be modified by sending various "AT" commands to the HC-06, manually or automatically. One of the easiest ones is a Windows app from the originator of HC-06.

This sketch has 2 unique features:

  • It is a Mega2560 based sketch offering interactive operating environment
  • It will help identify the set baud rate and parity of the HC-06, brand-new or used

This project consists of:

  • Hardware preparation
  • Software preparation and execution,
  • Various screen displays the Mega 256 provides for interactive operation, and
  • A table of supported AT commands, plus the expected response from the HC-06

Many Thanks

Thanks to members of Instructables and Arduino community, your participation are always greatly appreciated. Thanks also go to authors everywhere who unselfishly shared their experience and wisdom.

Special thank goes to Guangzhou IT Information Technology Co., Ltd. for bringing the HC-06 to the world; to Mr. Martyn Currey, for sharing his vast knowledge on the HC-06 and more.

Thanks to my dear parents (may them rest in peace) for giving me all the support, love, and freedom throughout the years.

Let's get started!

Hardware Preparation

Fritzing view.jpg

Please follow the breadboard diagram for circuit connection:

  • Arduino Mega 2560 board
    • Connect Ground to breadboard upper and lower Ground power strips (black wires recommended)
    • +5V to breadboard upper power strip (red wire)
    • +3.3V to breadboard lower power strip (orange wire)
    • TX1 (18) to Level Converter's HV4 (blue wire)
    • RX1 (19) to Level Converter's HV3 (white wire)
  • Cylewet CYT1076 compatible Logic Level Converter (or Sparkfun BOB-12009)
    • Ground to Ground power strip(black wire)
    • HV to Mega2560's +5V power strip(red wire)
    • LV to Mega2560's +3.3V power strip(orange wire)
    • HV3 to Mega2560's RX1 (white wire)
    • HV4 to Mega2560's TX1 (blue wire)
    • LV3 to HC-06's TXD (blue wire)
    • LV4 to HC-06's RXD (white wire)
  • HC-06 module
    • Ground to Ground power strip (black wire)
    • Vcc to +5V power strip (red wire)
    • TXD to Level Converter's LV3 (blue wire)
    • RXD to Level Converter's LV4 (white wire)

A word abut the Fritzing symbol of level converter: Sparkfun BOB-12009 was used in preparing for the breadboard diagram. The development was based on a clone of the CYT1076 which is green in color.

Software Preparation and Execution

Please follow the steps below to get the sketch up and running:

1. Download the sketch

2. Connect the USB cable between computer and the Mega 2560 to power it up

3. Start the Arduino IDE.

4. Under Tools menu, select the correct board, processor, and port.

5. Open the Serial Monitor (Tools | Serial Monitor)

6. Select "Both NL & CR" and "9600 baud"

7. Compile then upload to Mega 2560

8. The greeting screen will now appear in the Serial Monitor screen

A word about the source code:

The source code was organized with the called function appearing before the calling function. So, the "void setup()" is undoubted placed at the very bottom.

According to the IDE, Sketch uses 10078 bytes of program storage space. Global variables use 1051 bytes of dynamic memory, leaving 7141 byes for local variables.

The Greeting Screen and Some Observed Issues

Greeting screen Mega2560 Configurator.jpg

The Greeting Screen displays very important information, including a list of observed issues during the testing of HC-06 modules on hand:

1. On setting module name, HC-06 had problem with 15 characters or more @baud rates 38400 and higher

2. Setting LED On/Off does not seem to have any effect. The research indicates that it might work on HC-06s with an extra blue LED on board.

3. Setting Slave/Master mode does not make any difference. The official HC-06 is a slave module. There are non-official HC-06s claiming to cover both modes. The sketch might work out with these modules.

Since the LED On/Off and Slave/Master mode commands were included in the official manual, a decision was made to go ahead with the implementation.

Enter Keyword to Get Past Greeting Screen

Greeting screen with keyword entered Mega2560 Configurator.jpg

The greeting screen requires the right keyword for the operation to start.

In the text box on top, please enter "YesHC-06" then click the Send button on the right.

The Configurator will now start to search for a valid HC-06.

Initial Search for a Valid HC-06 Module, Then the Main Menu

main menu after initial scan.jpg
screen showing the scanning of 9600 Even parity.jpg

After passing the greeting screen, the screen will now display the initial search for a valid HC-06.

The search will cover all 3 parity setting, starting from None, then Even, then Odd. For the testing baud rates, it will start from the lowest baud rate(1200) all the way to the top(115200). The search will end only when a valid HC-06 is discovered.

In the first snapshot, a valid HC-06 was discovered running at 9600 baud, no parity. The second snapshot shows the result of another valid HC-06 with baud rate 9600, even parity.

Once an HC-06 is discovered, the main menu will now show up, display 8 available choices.

Choice #1: Scan for HC-06

Main Menu choice #1.jpg
enter 5 at setBaudRate submenu.jpg

Enter "1" then click the Send button will cause the Configurator to enter and execute choice #1: Scan for HC-06.

This choice has the exact same effect as the initial search described above.

Again, the search will end only when a valid HC-06 is discovered.

Choice #2: Set HC-06 Baud Rate

submenu set Baud Rate.jpg
enter 5 at setBaudRate submenu.jpg

Enter "2" then click the Send button will cause the Configurator to enter choice #2: Set HC-06 Baud Rate.

1. The configurator will display the "Set Baud Rate" submenu once choice #2 was selected.

2. Once the desired baud rate was selected, the Configurator will return with the execution result, as indicated on the second snapshot. (for example, select 5 from the submenu to change baud rate to 19200)

3. After the change was made successfully, the HC-06 and the Configurator would both change their operating baud rate right away.

4. In addition, the Configurator would remain in the submenu until a "0" was received to return to the main menu.

Leaving the Set Baud Rate Submenu

enter 0 to leave set Baud Rate subment and return to main menu.jpg

The Configurator will stay in Set Baud rate submenu until the choice "0" is received.

Choice #3: Set HC-06 Name

enter choice 3, set name.jpg
choice 3, entering name.jpg

Enter "3" then click the Send button will cause the Configurator to enter choice #3: Set HC-06 Name.

Enter the desired new name for HC-06 in the text box, "HC-06 Upstairs" in the example.

Results of Setting Name

successful execution of setName.jpg
Failed to set name, too many characters.jpg

The snapshot shows a successful execution of setting name.

If successful, a message will be displayed. The operation will then return to the main menu

If failed, a message will be displayed and the HC-06's name would not change. The operation will then return to the main menu.

Choice #4: Set HC-06 Pairing PIN

choice 4, setting pairing PIN.jpg
Error on setting PIN.jpg

Enter "4" then click the Send button will cause the Configurator to enter choice #4: Set HC-06 pairing PIN.

The valid PIN contains exactly 4 digits (0 to 9)

If successful, a message will be displayed. The operation will then return to the main menu.

If failed, a message will be displayed and the HC-06's PIN would not change. The operation will then return to the main menu.

Choice #5: Set HC-06 Parity

choice 5, setting parity.jpg
choice 5, interfacing parity remained unchanged.jpg

Enter "5" then click the Send button will cause the Configurator to enter choice #5: Set HC-06 Parity

After the parity change was selected and executed, the operation will remain in the submenu until a "0" was received.

An experiment was conducted to see which parity setting was required for Arduino o talk to the HC-06 directly. The results were in: 8N1, 8E.1, and 8O1.


Unlike the set baud rate operation whereas the new baud rate takes effect right away, the parity change will take place only after the HC-06 goes through power cycling (turns off, then turns back on)

This would help explain the somewhat confusing display upon returning to the main menu:

Parity of HC-06 Upon Power Cycling: Even

Scanning Parity of HC-06: None

Parity Interfacing with HC-06: None

Choice #6: Get HC-06 Version

choice 6, getting version.jpg

Enter "6" then click the Send button will cause the Configurator to enter choice #6: Get HC-06 Version

Upon receiving this request, the Configurator will retrieve the version from the HC-06 module

Once completed, the Configurator would return to the main menu

Choice #7: Turn LED ON/OFF

choice 7, turning LED ON OFF.jpg

Enter "7" then click the Send button will cause the Configurator to enter choice #7: Turn LED ON/OFF.

Again, entering "0" in the submenu will cause the Configurator to go back to the main menu.

The author's understanding is that this would apply to a blue LED on the upper side of the HC-06 board. During the testing, It did not have any effect on the HC-06 modules on hand. A decision was made to have this command implemented as it was part of the official manual.

Choice #8: Set HC-06 to Slave/Master Mode

choice 8, setting to Slave Master Mode.jpg

Enter "8" then click the Send button will cause the Configurator to enter choice #8: Set HC-06 to Slave/Master Mode

Again, the Configurator will remain in this submenu until a choice "0" was received.

The official HC-06 will only run under the Slave mode. Some unofficial HC-06 modules do claim to cover both Slave and Master modes. During the testing, It did not have any effect on the HC-06 modules on hand. A decision was made to have this command implemented as it was part of the official manual.

Supported AT Command Set and Expected Response From HC-06

supported AT command set.jpg

The table above represents the AT command the Mega 256 sends to the HC-06, and the expected response from HC-06.