Arduino can communicate with other device via Bluetooth using the module HC-06 (slave). It enables the Arduino to be connected and exchange data with other devices such as Smartphone, computer or other microcontrollers. Bluetooth communication can be used to control a robot remotely, Display and store data on your computer or on your smartphone, for instance.
Prerequisite: Arduino Serial communication
Material
- Computer
- Arduino UNO
- USB cable
- Bluetooth module HC-06
- Dupont cables M/F
Module HC-06 overview
The Bluetooth module HC-06 has 4 pins, 2 for power and 2 to establish connection.
- VCC power supply. Typically hooked up to 5V pin of the Arduino.
- GND ground. Typically hooked up to GND pin of the Arduino
- RX reception pin. Typically hooked up to transmission pin (TX) of the Arduino
- TX transmission pin. Typically hooked up to reception pin (RX) of the Arduino
Check the RX/TX pin of your Arduino board
N.B.: Since the module HC-06 is a slave module it cannot connect to other device on its own. To do so you need a master module such as the Bluetooth module HC-05.
Schematics
Some HC-06 modules operate at 3V3 and do not support the 5V voltage level on pin Rx. In this case, a voltage divider bridge is required to convert the logic signal (1k resistor between pin3 and Rx, and 2k Ohm between Rx and GND).
WARNING : We use pin 2 and 3 of Arduino Uno. Depending on the microcontroller, some pins may not support SoftwareSerial communication. Especially, Arduino Mega, Micro and Leonardo. Check the documentation.
Module HC-06 configuration
Configuring the module HC-06 can be interesting to verify that it is working, hooked up correctly and to modify its parameters such as its name (useful when your are using several modules), PIN code and communication speed (baudrate). To allow configuration, the module HC-06 should be powered but not paired (la LED is blinking).
The following code allows you to modify the parameters using the serial monitor.
#include <SoftwareSerial.h> SoftwareSerial hc06(2,3); void setup(){ //Initialize Serial Monitor Serial.begin(9600); Serial.println("ENTER AT Commands:"); //Initialize Bluetooth Serial Port hc06.begin(9600); } void loop(){ //Write data from HC06 to Serial Monitor if (hc06.available()){ Serial.write(hc06.read()); } //Write from Serial Monitor to HC06 if (Serial.available()){ hc06.write(Serial.read()); } }
To test serial communication, enter AT in the serial monitor and click on the send or press enter. Be sure to select “No end line” and the correct baudrate in the communication options. Module should answer OK. If it is not working check the wiring and the module version.
To modify the module name, enter AT+NAMEmodule_name.Module should answer OKsetname. (Ex: If you want to change module name into BTM1 enter AT+NAMEBTM1)
To modify the module PIN code, enter AT+PINxxxx. Module should answer OKsetPIN. (Ex: If you want to change PIN into 0000 enter AT+PIN0000)
To modify the module communication speed (only if required), enter AT+BAUDx. Ex: If you want to change baudrate into 9600 enter AT+BAUD4. Module should answer OK9600. (Note: 1 for 1200, 2 for 2400, 3 for 4800, 4 for 9600, 5 for 19200, 6 for 38400, 7 for 57600, 8 for 115200)
WARNING:Different versions of the Bluetooth module HC-06 exit and the list of AT commands may vary. Check the serial number written on the module and the firmware version by entering AT+VERSION.
For instance, the module HC-06 labelled ZS-040with firmware version 3.0-20170609 returns ERROR(0) when sending command AT+NAMExxxx (with xxxx the new name of the module). The AT commands to configure such module are:
- AT+NAME=xxxx to set the name of the module
- AT+PSWD:”xxxx” to set the pin code of the module
Do not hesitate to leave a comment if you encounter an issue while configuring your Bluetooth module HC-06.
Pairing Bluetooth module HC-06
Once the module is configured as you wish, you can pair the module HC-06 to the device of your choice like any Bluetooth device. Select the name of your module in the list of available Bluetooth device (default is HC-06) and enter the PIN code (default is 1234). When it is done, The LED should stop blinking.
Code
After your module is paired, you can modify the following code to obtain the desired functionality. In this example, we expect the other device (such as an app on smartphone) to send the command ON or OFF to activate a function on the Arduino.
To handle the module HC-06 we use the library SoftwareSerial.h which allows to define Serial port on the Arduino board. Functions to be known are:
- SoftwareSerial hc06(Rx,Tx) to define the pins of the serial port
- hc06.begin() to define the baudrate (value should be the same as your module)
- hc06.available() to test if data are available in the buffer of the module
- hc06.read() to read data one byte at a time
- hc06.print() to send a string in ASCII form
- hc06.write() to send data one byte at a time
#include <SoftwareSerial.h> SoftwareSerial hc06(2,3); String cmd=""; float sensor_val=0; void setup(){ //Initialize Serial Monitor Serial.begin(9600); //Initialize Bluetooth Serial Port hc06.begin(9600); } void loop(){ //Read data from HC06 while(hc06.available()>0){ cmd+=(char)hc06.read(); } //Select function with cmd if(cmd!=""){ Serial.print("Command recieved : "); Serial.println(cmd); // We expect ON or OFF from bluetooth if(cmd=="ON"){ Serial.println("Function is on"); }else if(cmd=="OFF"){ Serial.println("Function is off"); }else{ Serial.println("Function is off by default"); } cmd=""; //reset cmd } // Simulate sensor measurement sensor_val=(float)random(256); // random number between 0 and 255 //Write sensor data to HC06 hc06.print(sensor_val); delay(100); }
Bonus: Baudrate scanner
If you have difficulties finding your module baudrate, here is a code that initialize the Bluetooth and send AT command for each baudrate value.
#include <SoftwareSerial.h> SoftwareSerial hc06(2, 3); void setup() { //Initialize Serial Monitor Serial.begin(9600); // scan scanBaudrate(); } void loop() {} void scanBaudrate() { unsigned long bauds[12] = {300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000}; for (int i = 0; i < 12; i++) { hc06.begin(bauds[i]); delay(10); Serial.print(F("Baudrate ")); Serial.println(bauds[i]); command("AT", 2000); } } String command(const char *toSend, unsigned long milliseconds) { String result; Serial.print("Sending: "); Serial.println(toSend); hc06.print(toSend); unsigned long startTime = millis(); Serial.print(F("Received: ")); while (millis() - startTime < milliseconds) { if (hc06.available()) { char c = hc06.read(); Serial.write(c); result += c; // append to the result string } } Serial.println(); // new line after timeout. return result; }
When the communication baudrate is set correctly, the Bluetooth module should answer OK. This code will quickly tell you if the module is working properly and what baudrate it uses.
Application
- Communicate between two Arduino via Bluetooth
- Control your robot with your Smartphone
- Display temperature measurement on your Smartphone
- All our robots can be controlled via Bluetooth
Source
- Communicate with Arduino
- SoftwareSerial Example
- Bluetooth Module HC-06
- Create an application using App Inventor 2
Find other examples and tutorials in our Automatic code generator
Code Architect
Thanks, great information!
Hello, I tried a lot to get my HC06 Module paired with my smartphone but I have no luck. With AT Mode (and Arduino Uno) I get the version hc06V2.2_le. I can set the BAUD rate, I can change the name of the module. One Problem is I cannot change the pin code. If I use AT+PIN1234 no reaction also AT+PSWD=”1234″ does nothing.
When I turn on BT on my smartphone I can see my HC06. The first try to pair gives me the message “please go to the app of your device to pair it”. When I try again, the pairing dialog of the smartphone opens and I can type in the code. I tried 1234, 0000 and may others but always the smarphone gives the response “There was an error pairing with the device”. I am so helpless and it would be great anyone knows a solution.
I build a robot car with pupils. The car can be controlled over BT with an android app (with RFO-BASIC). An older HC06 module is working fine.
When I connect the bluetooth HC-06 module to Arduino, my module is not detected by the smartphone,
where is the error?
Hello, how do you connect HC-06 to Arduino? try to power the HC-06 without the Rx/Tx pin hooked up at first. What is the module Led doing?(ex: blinking every 2 secs)
Is the module detected by your computer?
ATENTION !!!!!
RX LINE NEEDS to have a VOLTAGE DIVIDER.
In other way, HC-06 will be DESTROYED as this pin only acepts 3.3v
2K2 from HC-06 PX pin to GND and
1K serial from Arduino’s PIN3 to node 2K2.
ARDUINO P3 —[ 1 K ]—–+——— RX HC-06
|
-+-
| 2 |
| K |
| 2 |
-+-
|
|
GND
I hope it’s clear enought.
2K2 must be connected at MIDDLE POINT where “PLUS” simbol between !k and HC06, not directly to Arduino…
I used SPACES to format the text and it didn’t work fine.
It depends on the module that you have. Some HC06 modules accept to be connected directly to Arduino.
It is true that it’s advised to put a voltage divider on the Rx pin
My Modul HC 06 Not Detected In Smartphone
What smartphone are you using?
Hi. I’m with the same problem. I turn the HC-06 with a micro USB breakout board (5v) and without the rx and tx pins. The led is flashing, waiting for pair, but my module is not detect by my smartphone.
I didnt any setup, only conect the modules and try conect. Can be this the problem?
This is the imagem from my circuit test: https://ibb.co/H2jWNtB
Hello, is it detected by your computer?
@Vinicius in you image, it looks to me like you do not have power connected to you HC-06.
???
Hi Terry,
HC-06 is connected to GND and 5V of Arduino
Hc-06 voltage level of Rx,Tx is 3v
would it work with arduino UNO.?
It worked for me in several projects. However, I should warn you that it is often advised to add a voltage divider to adapt voltage from Arduino(5V) to HC06(3V)
I can change the name and password on my HC-06 using AT commands . When connected to 3.3V it blinks quickly. When I use my android phone to pair, phone says it’s paired but it still blinks quickly… I was expecting it to blink slowly…. and I don’t think it’s connected properly. I read that it has 2 modes AT and command- there is an EN pin … do I need to use this to change from AT mode ? This is so frustrating… looks so easy on YouTube:-(. Any help you can give would be really appreciated
Hi,
You can try connecting the module to 5V. Which application do you use to connect the phone to the Bluetooth module? You shouldn’t have to use the EN or Key pin (it should be low or disconnected).
Hi, did you find any solution? That’s exactly what happens to me :c
I found the commands for my version 3.0-201706709 to change the name and pin, thanks. I need to change the baud rate to 115200.
To change baud rate I found this:
The new requirement of CR+LF was one reason why it got such a long time from me to find out why the HC06 is not responding to AT commands, and later on why it doesn’t accept the AT+BAUDn command. After getting the version read (it was 3.0-20170609), some googling got me on the track. It appeared that the AT+BAUD9 command had to be replaced by AT+UART=230400,0,0 (& CR+LF) in order to get the baud rate changed.
Here:HC-06 version 3.0 detection by the CurrentRanger (lowpowerlab.com)
Thanks a lot for this helpful comment!
Thanks. I spent too much time to find the solution for ERROR(0),
hi, I am testing with HC06 and Arduino Nano, using resistors for voltage reduction on RX and running your sketch to detect baudrate. the console never prints “OK” for any of the options!
the device appears in my laptop’s bluetooth settings, it accepts pairing, shortly says “connected” but one second later it says “disconnected”.
Red led in HC06 blinks quickly all the time.
some idea? is there some way to diagnose damage in the chip? thanks in advance!
You might need to change the end of line (carrier return, new line or both CR and NL) on the serial monitor
Hi! the “on and off” part of the code is working perfectly fine but hc06.print() is not working so I can’t send data to my phone. do you have an idea why this might be happening? thank you!
What application are you using on your phone?
can you show me where to put the pins on an expansion shield
You need to find where the pins 2 and 3 are on you’re expansion shield
Hello there, I’m trying to use a HC-06 with an Arduino Micro. I managed to get OK from AT, but that was it. I’m trying to send a keyboard press via Bluetooth to the computer when I hit a button, but I can’t seem to get any data transmitted, although I can connect the computer to the module, using the name and PIN I set.
VCC is connected to the 3V pin, and RX, TX to 9, 8 (according to the documentation on Micro, these pins should work). Any idea what could be wrong? Thank you!!
Hello,
The other AT commands work as well? How do you send the key press signal to the bluetooth module? How do you check that the signal is received on the computer side?
my hc-06 pairs with my laptop but it doesn’t connect to it , i can’t see the hc-06 on any comport ,
i connected everything as the schematics show
Hello, did you try some AT command to check the HC06 status? What software or code do you use to receive or send data over bluetooth?