Introduction: Arduino Bluetooth Master, and Slave Using Any HC-05 Modules

Do you have HC-05 Blue-Smurf modules already, and have no idea what to do, or are you just looking to be able to connect two arduinos together wirelessly? This is the place to be!

This tutorial is meant to cut through all of the confusing tutorials on the internet, and show you how to finally get your HC-05 modules working together. I personally struggled for days trying to get mine working, and simply couldn't. I pulled through however, and here is an all encompassing tutorial meant to be the end all, be all of master, and slave connections, no matter the model number, or set up. I will have links to contributors in the reference section, in the code, and will also add to this tutorial as I see fit.

From ZS-040 models, to FC-114 models, to the strangest of the strange, this tutorial is meant to work with all HC-05's, and will hopefully adapt to help this purpose over time.

The setup we will be using is just a simple blink over Bluetooth serial program, but I currently have a setup for my BB-8 which uses an ultrasonic sensor in the dome to control the drive system in the ball. It will soon be able to roll around autonomously, and I will link to that instructable here when it is done.

https://www.instructables.com/id/Autonomous-BB-8-Drive-System/

Let's get started!

Step 1: Parts

1. Breadboard (you could also have two of them)

2. Two Arduino Boards (I am using a nano, and an uno, but I am sure a mega, and some others would work)

If you don't have a lot of arduino, I recommend getting a starter kit.

https://www.amazon.com/Kuman-Project-Complete-Star...

3. 2 HC-05 Bluetooth Modules (The ZS-040 modules are the best, but any one with a button is convenient w/o is hard)

https://www.amazon.com/gp/product/B00XXALRUG/ref=o...

http://www.banggood.com/3Pcs-HC-05-Wireless-Blueto...

Or, find one on ebay for cheap.

4. Push Button (any kind will work, but the one I started out with began to slip out of the breadboard)

5. Led's (any color, and size works, but if using small ones, I recommend a resistor)

6. One Resister (1k ohm I think)

7. Wires (you will need a lot of these in various colors, and sizes)

Step 2: Wiring If Your HC-05 Has a Button!

There will be a small button connected to the En pin, it should be on the lower right hand side of the face with the electronics. If you don't have this button, skip to the next step, but if you do, follow the wiring above, and annotated below. This method has worked with the ZS-040, and FC-114 modules which I currently have.

Arduino 1 (Mine is the nano)

Plug the bluetooth module into the breadboard to the right of the nano.

5v to vcc on bluetooth module

gnd to gnd on bluetooth module

pin d10 to txd on bluetooth module

pin d11 to rxd on bluetooth module

Bridge the push button across the middle of the breadboard to the right of the bluetooth module.

pin d2 to push button top right

5v to push button bottom left

take out resistor 1k ohm from push button bottom right to gnd

Plug the led into the breadboard.

pin d8 to LED positive

gnd to LED negative

Arduino 2 (Mine is the uno)

Plug the bluetooth module into the breadboard opposite the nano setup.

5v to vcc on bluetooth module

gnd to gnd on bluetooth module

pin d10 to txd on bluetooth module

pin d11 to rxd on bluetooth module

Plug the led into the breadboard.

pin d8 to LED positive

gnd to LED negative

Skip the next step!

Step 3: Key Pin Wiring

If you do not have a button on your EN pin, in order to initialize AT mode, you will need to power the key pin with a refresh. The only change in the wiring is attaching pin 9 to your key pin. If you do have a button on your EN pin, skip this step!

Arduino 1 (Mine is the nano)
Plug the bluetooth module into the breadboard to the right of the nano.

5v to vcc on bluetooth module

gnd to gnd on bluetooth module

pin d9 to key pin on bluetooth module

pin d10 to txd on bluetooth module

pin d11 to rxd on bluetooth module

Bridge the push button across the middle of the breadboard to the right of the bluetooth module.

pin d2 to push button top right

5v to push button bottom left

take out resistor 1k ohm from push button bottom right to gnd

Plug the led into the breadboard.

pin d8 to LED positive

gnd to LED negative

Arduino 2 (Mine is the uno)

Plug the bluetooth module into the breadboard opposite the nano setup.

5v to vcc on bluetooth module

gnd to gnd on bluetooth module

pin d9 to key pin on bluetooth module

pin d10 to txd on bluetooth module

pin d11 to rxd on bluetooth module

Plug the led into the breadboard.

pin d8 to LED positive

gnd to LED negative

Step 4: Entering AT Mode

If you have a button on your HC-05

First, upload the code named BluetoothModuleMasterAndSlaveNoCode below to whatever arduino is connected to the bluetooth module you are currently configuring.

