Showing posts with label HC-05. Show all posts
Showing posts with label HC-05. Show all posts

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:

Jan 7, 2014

HC-05 Bluetooth link of 2 Arduino

In my first Arduino bluetooth experiment, I made an automatic connection between 2 Arduino using BlueSmirf modules. Based on the popularity of that post, it seems that many people are trying to do the same.

On the down side, many of the visitors are looking for a solution using the HC-05 (or other HC-XX type) modules with an AT command interface. I planned to eventually use this type of device but never got around to doing it for every bad reason imaginable including procrastination. This delay was clearly unacceptable for Lyman (one of my readers) who decided to send me 2 extra modules he could spare.

On a side note, this kind soul only has 2 followers on Twitter. Please help me fix this grievous situation.

After playing a bit with my new toy, here's what I've got:


In this first simple example, the code sets the module to be either Master or Slave, connects to one hard-coded address and starts the communication loop. While connected, the Master sends 1s and 0s to the slave who is using this 'command' to switch an LED on or off.

You can get the code on this Git repo.

Breadboard diagram (without the reset connection)

The biggest problem was to figure out how to reset the module programmatically. The [not so good] documentation explains how this can be done by cycling the power but the bluetooth chip has a reset pin that is supposed to do the same trick. I have the code to use the reset pin which I got from this blog post and I will test it soon.

On the HC-05 modules I received, the reset pin is not connected which 'forced' me into making a little hack that can barely be noticed...


The next step will be to recreate the same query and auto-connect loop that I did with the BlueSmirf. It should be straight forward since I've already tested all the necessary AT commands successfully.

Edit 1: Get some tips about using HC-05 in this video I made
Edit 2: I have a new post describing the automatic bind of 2 HC-05 module without having to write code.