Arduino and HC-05 in communication mode
Updated on 18.07.2015.
Updated 01.12.2016
There are now newer HC-06s and HC-05s that use the zs-040 breakout boards. These new modules have a LED (usually blue) at the top left of the Bluetooth daughter board and have a different firmware to the below. See HC-06 hc01.comV2.0 for an introduction to the HC-06. I haven’t written up details on the HC-05 yet.
Here is the zs-040 version of the popular HC-05. The HC-05 is based on the EGBT-045MS Bluetooth module. It can operate as either a slave device or a master device. As a slave it can only accept connections. As a master it can initiate a connection.
The EGBT-045MS Bluetooth modules (the smaller daughter board) is a 3.3v device. The HC-05 break out board has a 3.3v regulator that allows an input voltage of 3.6v to 6v but the TX and RX pins are still 3.3v. This means you can use the 5V out from the Arduino to power the boards but you cannot connect the Arduino directly to the HC-05 RX pin.
For the HC-05 RX pin (data in) we need to convert the Arduinos 5V to 3.3v. A simple way to do this is by using a voltage divider made from a couple of resistors. In my case I use a 1K ohm resistor and a 2K ohm resistor.
As a quick guide to the voltage divider; 1K + 2K = 3K. 1K is a third of 3K so it reduces the voltage by a third.
One third of 5V is 1.66 and 5-1.66 = 3.33 which is what we want. Putting the resistors the other way would reduce the voltage by 2 thirds.
For more information on voltage dividers have a look at the Sparkfun tutorial
Since the Arduino will accept 3.3 volts as HIGH you can connect the HC-05 TX pin (data out) directly to the Arduino RX pin (The 5V Arduino takes a voltage of 3V or more as HIGH).
HC-05 zs-040 Status LED
The onboard LED shows the current state of the module.
– rapid flash (about 5 times a second) – module is on and waiting for a connection or pairing
– single quick flash once every 2 seconds – the module has just paired with another device
– double quick flash every 2 seconds – connected to another device
The HC-05 remembers devices it has paired with and after cycling the power you do not need to re-pair. This means you can go from
– rapid flash (about 5 times a second) – module is on and waiting for a connection or pairing, to
– double quick flash every 2 seconds – connected to another device directly.
Connections
HC-05 Vcc to 5V (can be from the +5V out from the Arduino)
HC-05 GND to common GND
HC-05 RX to Arduino pin D3 (TX) via a voltage divider
HC-05 TX to Arduino pin D2 (RX) connect directly
Remember to connect the GNDs together
Power on the Arduino and the LED on the HC-05 should flash rapidly, about 5 times a second. This means it is on but not paired or connected. So far so good.
Default Settings
The default settings for new modules are
– Name = HC-05
– Password = 1234
– Baud rate in communication mode = 9600*
– Baud rate in AT/Command mode = 38400
*Not all modules have the same defaults. If you cannot get communication mode working with 9600 try other baud rates. 38400 is also common.
Thes above values can be changed but for now we will leave the default settings and try to connect to another device.
Added 12.07.2015
I have some new modules that have a default baud rate of 38400 for both AT mode and also communication mode. If you cannot get 9600 to work try 38400
Pair with an Android device
In my case, the Android device is a 7 inch tablet.
Before you can make a connection between blue tooth devices they need to be paired. So, with the Arduino and HC-05 powered, on the Android device;
– turn on bluetooth,
– scan for devices and the HC-05 should be listed,
– pair with the HC05 and enter the password “1234” assuming you have the default password.
Once paired the blinking LED on the HC-05 will change to a single short blink every 2 seconds or so.
Connect with an Android App
Connecting the Android device to the HC-05 creates a serial communication channel very similar to the serial monitor in the Arduino IDE. This means we need a Bluetooth version of the serial monitor. I use a Bluetooth terminal aptly named Bluetooth Terminal.
Download it from Google Play here. Download, install, and open Bluetooth Terminal.
– open the menu,
– select “Connect a device – Insecure”,
– click “HC-05”, and “connected: HC-05” should appear at the top of the screen.
The app I used is no longer available in the Play Store. However, a quick search for “bluetooth terminal” will list many alternatives.
Once connected, the LED on the HC-05 will blink quickly twice every 2 seconds or so.
OK, so the connection is working, now let’s try to send data from the Arduino to the Android device.
Arduino Sketch 01. Sending data to a connected Bluetooth Device
The following sketch sends “Bluetooth Test” to the serial monitor and the software serial once every second.
Upload the following sketch,
// Basic Bluetooth sketch HC-05_01 // Sends "Bluetooth Test" to the serial monitor and the software serial once every second. // // Connect the HC-05 module and data over Bluetooth // // The HC-05 defaults to commincation mode when first powered on. // The default baud rate for communication is 9600 #include <SoftwareSerial.h> SoftwareSerial BTserial(2, 3); // RX | TX // Connect the HC-05 TX to Arduino pin 2 RX. // Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider. // char c = ' '; void setup() { Serial.begin(9600); Serial.println("Enter AT commands:"); // HC-06 default serial speed for communcation mode is 9600 BTserial.begin(9600); } void loop() { BTserial.println("Bluetooth Test"); Serial.println("Bluetooth Test"); delay(1000); } |
Connect the HC-05 to the Android device and open Bluetooth Terminal. Open the serial monitor and you should see “Bluetooth Test” scrolling up the screen in both windows.
Arduino Sketch 02. Talking to a connected Bluetooth Device
Now lets try having a 2 way chat with the Android device. We are still using the Bluetooth Termainal app but this time we will trying sending messages to the Arduino.
// Basic Bluetooth sketch HC-05_02 // Connect the HC-05 module and communicate using the serial monitor // // The HC-05 defaults to commincation mode when first powered on. // The default baud rate for communication mode is 9600 // #include <SoftwareSerial.h> SoftwareSerial BTserial(2, 3); // RX | TX // Connect the HC-05 TX to Arduino pin 2 RX. // Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider. // char c = ' '; void setup() { Serial.begin(9600); Serial.println("Arduino is ready"); // HC-05 default serial speed for commincation mode is 9600 BTserial.begin(9600); } void loop() { // Keep reading from HC-05 and send to Arduino Serial Monitor if (BTserial.available()) { c = BTserial.read(); Serial.write(c); } // Keep reading from Arduino Serial Monitor and send to HC-05 if (Serial.available()) { c = Serial.read(); BTserial.write(c); } } |
Upload the above sketch.
Open the serial monitor
Open Bluetooth Terminal and connect to the HC-05.
Now, whatever you type in the serial monitor should appear in Bluetooth Terminal a,d whatever is typed in Bluetooth Terminal should appear in the serial monitor.
The above examples use the HC-05 Bluetooth module in slave mode and the HC-06 acts exactly the same and the same sketches can be used.
Next step, turn a LED on and off from an Android app. I used the HC-06 in the LED example but a HC-05 can be used as well.
Hi Martyn,
Thanks for the useful tutorial. I am facing an issue with using the HC-05 – initially on power up, the status LED blinks fast and on pairing with my smartphone it blinks slowly (once every 2s, I think) indicating that it is connected. However, the issue is that even after I disconnect on my phone, the LED still blinks slowly – even though the pairing has been broken. Do you see this as well?
Thanks.
On start up, with the HC-05 previously paired, the LED flashes quickly.
From the Bluetooth Terminal app I connect to the HC-05 and the LED changes so this is blinks quickly twice every 2 seconds.
When I disconnect from Bluetooth Terminal (the BT module is still paired but not connected), the LED on the HC-05 returns to blinking quickly.
If I then unpair the HC-05, the LED stays blinking quickly.
If I then re-pair the HC-05, I get a single blink every 2 seconds.
The only time you get the quick single flash every 2 seconds is directly after pairing. The HC-05 remembers devices it has paired with so after pairing you go from not connected directly to connected.
Hello
Thanks for the easy integration article.I am a research student working on health sensor devices using arduino uno. I am using this http://www.cooking-hacks.com/ehealth-sensors-complete-kit-biometric-medical-arduino-raspberry-pi kit .
Now i want to use ardunio HC05 bluetooth module to send data from sensor board to my android phone.Idea is to write a simple programme which can pull sensor data from sensor board and send it to the cloud through HTTP.I am looking for possible collaboration/help or suggestion on this. I am stuck on integration the bundle(ardunio + ehealth sensor board) with android phone. The ehealth shield has support for bluethooh(their own Bluetooth solution) and 3g connectivity(it requires bending the pins, which i am afraid to do, because it can break the circuit)
Would it be possible for you to communicate? skype or email?
Thanks very much and i would really appreciate your help
This is beyond the scope of my blog. I would suggest you join the Arduino forum. You will get lots of help there.
If you haven’t started on an Android app have a look at MIT’s appinventor. This is easy to use and should be enough to create an app to do what you want.
Thanks for your very good lessons.
I can do work sketch 1 and 2, but something is wrong on my system: I can send words from serial monitor to Blueterminal, but not in the other direction. Nothing apears in serial monitor, except “arduino is ready”. Any sugestion?
thanks for your attention.
Do you have the correct baud rate for BTserial?
Not all HC-05s and HC-06s have the same default settings. Try other settings. 38400 and 115200 are also popular.
To try different baud rates you need to change the BTserial.begin(9600) statement. For example BTserial.begin(38400)
If you are not sure and you know how to enter AT mode then you can change the baud rate using “AT+UART=9600,0,0”. This sets the baud rate to 9600.
Hy Martyn
I feel I’m very close to the goal, but not enought.
I ‘m using a new zs-040 HC-05, and a Mega ADK, but same pins as your sketch. Also 3.3v maximun (shift bridge divider) in H5-RXD.
Also I changed to another zs-040-HC 05 and no difference. both same action: I can connect from android BT terminal, and send ascci from Android, but nothing appears on Arduino Serial monitor. But the good thing is that every word I send from Arduino Serial monitor, it appears in Android BT terminal. So some way is working.
Changing baud ratio to other values in BTserial do de same, no better, no worse.
One important thing, but I dont understand meaning: RXled on MEGA blinks when I send from Arduino Serial Monitor (it is OK), but TXLed on MEGA never blinks when I send from Android. Also it is 3.3v (I tester measured it) on HC05-TXD pin, but never changes , neither TXled on MEGA does.
I thing it means something clear, but i dont know what it is.
Any idea?
Hy again Martyn.
No progress.
I tried run the sketch // Basic bluetooth test sketch 5a for the Arduino Mega. (in your web …https://www.martyncurrey.com/using-an-arduino-mega-with-a-hc-05-zs-040/#more-2090), and I could went to a new mode (HC05 blinking on/off every 2 seconds) with the button switch, but nothing more.
When I send 1 from Serial monitor, nothing happens, except RXLed on MEGA ADK blinks once.
In this case, I use pins 18/19 for tx1/rx1, same as your sketch.
I am using Arduino IDE 1.6.5, and seems Ok,
Also tried IDE 1.5.6-r2 and same results. Sending 1 from Serial monitor does nothing.
More ideas?
I now think you might be using the wrong sketch.
I have added a basic sketch at the bottom of https://www.martyncurrey.com/using-an-arduino-mega-with-a-hc-05-zs-040-at-mode/ for you to try.
Please note that this connects to the HC-05 at 9600 and uses communication mode. If you have your HC-05 using a different speed then you will need to change the Serial1.begin(9600); to the speed you are using.
Hi Martyn
Thanks a lot for your patience and dedication.
Finally it works !!!!!!!!! .
I’m ashamed to say it was my fault. I’m guilty (Very much!!). My voltage divider resistors (made on a breadboard) did BAD contacts, so it failed one way of the BT communication.
I am sorry you wasted this time.
By the way I have played a lot (really many hours) and I learned something .
I also used the SenaBT (an android BT terminal) that gives some help using the AT mode (but on the BT android of my Samsung) .
For information I discovered my ZS-040 firmware version obtained with AT commands
this is the output :
AT
OK
AT+VERSION
+VERSION:2.0-20100601
OK
AT+UART
+UART:9600,0,0
OK
AT+NAME?
AT+STATE
+STATE:PAIRABLE
OK
AT+ROLE
+ROLE:0
OK
Thanks again.
Jesus
No problem. Good to hear you got it working.
Hello Martyn ,
I want to use HC 05 to send my sensor reading via bluetooth on another BT device connected to COM port of my PC.
Please help me stepwise.
You need to pair the HC-05 with the computer first. You do this through the computer settings.
After you have paired the HC-05 with the computer it will be given a COM port. If you have 2 COM ports use the first one. The second will be something like Dev-B. You can then use the COM port as you would a USB COM port.
To get started with receiving data ffrom the Arduino/HC-05 have a look at https://www.martyncurrey.com/arduino-and-visual-basic-receiving-data-from-the-arduino/ and https://www.martyncurrey.com/arduino-and-visual-basic-controlling-an-arduino/. These are getting started guides for Visual Basic.
HELLO,
by using of hc-05 & arduino r3, i want to do something like, to know the communication status between terminal device(from my laptop) and hc-05,
For this initially i will pair the hc-05 with my laptop ,when ever it is paired i want to make one digital o/p pin of arduino should be HIGH, after when ever i move the laptop beyond the range to comminicate i want to make one digital o/p pin of arduino should be LOW, and again it should be when ever i move the laptop with in the range , this must be done automatically for this process i am stucking,
so please provide me the solution. thank u..
Updated:
To confirm my understanding, you want to have a LED light when the laptop is out of range. I don’t think there is a way to do this through the hardware but you could do it through software. The Arduino could send a message to the laptop and if no reply is received then the laptop will be out of range or no longer connected. I would send the message every 1 or 2 seconds.
It would be worth while asking on the Arduino forum. There are many helpful people there. http://forum.arduino.cc/
THANK Q
I am using HC-05 having 6 pins VCC,GND,TX,RX,STATE ,WAKEUP
and what is the use these ,STATE & WAKEUP pins. brieflly…
The STATE pin is normally connected to the LED2 pin on the mini BT module. It goes HIGH when the module in connected. You can connect to the Arduino to determine if the BT module is connected.
I do not have any boards that have a WAKEUP pin. However, if the breakout board is the same as others then it will trace back to pin 34.
one more query!!!
when the HC-05 is AT MODE can we communicate with the mcrocontroller
for making the digital o/p HIGH & LOW
waiting for ur reply??
Do you mean the pins on the Bluetooth module or the Arduino?
If you mean the BT module:, command. See the At command manual page 12. However, the pins on the mini BT module are not broken out and you will need to solder a contact yourself.
I don’t know what version of the HC-05 you have but for most you can control the pin state using the AT+PIO=
If you mean the Arduino then please explain a little more about what you want to do.
Dear Martyn,
First thank you very much for your quick reply. Those are really helpful. I and Viswesh working together.
Here I am re-framing Viswesh question:
1) Are there any AT commands ,which we can use when HC05 is in “Communication Mode”(not in AT MODE)?
2) In Arduino programming: How can we read AT command responses and compare?
3) When HC05 is in pairing with some device .. can I use “AT+STATE? ” to know the status of the device? One of Response of the “AT+STATE” is “DISCONNECTED” . How do I implement in code?
4) Our basic need is to know the connection status of the HC05 dynamically . I.e when HC05 is CONNECTED with any device my Arduino board should be able to identify and at the same time whenever it is DISCONNECTED from device my Arduino board should be able identify the same. I dont mind whether it is by Software code or by Hardware connection to Arduino
Thanks in advance.
HARI
Hi, I will try to answer your questions.
1. NO. Once the BT device is in communication mode everything it receives it treats like data and sends to what ever it is connected to. In communication mode the BT module simply sends out everything it receives.
If you need to move in and out of AT mode then you can make a connection from the Arduino to pin34 on the mini BT module (you will need to reduce the voltage to 3.3v though). Put the pin HIGH to go in to AT mode. LOW to return to communication mode. Have a look at https://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/
2. You need to parse the replies you receive from the BT module via the serial connection. Use a char array to store the reply. Keep adding the latest byte you get from the serial connection to the array. Once you have a full reply determine what it is.
You may need to experiment to see if there are common terminating characters used (I think chr(10)/nl is used) at the end of the data the BT modules sends out. When get the terminating character you know you have a full reply and can then work out what the reply is (such as “OK”).
I don’t have an example to post and I’m not at home for the next week or so so cannot work on anything. Go to the Arduino forum and search for serial basics by Robin2 this should get you started.
3. I don’t think you can check the pair status through the STATE pin. The STATE pin will go HIGH once a connection is made but it remains LOW at other times. Remember that the paired state is different to the connected state. It may be possible to tap in to the LED1 pin and check the flash rate. Most modules change the LED flash rate to show the device is paired. Or, depending on the module you have you may be able to use the serial response from the module. For example the FC-114 modules report when they are paired. If I needed to do this I think I would try using the LED1 pin.
4 You can do this through the STATE pin. LOW for not connected and HIGH for connected. Simply connect the STATE pin to an Arduino digital pin and use digitalRead() to get the value.
You can also do it through software by sending a regular message and reply. If you do not get a reply then you know you are not connected.
I have done this both ways. The STATE pin can tell the Arduino when the BT module is connected but it cannot tell the other device (in my case an Android device or a PC). Using software means both ends can be aware when there is a connection and when there isn’t.” once a second and expects “
For example. I have a Android app that after a connection is made sends out “
” as a reply.If the app does not get the “
” reply then the app knows there is no connection.” every second then the Arduino knows there is no connection. If the Arduino does not get the “
Please be aware that connected is a different state to being paired. When you pair the HC-05/06 with another device all that happens is the HC-05/06 stores the information of the other device so that a connection can be made later.
I would suggest you experiment. Pair the HC-05 with different devices and make different connections and monitor the pin states and the LED state. Do this with the serial monitor open and see what messages you get. Once you know the behaviour of the BT module you can decide how to proceed.
What module do you have? Is there a model number?
Thanks alot Mr Martyn for your reply. Unfortunately I couldn’t reply immediately as I was out of station. I will check the code as per your suggestion and will be back soon with details of my experiments.
Dear Martyn,
I have an Intel Galileo Gen 2 and I tried to communicate with Intel Galileo via HC-05 zs-40 bluetooth module from my computer but I couldn’t accomplish. I have already done the communication with my Arduino Uno and I want to do the same thing with my Intel Galileo. SoftwareSerial library is not supported for Galileos so I’ve decided to connect HC-05 module’s rx and tx pins to Galileo’s tx and rx pins. Unfortunately, I wasn’t succesfull. I will be so happy if you send a message related to my problem.
Best Regards…
I do not have nor have I used the Intel Galileo but I believe they have hardware serials so instead of software serial you should be able to use serial1. This should be similar to using serial1 on the Arduino mega.
I would suggest you join the Arduino forum. There are many helpful and knowledgeable people on the forum. http://forum.arduino.cc/
Hey !
Have you find a solution for your problem?
hello
i have a problem, with the second code
when i send data from the serial monitor to android
1. i can’t see the data on the serial monitor, but i receive the data on the android app
and when i send data from the android app to the serial monitor
2. i see the data on the android app ( orange color) but i don’t receive anything on the serial monitor
and i really don’t know why!
so what could be the problem?
Hi Anthony,
1. This is correct. The data you enter in the serial monitor is not copied to the serial monitor main window.
2. Check the connection from the bluetooth module to the Arduino and check that you are using the correct pin. BT TX/OUT goes to Arduino D3.
Edit.
I notice from the post in the Arduino forum you may have used different pins to connect to the Bluetooth module. I would suggest using the sketch as it is and using pins 2 and 3. Once you have it working then change the pins if you want to.
HI!! tried changing pins but it seems my module only communicate on pin 0 and pin 1. I am only able to send data from arduino to phone.
But data from phone to arduino is not coming to serial monitor.
So it would would be awesome if you can help me with this
Pins 0 and 1 are the hardware serial which probably means you are not using software serial.
When one side of the communication is not working it is normally a bad connection or the wrong pin is being used.
i got the same problem too, is it because of I have connected my HC05 RXD to 5V before and it is burnt and thus it couldn’t receive anything from the android phone?
If you have had it working and now it doesn’t, then this is possible but I believe unlikely.
If you have never had it working then it is likely something else.
alright, thanks a lot!
Hi, thanks for your post, its really helpful.
But, i want to ask, what happen is pin Rx directly connected to 5v?
I use HC-05 connected to RS232 to ttl adapter (not to arduino), as far as i know the adapter have 5v.
as long as i try, it still can send data, but i don’t know long effect of connecting Rx directly to the adapter without voltage divider.
There are many people who connect directly and short term it should be OK. Long term I recommend using a voltage divider as people have reported damaging the HC-05/06
Ok, thanks Martyn.
how can i stop my toy car when bluetooth range is over or bluetooth gets off
The only reliable way I have found is to have one device poll the other. The polling frequency based on you own needs.
For example, once a second:
Device A sends a “are you there” request to device B.
Device B replies “yes I am”.
If device A does not receive a reply within a second then it knows the connection is lost.
If device B does not get the “are you there” request within a second (I use slightly longer) then device B knows the connection is lost.
I should add that I am using app inventor to create Android apps and this has limited Bluetooth functionality. Using Java may be better.
hi there, i paired up 2 hc05 master “1” and slave “0” doing this
AT commands
//SLAVE = 98d3:31:2093cb AT+ROLE=0
//MASTER= 98d3:32:204d2a AT+ROLE=1
//AT+BIND=98d3,31,2093cb
//AT+BIND=98d3,32,204d2a
The end im left with paired modules that can talk to one another now i set this in AT+UART=9600.0.0 or 38400,0,0 im not sure now but it worked out fine now in the sketch i was doing all this in terminal obvsly using the terminal baud rate which was
as follow
include SoftwareSerial.h
SoftwareSerial mySerial(10, 11); RX, TX
void setup
{ Serial.begin(38400);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.println(“Enter AT commands:”);
mySerial.begin(38400);
}
so my problem is that when i try to take the 2 talking modules to use in another project im not sure but they dont communicate no more “after uploading new sketch” am i suppose to use the same baud rate as in the sketch (38400); or should i be using the baud that i set this 2 up to begin with when i did the whole AT+UART=9600.0.0 or 38400,0,0 ???
i was assuming what i have now when they were talking was a wireless RS232/terminal right? if so then all i have to do is make sure in the loop of what ever other program i go to use this modules on that they talk to that SoftwareSerial mySerial or something like this
void loop()
{
if (mySerial.available())
mySerial.write(“Variable or Array here”);
Hello, I can send text from android app to serial monitor, but not from serial monitor to android app. What is happening?
I assume you are using the example sketch 2 above. Check the Arduino TX to BT RX connection.
Does example sketch 1 work?
it happen to me also. all the connections are correct. but i only can receive the text that were sent from bluetooth terminal but i cant see the text from serial monitor send to the bluetooth terminal.
I ran the first sketch, successfully connected to the Android, however the text message did not appear in Blue Terminal. In the Arduino serial monitor, Arduino Ready only appeared. Any help is appreciated.
I’m not familiar with Blue Terminal. Can you try with the Bluetooth Terminal app (see above) and let me know.
hello sir
thanks for such a great tutorial.
my communication between 2 bluetooth modules is started but when i am sending something from 1st serial monitor it is not coming same on another one for example if i am sending ‘p’ in another monitor it prints some kind of symbols
pls help me in resolving it
It sounds like you have an incorrect baud rate somewhere.
Hi, I am trying to connect the bluetooth on an android to the HC-05 and it disconnects after about 10 seconds or so, any idea as to why this keeps happening?
Sorry, I can’t help except suggesting you check the time outs on the Android device. Try increasing the time before it goes to sleep.
I did some basic experiments and the connections I have are very stable. HC-06/05 to Sony Z3 Compact and also a Samsung 7inch tablet. Even after the Android device closes the screen and then comes back the connection is still available.
Martyn,
The HC-05 modules that I bought on eBay look like yours.
I have the RXD pin connected directly to D9 on the Arduino, and the module works.
Should I be concerned?
Some have connected the Bluetooth modules directly and report they work fine. Others report they slowly kill the RX pin on the BT module. I would always suggest adding a voltage divider and believe it is better to be safe than sorry.
The daughter board is a 3.3v device and if you trace the RX pin on the breakout board you can see that it goes directly to the RX pin on the daughter board.
Disregard previous question as it was answered.
It seems that I need to leave the EN pin floating or the HC-05 module will not work.
By will not work, I mean that the red LED is not lit at all. If I have the EN pin connected to D13 on the Arduino Uno, the module is unresponsive.
I have tried setting D13 to an output and setting it to LOW.
Any ideas?
The EN pin is the enable / disable pin. When pulled LOW it disables the module. When pulled HIGH the module should work as normal.
hello martyn ,i am a great admirer of your work over the internet.
can you please enlighten me whether i can use bt slave mode to get arduino working with other tasks. My main idea is when the bluetooth module connected with the arduino gets the signal of the android bt(app driven), then it gets to work i.e its gets on and when the bt signal of the android is off peripheral arduino connected projects like object detecting, relaying stops working;;;main theme is to use the arduino in standby mode and working mode when it gets the android bt signal; then what will be coding modification of your provided sketches;;so please give me a solution so that i can use bt signal to get my arduino working from “standby” mode(no android bt signal) to “working” (android bt signal on,hence the app too) mode;;;
i know my question is lengthy;;; just want to be enlightened by you sir;
it will be a great help;;tc;;
Hi manjur,
if I understand correctly, you want to have the Arduino + BT module come out of standby mode (sleep mode), connect to an Android device, send data, go back to sleep.
Will you power down the BT module when not in use/in standby mode?
thanks for your reply;;;
the main idea is to when the bt module detects the signal of android device bt, the arduino starts “working” with my project;; and when not the arduino stops working;; so do i have to create multiple functions to link the bt code with the peripheral arduino project i.e relaying ,detecting object etc?? Like, while arduino gets signal of android bt then it passes the task from bt sketch to other sketches;; and is power down is an easy option?? If not then its not necessary;;;the necessary thing is to get the arduino to “work” when it gets android signal;;no signal no working to the peripheral projects;:: so where the bt code should be modified and for passing the task to another sketch what can i do???
Your advice is very much appreciated;;
This is fairly straight forward if the Arduino and BT module are always on.
When the BT module receives data it will forward to the Arduino. When the Arduino receives the data it should do something based on the data received.
I am assuming that you have several different things to do and use different codes.
For example:
if recievedCode == “LEDON”, turn on the LED
if recievedCode == “LEDOFF”, turn off the LED
Within the main loop you keep checking for received data until you have something.
Powering down the Arduino is possible but adds many complications. I am not sure if you can power down the BT module and have it wake on a connection being made. I suspect this is not possible with the cheap HC-05s and HC-06s but I do not know.
Experiment with the connection, pair and connect the BT device with Android and then restart the BT module and see what happens.
A quick google brings up some autoconnect apps. Such as https://play.google.com/store/apps/details?id=org.myklos.btautoconnect&hl=en. These may be the way to go.
Hello!
I need to read/write PIO from pc connected in bluetooth (without pc connected directly to HC-05 )
Is there the possibility to send AT command from remote PC?
I can see 2 com port in the device connected in bluetooth; what is the second one?
Thank tou very much!
AT commands can only be sent via a hardware serial connection, they cannot be sent via a Bluetooth connection.
Ok. Thank you!
i have a problem my bluetooth module is able to receive data but not send.My connections are correct.
Double check your connections. If you are using a voltage divider check the resistors are the correct values and the correct way a round.
Hi Martyn,
Is it possible that HC-05 receive and send data simultaneously?
I am trying to use multiple Arduino+HC-05 to sense signal from each other.
Using “AT+INQ” to receive RSSI signal, then sent the value to PC (wireless),
but the result show the same value and repeated.
like this:
AT+ROLE=1
AT+INQ
+INQ:9060:F1:207147,7E010C,FFD5
OK
+INQ:9060:F1:207147,7E010C,FFD5
OK
+INQ:9060:F1:207147,7E010C,FFD5
OK
AT+ROLE=0
here is my code:
void loop()
{
//when BTserial.available()..
char c = BTserial.read();
readString += c;
BTserial.print(“AT+ROLE=1\r\n”);
BTserial.print(“AT+INQ\r\n”);
BTserial.print(readString);
BTserial.print(“AT+ROLE=0\r\n”);
}
It seems that HC-05 receive data once and send data repeatedly.
Hi Donna,
If you mean better read/write performance; the example sketch uses software serial which is not that good, for better performance use altSoftSerial, or better yet, the hardware serial to talk to the Bluetooth module. Note that AltSoftSerial uses pins 8 and 9 on the Arduino.
If you mean connect more than 2 modules at the same time then this is not possible. With Bluetooth you can only connect 2 devices at a time. To have a small network you would need to make a connection to each module one at a time.
Make a connection to device 1, get data from device 1, close the connection.
Open connection to device 2, get data from device 2, close the connection.
etc etc
Hi Martyn,
My work is:
Using multiple Arduino+HC-05 to sense signal from each other
And all of them just connect to PC (let PC collect the data)
So maybe I should try these steps
Arduino wake up
AT+ROLE=1
AT+INQ
AT+PAIR
send data to PC
AT+ROLE=0
Arduino sleep
But how could I keep the RSSI value after “AT+INQ” until paring PC and send to PC?
Is there some command/instruction in Arduino to keep data in register?
Thanks :)
If you need to store a value for an indefinite amount of time and across resets use eeprom.
Do you really need to store the RSSI value? This is just an approximation of the signal strength.
My homework just let me collect the approximation value, so I use RSSI.
I’ll try EEPROM to store the data.
Thank you Martyn : )
hi there!! when i tried both the examples ,the first one worked just fine but the second one in which a message is transmitted ,i received some symbols in my serial monitor,so what should i do to get the exact same transmitted code from my phone. is it any related to changing my AT baud rate…? if yes how can i do it ? I have got the same hc-05 zs-40 module …..or any other suggestions to get it working..
This normally indicates either; an incorrect baud rate or trying to send too much data too fast.
When using Software Serial, keep the baud rate low and keep the data segments as short as possible. If you need to use a faster baud rate, use AltSoftSerial or better still the hardware serial.
Hello,
I would like to log my temperature information. Should the HC05 be in slave or master mode? Is there a simple way to receive them on my laptop via the bluetooth connection.
Thanks
The HC-05 should be in slave mode.
You can connect from the computer. Use the PC to initiate the connection. Once you have made a connection you should have a COM port, use this as you would a usb COM port.
Hello,
I tried to program my arduino nano with the program that you have published on this webpage. I connect my HC05 in the same manner as you have suggested. I am able to receive all the characters on serial monitor properly but when I send message from serial monitor to BTSerial then I am only getting double digit numbers on BTSerial. May I know what would be the problem? Thank you very much.
Hello,
I am having problem while receiving data into my android bluetooth serial monitor from arduino connected with HC-05 bluetooth module. I am able to send data from arduino to the computer serial monitor correctly but I am not getting correct data while sending it from arduino to my android phone.
I am using the code as below:
#include
SoftwareSerial BTserial(10,11);
void setup() {
Serial.begin(9600);
Serial.println(“Arduino is ready”);
BTserial.begin(9600);
}
void loop() {
BTserial.println(1024);
delay(2000);
Serial.println(1024);
delay(2000);
}
On my android bluetooth serial monitor I am receiving data like 31 30 32 34 0D
i dont know what is the problem.
I have checked the Buad rate of HC 05 using AT it shows it is 9600, 0, 0
Please help.
Thank you very much.
BTserial.println(1024); sends the values 1,0,2,4 and the values you are receiving are the hexadecimal values of the numbers; 31=1, 30=0, 32=2, 34=4, and OD is the carriage return character added by using println.
If you want to send as ascii put the number in quotes – BTserial.println(“1024”);
I have connected it , but the device doesn’t appear in my android
I been doing my final project. I wonder, can i connect HC-05 to car Bluetooth then comunicate to arduino uno to trigger the port at arduino ??
Without knowing what Bluetooth system is being used by the car I can’t really say. However, I would suspect it is Bluetooth 3/HID or Bluetooth 4 and if so the HC-05 will not connect.
To know for sure you need to confirm what version of Bluetooth the car uses.
Hi, all,
thanks for this page, it is great.
I have a Arduino Mega with a HC 05 Bluetooth Modul as Master and a connected second BT Modul HC 05 as Slave, whithout any board. It is only powert by VCC and ground. After automaticlly connection i need then name of slave modul, or the adress???? is it possible??
thanks for helping.
You can only change the HC-05 settings from a hard wired connection using the serial communication.
hi,
I’m using bluetooth module hc05 with Arduino uno. I’m facing a problem in pairing my module with my android device. The module gets listed in the available devices, however, when I select it to pair, I get the message “Couldn’t pair because of incorrect pin”. My device does not even ask for any passkey.
Also, certain AT commands such as AT+PSWD does not respond.
Please provide a solution.
what HC-05 board are you using?
hey i have a problem with my hC- 05 IT cannot appear with my phone Bt when i search for devises its said nothinf appear
please help
There are many different HC-05s, which model do you have?
What mode is it in, slave or master?
its JY-muc,usually when i plug the current in 3.3v its appear in most videos in youtube but mine no i dont know what’s the problem exactly
If you are connecting vcc to the breakout board then the minimum voltage is 3.6v but 5v is better.
The breakout board has a voltage regulator that reduces the vcc to 3.3v. To work it needs at least 3.6v in.
can i rename the Bluetooth module
yes, see the other posts.
What is the use of switch button and how i can connect to another android devices by disconnecting my old device
The button switch brings pin34 HIGH which puts the module in to AT mode.
You connect to other Android devices in exactly the same way as the first device; pair, connect. You can only be connected to one Android at a time though. So to connect to a 2nd device you would need to disconnect from the first one.
I am working on a project using HC-05. It get connect with terminal but when i am using my android app, sometimes it get connection error and sometimes it works. I set baud rate at 9600. Can anyone tell me what is the problem??
I can’t help on this without knowing more details.
hi. I am trying to control mindwave headset with hc 05 and the led first rapidly blinks then it slowly blinks for about 2 seconds what is going on can you explain pls
hi martyn, i am not able to recieve the messsage in serial monitor when send from bluetooth serial terminal for pc but the bluetooth terminal is recieving from serial monitor.can you help me out?
i am using the HC-05 with ttl output
Check the connections and make sure the correct pin is defined as the Arduino RX pin.
The connection are correct,the problem is till there
??
i am using arduino mega 2560
code
#include
SoftwareSerial BTserial(2,3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//
char c = ‘ ‘;
void setup()
{
Serial.begin(9600);
Serial.println(“Arduino is ready”);
Serial.println(c);
// HC-05 default serial speed for commincation mode is 9600
BTserial.begin(9600);
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
c = Serial.read();
BTserial.write(c);
}
}
The sketch looks fine and all I can suggest is recheck the components and the connections, even if you believe they are correct. Almost always there is an issue with the circuit. Make sure you have the correct resistors in the voltage divider and that the resistors are in the correct order.
When using the mega you do not need to use software serial. The mega has extra hardware serial channels that can be used. See https://www.martyncurrey.com/using-an-arduino-mega-with-a-hc-05-zs-040-at-mode/
So I tried alot of stuff couldnt figure the problem…the first code works even though i connected my RX/TX pins in pin 0/1 of my arduino. In the second code, if i connect to 0,1 pin and change the code BTSerial(0,1); then when i send from android to computer i get wierd symbols. Also, I cant seem to send data from serialmonitor to the android.
But if i connect the TX pin of HC-05 to someother pin then it works( by changing BTSerial to the corresponding pin). But i cant figure how to send data properly from arduino to the android…
Hi Martyn,
In my home automation project, I’m using both BT HC-05 and IR, with RTC 1307 and 20*4 LCD on I2C bus. First problem is that buzzer / tone command is not supported with I2c , gives compiling error, secondly major issue is that either BT or IR works, depending on which is placed first in loop.
Individually both IR and BT function well. Can you guide?
Thanks
Romy
I haven’t used BT and IR together but suspect it is a resource conflict, possibly they are trying to use the same timer. I have had issues using software serial and the servo libraries together due to this.
If not already a member, join the Anduino forum and ask there. I am sure somebody there can help.
Hi Martyn,
Thanks for your quick reply, I’ll try to find and resolve these conflicts, one more thing can I use an Atmega with bootloader , say a nano or Pro Mini with IR sketch in slave mode and master Uno having LCD, RTC , BT and relay output. Connecting both thru I2C. How to configure …any hints or idea.
Thanks
I have little experience with I2C. The times I have used it it has simply worked.
Ask on the Arduino forum. You should get help there.
Hi !
I got a problem with my hc05.It.s the third module that i`m using and the result is the same.It doesn.t pair with any phone (but with a bluetooth LE app seems to connect, even though it.s not helping me in any way), with my laptop pairs without asking me for any password but finnaly it doesn.t show up on com ports list. Long story short, i tried to enter in command mode, but there ar more command that it doesnt respond to, such as AT+UART=, AT+CMODE (i tried using \r\n but no difference) , on AT+NAME or AT+ROLE the value after these commands it.s used(including =). What should i do? it upsets me for 2 weeks but i don.t want to go back:)
-pins are correctly attachet
-blth module it.s not burnt , i use a voltage divider just in case(when it.s “paired” with laptop or phone in ways that i said earlier the led is stop blinking)
Sorry for waisting your time and for my bad english but i would appreciate a lot any help.
Tnxxxxxxx~~
Sounds like you have a BLE module. These don’t pair in the same way at the BT2 modules.
Try using an Android terminal app that supports BLE / BT 4.
See https://www.martyncurrey.com/bluetooth-modules/
as i tried before, my phone sees the blth module but using apps just for scanning is not helping that much. I tried the apps u mentioned in that link and with HMBLE Terminal connects but doesn.t recieve any data. I tried a code that lights up a led when press1 and goes off when 0.The program that i.m using has a default message that shows up (when i open serial monitor) on this app (i charge my board using laptop) which tells me that i miss smth else.
I tried to find any other apps but stiil can.t find one as this
What module do you have?
Does it have a blue LED top left?
What name does it have when searching on a phone?
it is exactly as AT-09 Bluetooth V4.0 CC2541 / HM-10 , first name was bt05 but i change it many times because AT+NAME was one of the fewest commands that i could send and work
The At-09 is Bluetooth 4/BLE with a Bolutek firmware.
Finnaly works.Thanks a lot for your article and advices.Keept it this way!!
I m using hc 05 for smartphone controlled car . It was working in good condition in beginning. But from last 2 to 3 days my bt module is not working properly. It’s not giving respone to my signals. It is paired and connected to my phone but it don’t recieve the signal please help.
check the connections. And then check them again.
I have a Samsung Chromebook, an HP envy running windows 10, a Toshiba machine with windows 7, two kindle fires and a couple of I-pods, none of which will let me access Google play store. Do you have some suggestions on another app, like Bluetooth Terminal, that will run on these machines, or on an I-phone?
How i can clear the receive data of hc-05 ?
The serial.available() It still retains the last command i sent from android .
The simplest way is to keep reading until there is nothing left.
while ( serial.available() )
{
c = Serial.read();
}
Thank you for the quick reply .
I did it exactly as you wrote .
Thanks in advance.
Not explaining myself well.
What I meant was that I got it right .
But when I read it again after a while , I expect to get serial.available() = 0
or empty buffer.
Thanks.
If you are not getting an empty buffer it means you are receiving data. Check the sending device, is it sending data?
Also make sure you are using serial.available() == 0 rather than serial.available() = 0.
Now it work well after adding Serial.flush() .
Anyway. Thanks a lot.
hello , all i need is how to send data from arduino to pc via bluetooth thats my app i havn’t found a solution yet welll any one can help !
Bluetooth on a Windows PC uses regular com ports so the connection is the same for Bluetooth as with usb,
To get started have a look at the Visual Basic examples starting with https://www.martyncurrey.com/arduino-and-visual-basic-part-1-receiving-data-from-the-arduino/
Hallo Martyn
i was using HC-05 with arduino nano and arduino Leonardo using the pins TX, RX on Arduino to connect with HC-05 and i used of corse voltage divider and connected Vcc to 5V on Arduino. I was using an android app i created to drive a dc motor via bluetooth. everything was working perfectly, suddenly i can connect to HC-05 successfully but cant send any Data from my phone to Arduino.. i didnt change anything niether in code nor wiring..what do you think is the problem ?
thanks
Try one of the basic serial in / serial out sketches with software serial on pins other than TX&RX and an Android terminal app and see if you can get it working.
Sir im doing project based on Bluetooth based home automation.the problem im facing is about Bluetooth (HC-05). im able to read data on serial monitor but it’s not controlling the home appliances. It would be helpful if u suggest something..tq u
I can’t help directly but have a look at the posts that deal with remote control. Start with https://www.martyncurrey.com/arduino-to-arduino-by-bluetooth/
my bluetooth module hc-05 connected with ardunio UNO and well paired with mobile phone but its not working , when i open serial monitor of ardunio and sends command 1 from mobile phone ,serial monitor shows βΈ® at every command.
what should I do……please help….??
This is almost always due to incorrect baud rate between the Arduino and the serial monitor.
Thank you very much for this post. This was the tutorial that worked for me. I was also able to use your sketch to customise my HC-05 with AT commands by editing 9600 to 38400 (in 2 places – a tiny troubleshooting exercise there).
I have successfully connected two hc-05 Bluetooth modules in master-slave mode. And they are working fine with reliable data exchange using ‘AT+LINK’.
Once I enter the Link mode, I am unable to come out of it. How can I come out of this mode as module doesn’t recognize any AT commands now? Do I need to use a termination character to break the link?
You need to bring the EN (or BRK) pin momentarily LOW to break the connection. Note though, if you have the modules set to auto connect they will simple reconnect on power on.
I’m using the hc 05 module with a connection to arduino for a bluetooth control robot. The issue I’m facing is that the first time i paired with the module, it easily paired and connected to the application. I accidentally reset the module by pushing the small black button on the module and now it won’t connect to any application and will only pair with the phone.
Can you please suggest a solution?
Please and Thankyou.
Not sure what you mean by reset.
The small push button connects pin34 to HIGH and puts the module in AT Command mode. Cycling the power to the module starts the module in communication mode again.
In sketch2 ,In serial monitor my msgs are shows such square symbol so.So what is the problem,plz reply fast
Hello Martyn
After reading some of your excellent articles I purchased three HC-05 modules to experiment with. They all seem to work OK but switch off after 5 seconds. The power has to be removed for at least 30 seconds and then re-applied, then they work for another 5 seconds before shutting down. AT commands seem to work but you have to be quick! Is there an AT command I haven’t yet come across that will cause them to stay on? SW version is 3.0 – 20170601. Thanks for your help.
hi i ordered a kit off of amazon that included this and it claimed that this caold use ir signals. is that true and if it is, how?
I have gone through your instructions and connected circuits as told. But after connection,though led is blinking rapidly but HC-06 is not discovered in app and it simply says “No device found”.
I was having the same problem that many others in these comments have encountered – I could send from serial to bluetooth but not the other way.
In my case I finally figured out that the problem was I had not tied all my GNDs together. My Arduino was running from USB power, while I had the HC-05 and the ground of the voltage divider coming from a separate power supply. As soon as I added a link between the GND of the Arduino and the GND of everything else, it started working.
I hope this might help someone else who runs into this problem!
Another issue I found was that Bluetooth Terminal would not find the HC-05 when it was unpaired. I had to pair it first in my phone’s Bluetooth settings, and only then would the Terminal connect to it.
Thanks for the comment. Since this post is about getting the HC-05 working I don’t go in to detail about connecting grounds. It is mentioned in the connections list but I now realise that it is not explicitly stated. I will add a comment to say the GNDs need connection. The above does say you need to pair before you can make a connection though.
please do not provide … I repeat…DO NOT PROVIDE any information about the serial library you use
this could actualy be helpfull and make sens…but, for f@#k sake, DO NOT DO THAT
A full serial post is in the works. It has been in the works for a long time and I keep adding to it when I have time but it is not ready yet.
Dear Martin,
I have been working on my project with Arduino for over one year and a half. Since then, I’ve been facing issues with the use of bluetooth (HC-05) and GSM (SIM 800L), because when the gms send sms, the bluetooth module would automatically loose connection with my cell phone. Reading this article of yours, I suspected that the HC-05 was actually restarting to protect itself against current overload (since the RX port works with 3.3V and not 5V that Arduino works). I bought the resistors and put it as you suggested here, and voila!!! It worked perfectly!!!! I’m really thankful for your help!!! Regards From Brazil!!!
I do not see “Bluetooth Test” in the Bluetooth terminal app. Any suggestions?
Hi! I am using arduino uno, hc 05 Bluetooth module and P10 DMD. However I cannot display more than 60 characters when I type 100 characters from the phone. What might be the problem?
Martyn,
Can HC-05 reduce the connectivity range?
As I know, HC-05 is Class-2 Bluetooth module which has nominal range of 10 meters.
I want to reduce it by only 2 meters. Do you have any idea?
Great article. I was interested in the basics of WIFI and Bluetooth and kept getting confused. I have read a lot about it from the Internet also and wanted to know more about it.
It’s actually a great and useful piece of info. I’m happy that you simply shared this helpful information with us.
Please stay us up to date like this. Thank you for sharing.
Hey, I have been using this method to configure following things on my bluetooth module.
AT>OK
AT+VERSION? > V2.0-20171015
AT+NAME=abcd 123 > OK
AT+POLAR=1,0 > OK
AT+UART= 57600,0,0 >OK
It was completely working for a long long time now that I have got a different bluetooth modules its not working now. I suspect it’s because of the version change. Now I am unable to make it work in case of AT+POLAR? its always returning ERROR(0) due to the version change.
AT+VERSION?
+VERSION:4.0-20190815
OK
Anyone has any idea on the AT Commands for Bluetooth V4.0 (NOT BLE)
Anyone managed to get a Arduino Mega to talk to a HC-05 using Sketch in a Windows 10 x64 environment ?
Despite being 100% in AT Mode and connected correctly I simply cant get ANY response to an AT Command in Serial Monitor.
Tried all Baud rates (especially 38400 & 9600), CR & LF selected, correct board/processor, correct port but nothing (even entering the commands whilst holding the button on the HC-05 board). ANY IDEAS ??