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!
Attachments
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
Attachments
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!
60 Comments
Question 17 days ago
My HC-05 wont work as master mode. the AT commands works just fine but it just wont get to other state than "INITIALIZED". I'm trying to connect to a ELM327 Bluetooth dongle but it seems just doesn't want to connect. after many hours of testing, i tested it as slave mode and it works flawlessly. so the conclusion is that my HC-05 wont go into master mode.
do you have explanation and/or solution for this problem? I lost 3 full days worth of sleep because of this module.
1 year ago
Hello; for some reason after I type AT+UART into the serial monitor, I get an error message that looks like "ERROR: [0]". I went to the next step by typing AT+ORGL, but the same message pops up. Is there any advice on how I can fix this problem? Thank you!
4 years ago
HIII, I HAVE SOME PROBLEMS WHEN I TRY TO CONFIGURE MY BLUETOOTH MODULE IN TO MASTER MODE USING ROLE COMMAND IT SHOWS NO RESPONSE, BUT ALL OTHER COMMANDS ARE WORKING FINE, I TRIED TO RESTORE AND MAKING IT IN DEFAULT MODE STILL IT DOES NOT WORK FOR ME, PLEASE TELL ME IF I AM MISSING ANYTHING DURING CONFIGURATION OF MASTER MODE.
THANK YOU.
Reply 1 year ago
@LavD1 Did you get any solutions to this? My module has an EN pin rather than a Key pin.
2 years ago
this tutorial was awesome and very helpful. Thanks for the tutorial.
Question 4 years ago
Hie..This post is really helpful. In my college thesis, i need to work with more than 2 arduinos such that if master gets disconnected from one slave , it gets connected to other slave immidiatly for reliable connection. Could you pls guide me in the direction of working with one Master and 2 slaves.
I am a computer science student, so dealing with hardware is new to me. Your guidance and tips would be really helpful.
Thankyou.
Answer 3 years ago
put 2 modules connected to different uart pins then connect them to different nodes, that`s the way to make a mesh network, create a simple broadcast protocol and done.
4 years ago
hey, we have done the wiring and everything correctly. Everything works throughout the instructions. But the Master Module led blinks automatically even without pressing the button. And pressing the button does nothing. Plz help
Question 4 years ago
Hi, I have a project about make a remote wireless using HC-05 Module. I try this tutorial and its succes. But, in this tutorial just using one push button, how about using 5 push button with different LED? I've try it, but its not succesful. Please help me to solve my problem. Thank you.
4 years ago
this makes no sens, what tells which is master or slave in the code ?
this is just "no protocol" regular serial communication
how is pairing done at all ?
5 years ago
For anyone having trouble pairing the HC-05's, after step 2, but before step 3, in the Master module set up, type: "AT+ROLE=1"
This is noted from the video. It's a missing step in the description
Question 5 years ago on Step 5
will role change after step 4 in master case if yes then why mine is still showing role=0 ,plz help
5 years ago
hi sir ,
i need a help on connecting my bluetooth module to AT mode one of my module is connected one is not getting to AT mode
but finally i got the address of the other module but not getting paired ,
so please help me how to pair or tell me other ways to pair other than AT commands
5 years ago
Hola! el tutorial funciona para conectar un modulo hc-05 con un modulo hc-06?
5 years ago
Does the normal serial still work with this or is it hijacked in some way?
I tried altering the master and slave code to add Serial.println("ON"); (or "OFF" as required) but nothing appeared in the Serial monitor. Any ideas?
Reply 5 years ago
Actually forget that. It is now working
5 years ago
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!!!!
Reply 5 years ago
hold the button on the hc -05 when u plug in the arduino and you should be in AT mode
6 years ago
It's great, thank you.
schk.
Reply 5 years ago
Thank you!