Arduino HC-05 to HC-05 Communication

Here is an outline of the setup process.  These instructions assume that the device is in default mode from factory and never been programmed.

  • Setup Slave device
    • Connect Nano to HC-05 in using AT Command setup wiring
    • Verify AT command response
    • Get address of Slave using AT command, record address value
  • Setup Master device
    • Connect Nano to HC-05 in using AT Command setup wiring
    • Verify AT command response
    • Set CMODE for communication to a single device
    • Bind Master to Slave using Slave address recorded above.
  • Setup Master and Slave devices for communication
    • Rewire RX and TX connections on Master and Slave
    • Program Master with Master program code
    • Program Slave with Slave program code
  • Write application code for Master and Slave

Detailed instructions

Setup Slave Device

Plug the USB into the Arduino and load a blank Sketch using the keyboard shortcut: CTRL+N.  It should look like Figure 1.

Disconnect power to the Arduino.  Connect RX and TX pins as shown in Figure 1.  Note that RX goes to RX (with voltage divider) and TX to TX.  This is the required connection for AT command mode.

Figure 1

Physically mark the slave device with an “S” with a marker so that you will not lose track of which is the slave device.  We will do the same with the master in a later step.

Figure 2

Apply power to the Arduino by plugging in the USB.  The LED on the HC-05 should blink at 2-second intervals.  (Some instructions tell you to press the small button on the HC-05 while applying power.  However, it is not necessary if EN (Key) is hard-wired to 3.3V.  The button on the HC-05 is useful, as it can be used as an alternative to hard-wiring  EN (Key) pin to 3.3V.  Simply leave EN (Key) pin disconnected and hold the button while powering the Arduino. )

Next, the Slave will be setup using AT commands.  Open a terminal window in the Arduino environment (CTRL+SHIFT+M).  Set baud rate to 38400 if it is not already.  Set “Both NL and CR” at the bottom of the terminal window.

Start first AT commands by typing:

AT

The response should be “OK” or sometimes “ERROR(0)” but ignore the latter, just type AT again to get the “OK” response.

Confirm that the device being set up as a Slave, type

AT+ROLE?

The response in the terminal window should be:

+ROLE:0

This indicates “Slave” mode.  If instead the device was previously setup as a Master, we would not to change ROLE to 1.  Setting ROLE to 1 will be described in the Master Setup section of this tutorial.

Next get the address of this HC-05 module.

AT+ADDR?

The HC-05 will respond with a number that looks similar to this: 98d2:31:fd4ea1  This number must be recorded on paper or in a notepad on your computer.  It will be needed later.

This is all that is required for the Slave module.

Unplug the Arduino.

Setup Master Device

Plug the USB into the Arduino and load a blank Sketch in the Arduino environment using the keyboard shortcut: CTRL+N.  It should look like Figure 1.

Initial wiring for Master setup is the same as it was for Slave setup and is shown in Figure 2.  Physically mark the slave device with a “M” with a marker so that you will not lose track of which is the slave device.

Apply power to the Arduino by plugging in the USB.  The LED on the HC-05 should blink at 2-second intervals.  (Some instructions tell you to press the small button on the HC-05 while applying power.  However, it is not necessary if EN (Key) is hard-wired to 3.3V.  The button on the HC-05 is useful, as it can be used as an alternative to hard-wiring  EN (Key) pin to 3.3V.  Simply leave EN (Key) pin disconnected and hold the button while powering the Arduino. )

Next, the Master will be setup using AT commands.  Open a terminal window in the Arduino environment (CTRL+SHIFT+M).  Set baud rate to 38400 if it is not already.  Set “Both NL and CR” at the bottom of the terminal window.

Three things must be accomplished setting up the Master module

  • Change ROLE to Master
  • Change connection mode
  • Bind the Master to the Slave using the Slave address recorded earlier

Start first AT commands by typing:

AT

The response should be “OK” or sometimes “ERROR(0)” but ignore the latter, just type AT again to get the “OK” response.

Change the ROLE of the device to Master, type

AT+ROLE=1

The response in the terminal window should be:

OK

Verify that the change was made by typing:

AT+ROLE?

The response should be:

+ROLE:1

Next, check the connection mode:

AT+CMODE?

The device will probably return:

+CMODE:1

This means that the device can connect to any device.  Change it to CMODE=0 because we want to talk to only one device.

AT+CMODE=0

The response should be “OK”

Next bind address of this HC-05 module to the Slave device using the Slave address captured earlier.  However, we will replace the colons (“:”) with commas (“,”).

AT+BIND=98d2,31,fd4ea1

The HC-05 will respond with “OK”

This is all that is required for the Master module.

Setup Master and Slave devices for communication

Now that Master and Slave modules have been setup, the wiring must be changed in order for them to communicate. The wiring change is identical for Master and Slave and is described below.

Disconnect power.  Remove the connection between EN (Key) and 3.3V, and switch RX and TX connections at the Arduino resulting in the circuit shown in Figure 3.   Make these changes on both devices.

Power both devices.  After a few moments, both devices should be blinking quick blinks in close synchronicity about every two seconds.

Figure 3

Write application code

Next, step is writing application code to send information between master and slave.  Simple enough, but the HC-05 attached to the RX/TX pins impedes downloading code to the Arduino.  So, in order to download code, the RX and TX pins need to be temporarily removed.  After download, they may be re-attached.  For a final product, a DPDT switch can be installed inline with the RX/TX pins.

Credits

Leave a Reply

Your email address will not be published. Required fields are marked *