HC-05 Bluetooth module (Part 2)

Part 1, 2, 3

Once the hardware is under control, let’s focus on the software. By default, the HC-05 modules are configured in slave mode and they are discoverable by any master and consequently linkable by any master (eg. A PC, a smartphone or an HC-05 module configured as master). This configuration is obviously the easiest way to start with the HC-05 modules, however this is the less safe.

Configuring the HC-05 module is quite easy as long as you follow one of the procedures

  • Using the “basic platform”: Build the platform, set the jumper in place and power the platform. The module’s LED should blink at a slow rate (see previous post): you are in the AT mode. Once the programming done, power off the platform, remove the jumper and power the platform on again. The module’s LED should blink at a fast rate (see previous post): you are in the search mode.
  • Using the “basic platform” without the jumper: Build the platform without the jumper. Press the push button on the module and then power up the platform. The module’s LED should blink at a slow rate: you are in the AT mode release the push-button (the LED keeps flashing at a slow rate. Once the programming done, power off and power on the platform. The module’s LED should blink at a fast rate : you are in the search mode.
  • Using the “advanced platform”: Write a sketch which will turn PIND4_EN HIGH and PIND6_PWR LOW. Wait for 1 s (I could not find the exact timing, however it proved to be the safe mandatory delay required by the module). The module’s LED should blink at a slow rate: you are in the AT mode. Execute the AT commands, turn PIND4_EN LOW then turn PIND6_PWR HIGH and LOW again. The module’s LED should blink at a fast rate : you are in the search mode.

Nest is an example of code used to drive the “advanced platform”:

	\brief Set or reset at mode
	\param [in] state at mode state
	\return Nothing
	\details    In the at mode, the module's led blinks slowly
    \n          In the normal operation mode, the module's led blinks fast
void atMode(bool state)
    digitalWrite(_powerPin, HIGH); /* power off */
    if (state)
        digitalWrite(_enablePin, HIGH);
        digitalWrite(_enablePin, LOW);
    digitalWrite(_powerPin, LOW); /* power on */
    delay(1000); /* mandatory wait for module readyness */

The HC-05 modules can be given one of the three roles:

  • 0 = slave
  • 1 = master
  • 2 = slave-loop

Set the role using the AT+ROLE command, e.g. AT+ROLE=0. Read the current role from module using the AT+ROLE? command.

The HC-05 master modules can be configured in one of the three communication modes:

  • 0 = connect to fixed address
  • 1 = connect to any address
  • 2 = slave-loop

Set the communication mode using the AT+CMODE command, e.g. AT+CMODE=0. Read the current communication mode from a master module using the AT+CMODE? command.

Now a few words about the AT commands. As seen before, the module must be configured in AT (or command) mode. Under these conditions, it is able to communicate at a 38400 baud rate through its UART (Universal Asynchronous Receiver Transmitter). AT command are originated by any device physically connected to the module and able to send ASCII characters. Each command starts with “AT”, followed by the “+” sign and the command itself. Each command is terminated by a d Carriage Return (ASCII character 13 and a Line Feed (ASCII character 10). On completion of the command, the module simply acknowledges the command by issuing a “OK” or issuing a value followed by “OK”. On case of error, the module answers ERROR(nn) where nn can take the following values described in list below:

  • 00: AT command error
  • 01: Default result
  • 02: PSKEY write error
  • 03: Too long length of device name (more than 32 bytes).
  • 04: No device name
  • 05: Bluetooth address: NAP is too long.
  • 06: Bluetooth address: UAP is too long.
  • 07: Bluetooth address: LAP is too long.
  • 08: No PIO number’s mask
  • 09: No PIO number
  • 0A: No Bluetooth devices.
  • 0B: Too length of devices
  • 0C: No inquire access code
  • 0D: Too long length of inquire access code
  • 0E: Invalid inquire access code
  • 0F: The length of passkey is 0.
  • 10: Too long length of passkey (more than 16 bytes)
  • 11: Invalid module role
  • 12: Invalid baud rate
  • 13: Invalid stop bit
  • 14: Invalid parity bit
  • 15: Authentication device is not at the pair list.
  • 16: SPP lib hasn’t been initialized.
  • 17: SPP lib has been repeated initialization.
  • 18:Invalid inquire mode
  • 19: Too long inquire time
  • 1A: No Bluetooth address
  • 1B: Invalid safe mode
  • 1C: Invalid encryption mode

You might find convenient to issue batches of commands using the following function:

	\brief Send command the the HC-05 module
	\return Nothing
	\details None
void sendCmd(char cmd[])
    /* send AT command */
    Serial.print("command: ");
    /* read answer */
    uint32_t tickMark = millis();
    bool timeoutTriggered = false;
    while (!btSerial.available())
        if((millis() - tickMark) >_atCmdTimeout)
            timeoutTriggered = true;
    Serial.print(" answer: ");
    if (timeoutTriggered)
        Serial.println("<!> timeout");
        while (btSerial.available())

Next post on same subject

Leave a Reply

You must be logged in to post a comment.