Next, unplug the power to your bluetooth module, hold the button down, and plug the power back in, then release the button. This should put the bluetooth module into AT mode causing it to blink on, and off every 2 seconds, as seen in the video above.

If you don't have a button on your HC-05

First, upload the code named MasterAndSlaveBluetoothNoCodeKeyPin below to whatever arduino is connected to the bluetooth module you are currently configuring.

This should cause your HC-05 to enter into AT mode when the arduino is refreshed, to do this... before you plug in the USB cable to the arduino, unplug the power pin which goes to the bluetooth module, this is the 5v pin. Now, plug the power pin in, and the HC-05 should be blinking on, and off every 2 seconds, signifying at mode.

If this doesn't work, please check out techbitar's tutorial, and then come back for the next step..

https://www.instructables.com/id/Modify-The-HC-05-Bluetooth-Module-Defaults-Using-A/

Step 5: Entering AT Commands, and Uploading the Code!

For The Slave Module (Whichever one you want to be the slave Hint: It doesn't matter!)

1. Connect to the computer to the port connected to the arduino which is connected to the bluetooth module you wish to configure.

2. Open Serial Monitor

3. Select 9600 as your baud rate, and both nl, and cr. If this config doesn't work, comment below, or go to one of the other links, and I will try to help.

4. Now, type AT, and hit enter. You should get a message saying OK, but if you get an error, try one more time until you seek help.

5. If that works, type AT+UART, and press enter to show your baud rate. It should be 38400, if it is, move on, if not, try the next step, then check again using this command. The baud rate must be the same for both modules, and this is often where things go wrong. If the next step doesn't fix it, leave a comment.

6. Next, type AT+ORGL, and press enter.

7. Finally, type AT+ADDR, and press enter. You should get an address like the one shown in the video 2016:3:250491, or something similar. Write this down, you will need it later.

8. Close the serial monitor, unplug, and re-plug in the arduino, and upload the code called B_SlaveLink below.

For The Master Module (The other one that will be controlling the link.)

1. Connect to the computer to the port connected to the arduino which is connected to the bluetooth module you wish to configure (not the same one as before, which is now the slave).

2. Follow Steps 2-6 above.

3. Now, type AT+BIND=2016,3,250491 obviously with your respective address to the slave. Note the commas instead of colons given by the slave module.

4. Alternatively, you may type AT+CMODE=1, and press enter. This will connect to all bluetooth modules within range, but does not work when you have more than two.

5. Close the serial monitor, unplug, and re-plug in the arduino, and upload the code called B-Master below.

6. Close all instances of the arduino IDE. This will counter any linkage, limiting the functioning of the program to only as long as it is initially plugged in.

7. The program should work now. If it doesn't, check your wiring, and look at the next step.

Step 6: Further Debugging, and Beyond

What should happen?

The modules should auto link when plugged in, and when the button is pressed, both LED's should blink.

What to do if it doesn't work!

1. Pressing the button doesn't do anything: Either your wiring is wrong, or the code got corrupted, and you should re-upload.

2. Pressing the button only causes the LED connected to it to turn on: Your HC-05's are not linked, so try some of the other tutorials below, and leave a comment.

3. The LED's are always off, or on: Your button is wired incorrectly.

3. My LED is still blinking slowly: Unplug for 30 seconds, and plug back in.

4. I am not getting any messages in the serial monitor: Check the comm port you are connected to, and that the type of arduino matches up with the one you are programming to. Make sure both nl, and cr are set, and that the baud rate is correct.

5. Any other issues, or suggestions: Post a comment.

Note: The LED's on the bluetooth modules will go out of sink after a while, this does not mean that the bluetooth connection has stopped.

Links To Other Tutorials...!

Help:

For Key Pin https://www.instructables.com/id/Modify-The-HC-05-B...

AT Commands http://phillipecantin.blogspot.com/2014/08/hc-05-b...

Obscure Models, and Configs http://www.martyncurrey.com/

Very Helpful! http://howtomechatronics.com/tutorials/arduino/how-to-configure-pair-two-hc-05-bluetooth-module-master-slave-commands/

More:

http://howtomechatronics.com/tutorials/arduino/how...

https://www.instructables.com/id/AT-command-mode-of...

Post your links, and projects below, and inform me if any of these should be removed.

Beyond!

1. Will this work with the HC-06? I believe so, post a comment if it does.

2. What now? Use my code as an example for your projects, and wait for my BB-8 control system!

https://www.instructables.com/id/Autonomous-BB-8-Drive-System/

3. Other? Post a comment!

Comments

author
KhadijaB1 (author)2017-08-18

Hey, what do you mean when you said "unplug, and replug to the Arduino?" are you trying to say unplug and again replug the arduino to the PC again after entering in the AT command mode? I did this but when I repluged my Arduinos to the PC they aren't in the AT command mode. HELP PLEASE ANYONE!!!!

author
Zenmax (author)KhadijaB12017-08-20

hold the button on the hc -05 when u plug in the arduino and you should be in AT mode

author
schk (author)2016-07-05

It's great, thank you.

schk.

author
Riftliger (author)schk2017-06-24

Thank you!

author
DonCesare (author)2016-07-02

Awesome!!! This is exactly what I was looking for. Thank you so much for the instructable!

author
Riftliger (author)DonCesare2017-06-24

Thank you!

author
Shivnash (author)2016-07-08

Great tutorial.Helped me a lot..!!! Thanks

author
Riftliger (author)Shivnash 2017-06-24

Thank you guys for the great response!

author
YenigelenustalıkC (author)2016-07-10

God, at the end some tutorial which works!

author

Glad I could help, this is still my favorite project I have worked on.

author
imalan54 (author)2016-10-08

Hi, great tutorial. I have a problem getting my HC-05 to flash rapidly so it can be linked to either my laptop or tablet. I have used several tutorials and was able to connect it up to a arduino UNO and change settings on the bluetooth device in the serial monitor. When I unplug the key wire the led goes off. I unplug the power to bluetooth - wait - then plug it back in leaving key wire unplugged but the led does not come on and my computer can't find it. Help please. My bluetooth settings are: Name: HC-05, Version: 2.0-20100601, Role: 0 slave,

ADDR: 2016:1:64004, UART: 115200,

author
Riftliger (author)imalan542017-06-24

Sorry for the unimagineably late response, I was in China, if you are still working on this project, I would suggest trying to use the button to change rolls.

author
kb3cxe (author)2016-10-19

Great tutorial,

I have one question. How many slave Hc-06, or Hc-05 can a master Hc-05 connect to, and send/receive data to, and from?

Thank you,

Roland

author
Riftliger (author)kb3cxe2017-06-24

As far as I know, they can only connect to 1 other slave module, but there are commands to connect a second host, I would look into this with some other tutorials I linked.

author
AdlinF (author)2016-11-15

i push the enter after typing AT and nothing happen. what should i do please help me :(

author
Riftliger (author)AdlinF2017-06-24

This probably means you have the baud rates messed up, make sure they are 38400 across the board.

author
GullP (author)2016-12-15

i want to control multiple doors with bluetooth , plz guide me how can i do this

author
Riftliger (author)GullP2017-06-24

You can only link one master to 1 slave, if you wanted, you could connect multiple HC-05's to 1 board. I would personally suggest exploring IOT applications with arduino.

author
PrasannaaKumar (author)2017-01-14

i get something like this :xxxxÿx...........................??

author
Riftliger (author)PrasannaaKumar2017-06-24

This is likely a wiring issue, or a problem with the baudrate linkage. Make sure it is set to 38400 across the board.

author
MaksuraM1 (author)2017-01-26

is the push button necessary???

author
Riftliger (author)MaksuraM12017-06-24

No, the push button is simply added to show the functionality, applications of this project expand much further than the example which i've shown.

author
purnimakambleg (author)2017-02-13

hii....

i do as it you told...at master side only LED blinks...but LED on slave side not blinks afrer pressing push button..plz guide ..what is the problem ?

author
Riftliger (author)purnimakambleg2017-06-24

Sorry for the late response, but with minimal info on ur project, I cannot help. I can only suggest you check your wiring, and add an emphasis on following all role command instructions in the serial monitor.

author
VincentY16 (author)2017-02-14

Hello Riftliger... Is there anyway that we could actually get into the AT Command mode of the HC-05 pragmatically without holding the button? I'm having a project that which requires every startup of the Bluetooth it requires a brand new 4 pin numeric password. Thanks in advance.

author
Riftliger (author)VincentY162017-06-24

Sorry for the late response, and yes, with some models, and I am guessing the functionality works for all modules, you may use the key pin to enter command mode (I have this under a separate section in the instructable). Simply hooking this pin up to a digital output should yield the results you want. Alternatively, martyn currey has a tutorial on soldering to pin 13, and controlling the mode that way I believe. Worst case scenario, you could just wire the button to a digital pin.

author
garimagarga (author)2017-02-20

hi can u tell the code to connect different sensors on master code and get different outputs on slave code?

author
Riftliger (author)garimagarga2017-06-24

Yes, the capability is there, all these transmitters are designed for is transmitting ones, and zero's through bluetooth, but you can augment the functionality of multiple sensors by using a series of delays, more of a coding issue than anything.

author
Vinodkolte (author)2017-04-01

I want this code in word format, can you please sent me??

Given code is not opening properly.

please help me...

author
Riftliger (author)Vinodkolte2017-06-24

Sorry for the late response, the code should open properly as long as you have the latest version of the arduino ide installed, and you have saved it in a folder.

author
Johncoffee (author)2017-01-12

I had to set ROLE=1 for the master (AT+ROLE=1) since the master stayed at ROLE=0 (=slave). Afterwards it worked like a charm! Thanks a lot!!!

author
Riftliger (author)Johncoffee2017-06-24

Glad I could help, yes these declarations of the roles are necessary, and with these modules which are multi-funciton, it can get a bit tricky, and for some reason when powered off, it sometimes looses its role.

author
dipa57 (author)Johncoffee2017-04-23

Very usefull...
Thank you

author
filippo_damuzzo (author)2017-05-18

Hi everybody,

I'm wondering if this awsome guide works also with a Flora board.

Thanks in advance,

Filippo

author

As far as I can tell, the flora uses an arduino processor, and has the necessary number of pinouts for this project, so I don't see why it wouldn't work.

author
강윤허 made it! (author)2017-05-22

Thank you for your knowledge. It was a little difficult to me to make this work because I failed to do this work. But now, I finally succeeded. the reason I failed is that I didn't enter 'AT+ROLE=1' for Master moddule.gfadsgdf

bandicam 2017-05-23 01-37-27-084.jpgbandicam 2017-05-23 01-37-35-576.jpg
author
Riftliger (author)강윤허2017-06-24

Awesome, glad you got it working, a lot of simple things can go wrong, I had a similar problem at the early stages of my project.

author
dipa57 made it! (author)2017-04-23

Nice tutorial.
Johncoffee's remark was the key...
set ROLE=1 for the master (AT+ROLE=1) and ROLE=0 for the slave

I

IMG_8823.JPGIMG_8821.JPG
author
mariamntz (author)2016-07-06

Hello, the code worked perfectly for the slave bluetooth but when I try to do the same for the master it doesn't enter AT mode. I followed all the steps and when i'm pressing AT at the serial monitor it doesn't do anything. Can you help?

author
Riftliger (author)mariamntz2016-07-07

Yes, in order to help, I need to know what model, and type your master module is, and what arduino board you are running it on. Make sure your wiring is correct also. There are a few things that could be wrong.

1. It isn't wired correctly (i.e. make sure it is the same as the slave... Check the rx, and tx pins)

If this is all correct, try changing the position of the rx, and tx pins in the code, and in your wiring.

2. It isn't in AT mode

3. There is something wrong with the baud rates.

Try changing these around in the code, as well.

author
mariamntz (author)Riftliger2016-07-07

I'm using 2 HC-05 ZS-040 Bluetooth modules and 2 arduino micro. I switched the Bluetooth modules between them and now the master is the one that works. So I changed the other arduino micro with an arduino uno and I still have the same problem. Both modules enter AT mode because the led is blinking every 2 seconds. The wiring and the baud rates are correct. This is what I get:

SLV.JPG
author
Riftliger (author)mariamntz2016-07-07

Interesting. Like I say in my post, this is a hard process. I have a couple of ideas, and will continue to try and support you, but also try other tutorials that I have linked. First of all, you could simply configure both modules with the one arduino, but I doubt the code would work if one of your setups is not working. But try this, and just upload the master, or slave code to the arduino uno, don't configure the module on this device. Another thing I would try is switching out aspects of one setup to the other, like there might be a faulty usb cable... .

author
mariamntz (author)Riftliger2016-07-07

I used the AT+CMODE=1 in serial monitor and they linked. it works now but i have no idea what happened. I didn't change anything. Do you think it's possible to connect an HC-06 instead the HC-05 using that command?

author
Riftliger (author)mariamntz2016-07-07

AT+CMODE=1 will connect to all nearby bluetooth modules. It would probably connect to an HC-06. So was the connection by address not working?

author
mariamntz (author)Riftliger2016-07-07

No, in the beginning i thought it was the wiring but I changed it multiple times. Then I changed the arduino board just in case there was something wrong with that, but in the serial monitor i couldn't get a reply again. So I programmed the slave bluetooth with the arduino board that works correcly and then i swiched it with the master and I programmed it also. They didn't link with the AT+BIND=..... command so I tried the AT+MODE=1 and it worked. I also tried to connect the HC-06 but it doesn't work, maybe it needs some changes in code?

author
Riftliger (author)mariamntz2016-07-07

Ya, I haven't worked with the HC-06 a ton, but http://www.martyncurrey.com/ has a good tutorial. Just skip his wiring, and it should work.

author
mariamntz (author)Riftliger2016-07-07

Ok, thanks a lot! Your project is very helpful, I struggled for days trying to figure out how to connect them. Yours is the only one that actually works. I'll try to connect the HC-06 too, thanks for the link.