Aug 1, 2014

HC-05 Bluetooth link with zero code

So you want to two HC-05 modules to automatically connect together, as soon as they're powered up and with zero code? Well this is your lucky day since this can be done using the AT+BIND command.

Let's do this thing!


For this, you will need:

  • 1 Arduino (I'm using UNO)
  • 2 HC-05 modules
  • 1 breadboard
  • Wires
  • Arduino IDE (I'm using version 1.0.5-r2)


Step 1 - Code and Wires

To bind the two HC-05s, we will need to input AT commands manually which can be done using this simple code made by techbitar.

1.1 Upload the code to your Arduino.
1.2 Using one of your 2 HC-05, follow very carefully the wiring instructions (this is where most people make mistakes.)
  Note that the pins layout on your HC-05 might be different so read the pin label and be sure to connect the right ones.

Step 2 – Configure the Slave

2.1 Make sure the power wire(5.0v or 3.0v) is disconnected from the HC-05
2.2 Make sure the Key wire is connected to pin 9
2.3 In the Arduino IDE, goto the Tools\Serial Monitor menu
2.4 The two following settings are correctly set (at the bottom right of the window):
2.4.1 Line ending should be set to “Both NL & CR”
2.4.2 Baud speed should be set to “9600 baud”
    Note: f you had to modify those settings, I suggest to close and re-open the Serial Monitor dialog
2.5 When the Arduino is reseted (opening the Serial Monitor dialog will force a reset) you should see the following text “Enter AT commands:” in the Serial Monitor dialog.
2.7 Connect the power wire(5.0v or 3.0v) on the HC-05.  You should see the red light slowly blinking (once every ~2 seconds)
2.8 In the Serial Monitor, type AT and press Enter (or click the Send button).  The module should return OK right away
2.9  In the Serial Monitor, type AT+ROLE=0 and press Enter.  This will set the module in Slave mode. You can verify that it worked by typing AT+ROLE? (It should return +ROLE:0)
2.10 In the Serial Monitor, type AT+BIND= and press Enter.  This will remove any existing binding. You can verify that it worked by typing AT+BIND? (It should return +BIND:0:0:0)
2.11 In the Serial Monitor, type AT+ADDR? and press Enter.  This will return the MAC address of this HC-05 module. For example: my module's address is 00:13:03:19:14:07 and the command is returning +ADDR:13:3:191407. As you can see the zeros are stripped out and the formating is a bit different but, as you will soon see, this will not be a problem.  Write down on a piece of paper this MAC address.
2.12 Disconnect the power wire from the HC-05 and remove this HC-05 from the breadboard.  It is now ready to act as the Slave module.
   Note: Don't disturb the wiring since you will use it to configure the second module.

Step 3 – Configure the Master

*Start by installing the second HC-05 on the breadboard. Exactly where the first module was plugged.

3.1 Make sure the power wire(5.0v or 3.0v) is disconnected from the HC-05
3.2 Make sure the Key wire is connected to pin 9
3.3 In the Arduino IDE, goto the Tools\Serial Monitor menu
3.4 The two following settings are correctly set (at the bottom right of the window):
3.4.1 Line ending should be set to “Both NL & CR”
3.4.2 Baud speed should be set to “9600 baud”
    Note: f you had to modify those settings, I suggest to close and re-open the Serial Monitor dialog
3.5 When the Arduino is reseted (opening the Serial Monitor dialog will force a reset) you should see the following text “Enter AT commands:” in the Serial Monitor dialog.
3.7 Connect the power wire(5.0v or 3.0v) on the HC-05.  You should see the red light slowly blinking (once every ~2 seconds)
3.8 In the Serial Monitor, type AT and press Enter (or click the Send button).  The module should return OK right away.
3.9  In the Serial Monitor, type AT+ROLE=1 and press Enter.  This will set the module in Master mode. You can verify that it worked by typing AT+ROLE? (It should return +ROLE:1)
3.10 In the Serial Monitor, type AT+BIND=13,3,191407 and press Enter (of course here you should be using your MAC address that you have noted on step 2.12).  This will force the Master module to automatically link (bind) to the Slave. You can verify that it worked by typing AT+BIND? (It should return +BIND:13:3:191407)
3.11 Disconnect the power wire from the HC-05 and remove this HC-05 from the breadboard.  It is now ready to act as the Master module.

Done.

Now if you power both modules, they will connect right away. It doesn't matter if you power one before the other one.  They will always initiate the connection automatically.  All you need to use them in your next Arduino project is to open a Serial port and communicate through it.

Edit 1: Get more info on HC-05 through this previous post and this video.

This post is featured on:

31 comments:

  1. Hi Phillipe Cantin
    Foremost i want thanks you very much about your intruction. I was sucessful when link between two HC05.

    But may you have a little incorrect in command line "AT+BIND=13:3:191407"
    Actual : AT+BIND=13,3,191407
    I hope you will change this issue.

    Kind regards
    Richard Le

    ReplyDelete
    Replies
    1. Thanks for catching that one. I have made the correction.

      Delete
  2. Phillipe, Thank You for such great information!! My one concern, shouldn't 2.12 read "act as the slave module"? Thank you again for this Great work!!
    All my best,
    -Patrick

    ReplyDelete
    Replies
    1. Wow, I shouldn't write posts when I'm tired. Thanks for pointing this out.

      Delete
    2. I felt humble about even bringing it up. I'm such a novice, I was afraid I would be somehow wrong ;)

      Delete
    3. This comment has been removed by the author.

      Delete
    4. If your previous question is still valid: Yes you can use the data received through bluetooth (a series of char) and display it on the Serial Monitor.

      Delete
  3. I have problem with AT command whenever I try to send any AT command I do not get any answer. The only thing I can see on serial terminal is "Enter the AT command". I am pretty sure that the module is in its AT mode as its LED blinks every 2 seconds. Can you help me with this problem? I am trying to change it to the master mode.

    ReplyDelete
    Replies
    1. Other than going back over the steps described in this post, the only other things I would try would be to 1) double-check that your bluetooth module is compatible with AT commands and 2) Try it with an other computer. I know that I sometime had problems with one of my Ubuntu computer where the Arduino was not always behaving the right way, forcing me to fiddle with it until it was detected correctly by the system.

      Delete
  4. I have problem to automatically connect between my android phone and hc05 slave module. When i turn on BT handset, it can auto connect with my android phone. But hc05 can't auto connect my android phone when i turn on hc05. Do you have any idea?

    ReplyDelete
    Replies
    1. I may oversimplify things but here is what I understand. To auto connect, the bluetooth Master must actively create the connection. In the case of if peripherals like a headset or mouse, the device OS (or other software) is acting as the master and will take care of initiating the connection automatically. On the other hand, no software on your phone is installed to actively connect with a generic HC-05 module. For this to happen you would have to install an app enabling you to do it or you could write your own app that would do it. Short of doing this you will have to initiate the link manually every time.

      Delete
  5. Thanks your tutorials are great..I have a doubt..is it possible to pair hc-05 (master) with hc-06(slave).I have mostly seen people using two hc-05 modules and using them as master and slave..so I was just anxious if its possible to do the same thing with hc-05 and hc-06

    ReplyDelete
    Replies
    1. As long as the two modules are following the bluetooth standards, they can be of any model.

      Delete
  6. Thanks for your input...I have configured my hc-05 as master ..now I needed to find the MAC-ID of hc-06 module but there is no key pin in hc-06 which I could set high like hc-05 module to high to make hc-06 work in AT mode...I need to find MAC-ID of hc-06 to so that I can automatically bind it to hc-05 as you did in your tutorial.

    ReplyDelete
    Replies
    1. You can get that MAC address in many way. The lazy way (my favorite one) is to use an app like Sena (on my Android phone) and scan for the available devices. You can scan with you HC-06 powered down and then scan again with your HC-06 powered up. The extra MAC address in the second scan is probably the HC-06. Another way is to use a master module (like @marco mentioned).

      Delete
  7. You can enter AT+INQ command from the master to get the address of nearby devices. Here is a data sheet for hc05/hc06 if you need more AT commands:http://www.e-gizmo.com/KIT/images/EGBT-04/EGBT-045MS-046S%20Bluetooth%20Module%20Manual%20rev%201r0.pdf

    ReplyDelete
  8. Phillipe Thanks for your tutorials, I have a couple questions for you.
    1) I have an HC05 device controlling a door lock that I open using my phone (I use an app inventor app). I am able to use the device in master mode, and I execute a BIND command to automatically seek for the slave device (the phone) and pair to it automatically; however, once the Bind is done it takes a LOONG time to make the connection with the slave. Is there any way to have the master connect with the slave faster? Every time that the phone gets close to the device it takes like 30 seconds!!!!
    2) As this is a security sensible application I need to make sure that no other than the user inside the house can make pair. I know that if I have the hc05 as master I can execute a programmatic at+pair from the microcontroller (for example when the user presses a button, I can run code to seek and pair), but I noticed that the HC05 is visible all the time, and I can still pair with it. How can I prevent the device to be visible unless I want it to be? Thanks a LOT!

    ReplyDelete
    Replies
    1. First, nice project! 1) I don't know how the bind is handle on Android. It is possible that the automatic connection still take place after an inquiry period (where the phone looks for any available device). You would have to look into a way to skip such delay if they exist. 2) As I understand it, by setting the AT+CMODE to 0 the Slave device should only accept connection to the binded device. This could mean that it is still discoverable but other devices would not be able to connect. You can also secure it through changing the password. I'm not sure how I would hide a slave device from being discovered. I must read more on this.

      Delete
  9. hi Phillipe,

    Is it possible to bind Mac-id of PC or mobile phone with hc-05(master).I have been trying to do that but wasn't successful in it...

    ReplyDelete
  10. Also when I try to give AT+INQ command to my hc-05 it returns with error(16)..

    ReplyDelete
  11. kINLDY HELP ME .....I HAVE CZ-HC-05 MODULE ......IT CONNECTS TO THE PHONE VIA HARD CODED MAC.......BUT IT IS NOT DISCOVERABLE....NO DEVICE FIND ITS PRESENCE

    ReplyDelete
  12. @atharav I'm answering you through G+ Hangout

    ReplyDelete
  13. Very nice job !

    And now, if i want to send some infirmations to the slave from the master, how should i do ?

    Thanks for help ;)

    PS : Sorry if my english is bad, but im french ;)

    ReplyDelete
    Replies
    1. Once the connection is created the master and slave can both send and receive data through the serial port. This can be done going back and forth between the listening and the sending mode. A good way to handle this with code is tu use a simple Finite-State Machine (FSM) where you whould have states like: initialisation, connecting, sending, receiving, .... You can then build the logic code on top and switch from one state to another.

      Delete
  14. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I've answered your question in the Instructables thread (http://www.instructables.com/id/BlueTooth-Link-with-auto-detect-connect/)

      Delete
  15. Exelent!!!! I conect one of them to a litle lipo battery (1s-3,7v) and hide it in to my jaket. The other, i connect the "STATE" pin directly to a relay module and my doorway light, Voilá!!!! my Doorway light turn ON when im approaching!!!

    ReplyDelete
    Replies
    1. Nice! I like the idea of using the State pin. You can't get any faster than that

      Delete
  16. Hi phillipe.

    I'm Mao, could you help me?

    I'm trying to connect one Master with several Slaves (just 5), HOW CAN I CONNECT THEM AND CONTROL THE DISCONNECT PROCCES??

    Now, I connect "One Master to One Slave", but when I disconnect the Master, the Slave continue 'connected', and when I try again to connect it, have to pass several seconds to reconnect it, and sometimes dont transmit info or dont re-connect it!!!

    Please I need your orientation!!!

    Mao.

    ReplyDelete
  17. hi phillipe.
    i am using the command at+link to establish the link of hc05 with obd2 module but it always fails. can you please help me

    ReplyDelete