Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Easy Method Using CMODE

Making a connection Between a HC-05 and a HC-06: Method 1

Using the CMODE command we have an easy way to connect the HC-05 and the HC-06 (or 2 HC05s). When the HC-O5 is configured to pair with any address (AT+CMODE=1) it should connect to a Slave module automatically. No binding etc is required.

I am using the zs-040 modules with firmware 2.0-20100601 and other modules with the same firmware will be the same. If you have issues check the data sheet for your module.

The Set Up

I am using 2 different Arduino IDEs; version 1.0.5 and version 1.6.3. This gives me 2 separate serial monitors. The Arduino connected to the HC-05 is on COM8 and the Arduino using the HC-06 is on COM17

ARD2ARD_SETUP_IDE

On the HC-05, I have changed the baud rate used in the communication mode to 38400. This means I do not need to change anything when switching between AT mode and Communication mode.

I am using the HC-05_AT_MODE_01 sketch on the Arduino connected to the HC-05 and the HC-06_01 sketch on the Arduino connected to the HC-06

HC-05_AT_MODE_01

// Basic Bluetooth sketch HC-05_AT_MODE_01
// Connect the HC-05 module and communicate using the serial monitor
//
// The HC-05 defaults to commincation mode when first powered on you will need to manually enter AT mode
// The default baud rate for AT mode is 38400
// See www.martyncurrey.com for details
//
 
 
#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");
    Serial.println("Remember to select Both NL & CR in the serial monitor");
 
    // HC-05 default serial speed for AT mode is 38400
    BTserial.begin(38400);  
}
 
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);  
    }
 
}

HC-06_01

// Basic bluetooth test sketch. HC-06_01
// HC-06 ZS-040 
// 
// 
//  Uses hardware serial to talk to the host computer and software serial for communication with the bluetooth module
//
//  Pins
//  BT VCC to Arduino 5V out. 
//  BT GND to GND
//  BT RX to Arduino pin 3 (through a voltage divider)
//  BT TX  to Arduino pin 2 (no need voltage divider)
//
//  When a command is entered in the serial monitor on the computer 
//  the Arduino will relay it to the bluetooth module and display the result.
//
//  These HC-06 modules require capital letters and no line ending
//
 
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX | TX
 
void setup() 
{
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
 
  // HC-06 default baud rate is 9600
  BTSerial.begin(9600);  
}
 
void loop()
{
 
  // Keep reading from HC-06 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());
 
  // Keep reading from Arduino Serial Monitor and send to HC-06
  if (Serial.available())
  BTSerial.write(Serial.read());
}

Connections

Both Bluetooth modules are connected in the same way

ARD2ARD_BreadBoard_01_1200

HC-05 to HC-06 Easy Method Using CMODE

Make the 2 circuits and power on. The LEDs on both the HC-05 and the HC-06 should be blinking quickly.

To get the HC-05 to connect to the HC-05 we need to
– set the HC-05 as a master device
– configure to pair with any address
– cycle the power to the module

Put the HC-05 in to AT mode (hold the button switch closed while powering on). If you are not sure how to do this see Arduino with HC-05 (ZS-040) Bluetooth module – AT MODE. When in AT mode the LED on the HC-05 should blink on/off every second.

First, check that we are in AT mode. Enter AT and hit Send. You should get “OK”
ARD2ARD_Bluetooth_01_AT

Set the HC-05 as a master device with the AT+ROLE command: AT+ROLE=1. You should get another “OK”
ARD2ARD_Bluetooth_02_AT+ROLE

Configure the HC-05 to pair with any address: AT+CMODE=1. You should get yet another “OK”
ARD2ARD_Bluetooth_03_AT+CMODE

Cycle the power to the HC-05. The LED on the HC-05 will blink twice a second as it searches and once it has connected with the HC-06 the LED will quickly blink twice every 2 seconds or so. The LED on the HC-06 should be constant on.

Now, what every you enter in one of the serial monitors will be sent by Bluetooth to the other Arduino and then displayed in the opposite serial monitor.
ARD2ARD_Bluetooth_03_Hello01

ARD2ARD_Bluetooth_04_Hello02

Hello sent from the HC-06 to the HC-05
ARD2ARD_Bluetooth_05_Hello05

ARD2ARD_Bluetooth_06_Hello04

 
 

Next

The next step is to link 2 modules by specifying which one you want to link to. You do this using the mac address. Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link

 
 

87 thoughts on “Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Easy Method Using CMODE”

  1. Hi Martyn,

    I am really enjoying your flurry of BT blogs. I am setting up a bluetooth connection and have received a lot of help through your efforts. Thank You.

    Did you ever notice you can run several instances of the same version of the Arduino IDE simply by starting the application a second or third plus times? This has worked since before 1.0. I remember??? It works are the current versions too. I’ve been doing this without thinking about it for quite a few years.

    Reply
    • Good to hear the blog is useful and thanks for your comment.

      I started using 2 separate IDEs a while ago due to issues I had having 2 serial monitors open at the same. This became a habit and ever since I never thought to try running multiple instances of the newer IDEs. Thanks for the tip.

      Reply
  2. Hi Martyn,

    Thanks for this post. It is great. I am new to this bluetooth thing and I have some questions.

    I was able to connect the HC 05 (master, cmode=1) to android (slave) using AT+PAIR command. Before they were paired, the android pops up a screen to ask for the password (1234). I typed it and they were paired.

    Looked your post, I don’t see anything about providing password when connecting hc-05 to hc-06.

    Can you provide me more info ?

    Many thanks

    Frank

    Reply
  3. CMODE=1 allows the HC-05 to pair with any device. I can’t remember if the passwords have to be the same though (I suspect they need to be) and I am traveling at the moment so can’t confirm. Try a test. Set the the HC-05 and the HC-06 to have different passwords and see if they still connect. Report back what you find.

    Reply
  4. Hi Martyn!

    I really enjoyed working on your setup. Its an awesome tutorial for starters like me. Thanks for the post. I was just wondering can I connect 2 master HC05s or 2 slave hc05s only? I mean can I configure both as either master or slave and still initiate communication between both of them?

    Reply
    • Bluetooth connections always require a Master/Slave combination with the Master device initiating the connection.

      The HC-05 can be used as a Master or a Slave device and so 2 HC-05s can be used (one must be set as a Slave though). The HC-06 is a Slave only device and can only accept connection requests, it cannot make them and so will always need a Master device to connect with.

      Reply
  5. Hi Martyn!
    I shorted pin 34 with EN pin. But now I cannot see my LED on BT module blinking 5 times a second by providing digital LOW to EN pin. Anyhow, if I provide digital HIGH to EN pin it blinks on/of f every second. What am I doing wrong here? How can I control configuring BT module either in Command mode or in Data mode as per my wish through my Arduino Code. Kindly help!

    Reply
  6. AT+CMODE=0 means connect to one slave module, AT+CMODE=1 means what ,

    can we connect more than two slave at the time with master . ?

    Reply
    • AT+CMODE=0 mean connect to a specified address (specific BT module) only
      AT+CMODE=1 means the module can connect with any BT device.

      You can only make one connection at a time. If you want to connect to several BT modules you need connect one at a time.

      Reply
  7. hi, I’ve managed to get two hc05’s paired, one on a Leonardo as a master and one on an Uno set as slave. they seem paired ok.
    what I was wanting to do is send variable values that have been calculated on the Leonardo across to the slave (Uno) to control a motor.
    have been looking for some sketches to get an idea of the send / receive commands between the two but no luck. I’m new to arduino and have only been learning for a few weeks, any idea where I can learn specifically on this? every search I do comes up with how to pair modules and not how to use them once paired….

    Reply
    • It would be better to sort out the communication side using a wired connection first. Once you have this working add the BT connection.

      Think about how you want to format the data,not just for sending but also how to make it easier for receiving. For example I generally use ascii formatted data as this makes the code on the receiving end a little easier. It is not the fastest way to send numeric data though.

      Start with Arduino Serial Basics at http://forum.arduino.cc/index.php?topic=288234.0
      This thread is very useful and Robin’s recvWithEndMarker() function forms the starting point for all my projects that use serial communication.

      Reply
  8. Hello. Your blog has been really helpful in my project. I am working on connecting two micro-controllers using bluetooth. For that I want to light a LED connected to one arduino by giving command to the other one. Is that possible?

    Reply
      • Thanks for the help. I have successfully been able to light an LED by connecting a button to one arduino and the LED to another. But Can you suggest some real life application of such connection between two arduinos?
        Thank you

        Reply
        • You could use two microcontrollers to create an app in which one microcontroller collects certain data and sends it to your phone. The other microcontroller could collect another variable of information and send it to your phone. Thus making it possible to create an app in which you could connect the microcontrollers and send all information to you at once, or each device relays at its own pace. By the way could you commet/post your sample code to connect two microcontrollers? Thanks!

          Reply
  9. Actually , i want to pair the two HC-05 bluetooth Modules but according to your site , USB to TTL convertor has been used . How can we pair the 2 bluetooth modules withou using USB to TTL Convertor? Can pairing of bluetooth be done with help of AT commands ?

    Reply
    • To use AT mode you need to communicate with the BT modules by serial. This means using a USB to serial converter. This can either a dedicated device or an Arduino with a basic sketch.

      You do not need to have both BT modules connected by serial at the same time.
      Connect device 1, set as a slave, set up the password and baud rate etc, then connect device 2 and set up as a Master.

      Reply
  10. Thanks for the help. I have successfully been able to light an LED by connecting a button to one arduino and the LED to another. But Can you suggest some real life application of such connection between two arduinos?
    Thank you

    Reply
    • It’s hard to suggest an application and most of my projects are things I do simply to learn how to do them.

      I do have a a turtle lamp that is controlled by an Arduino. This normally works on a timer but has a Bluetooth controller for manual over-ride.
      I also have a Bluetooth LCD. This is simply an Arduino connected to a 4×20 LCD and a Bluetooth module. I then display things on the LCD from remote Arduinos. No real application and I usually use it for debugging.

      Reply
  11. Hey Martyn,
    This post is really useful. I am trying to use Bluetooth to transmit a potentiometer value between two arduino nanos. I followed this guide to setting up the modules however when I type “Hello”, for example, it gives “zú”. On the HC-06 my serial settings are No Line Ending and 9600 baud. The HC-05 Both NL CR and 38400.
    Many Thanks

    Reply
  12. Bonjour à tous,

    J’essaye de connecter 2 arduino uno entre eux par le biais de 2 modules bluetooth HC05 et HC06.
    En essayant avec le tuto ci dessus ça ne fonctionne toujours pas.
    Peut etre ne suis je pas le premier …
    Quelqu’un pourrai m’aider svp?

    Reply
  13. Thanks for this, been a great help. Do you know why the receiving unit is showing symbols instead of the text that has been inputed by the transmitting unit?

    ie typing TEST comes out as øøx€x€xøxø

    Reply
  14. Hi,if the default baud rate of hc-06 is 9600 as the previous article you post,then I cant send right data to each other BT,I also check MY default baud rate ,is also 9600.
    So I change the BTserial.being() to 9600 ,so it works.
    I still want to make a sure,do you use 38400 or you change the baud rate already?

    Reply
  15. Hi,
    We are trying to connect to 2 arduinos with their respectives bluetooths. We already pair the bluetooths but we can’t send information from one arduino to the other one.
    We have an arduino uno with one bluetooth and an arduino mini with another bluetooth, and we want to send information from one arduino to the other one so it writes the values into an lcd screen. We´ve been trying to do it for weeks but we can’t find a solution and i was wondering if you could help us. Bc we tried with your code but it doesn’t work and off all the possible examples we looked for yours is the better.
    Thank you very much we´ll really appreciate it

    Reply
  16. 12/26/16

    Martin:
    I have been working with an HC-05 now for two days and have not been able to get to the bottom of my problem.

    First, I can talk to the HC-05.
    The firmware version is 2.01-20100601. I see that you have dealt with that before maybe you can help.

    I can get the HC-05 to talk both directions if it is in the Slave mode.

    When I put it in the master mode, it will only receive but will not transmit.

    In looking at all of the documentation I can find, I see that some of the documentation lists a “AT+MODE” function. The documentation states that if the Parm= 0 it equates to receive only, if the Parm= 1 it will to transmit only and if Parm= 2 the devise will communicate in both directions . Sounds simple, put the HC-05 in Mode 2 and go. My problem!! The HC -05 will not respond to the AT+MODE command. It comes up with an ERROR =0. I have found there are several of the listed commands that the HC-05 won’t answer to one of them being the AT+HELP command.

    What are you thoughts on the situation??

    Many thanks for your input in advance

    Ken

    I can

    Reply
  17. Hey Martyn
    I have been trying to pair two HM10. I am using Arduino Uno both sides. I tried bit there has been no success.
    Thank you

    Reply
  18. I have a Master (Arduino + Bluetooth module HC-05) and several Slaves (Arduino + Bluetooth module HC-05). I know the MAC address of all Bluetooth devices. How do I programmatically assign the MAC address of the device to which I want to connect.
    AT-command AT-BIND is not suitable, because I want to choose which device I want to connect to at the moment.

    Reply
  19. Hello Arduino Community, I am connecting two bluetooth modules, and i can see that they are pairing because of the LED lights. When I try sending any data, it returns an “unknown character” sign(the question in the black box).
    I am using this code on both arduinos:

    // Basic Bluetooth sketch HC-05_AT_MODE_01
    // Connect the HC-05 module and communicate using the serial monitor
    //
    // The HC-05 defaults to commincation mode when first powered on you will need to manually enter AT mode
    // The default baud rate for AT mode is 38400
    // See https://www.martyncurrey.com for details
    //

    #include
    SoftwareSerial BTserial(10, 11); // 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(“Remember to select Both NL & CR in the serial monitor”);

    // HC-05 default serial speed for AT mode is 38400
    BTserial.begin(38400);
    }

    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);
    }

    }

    Thanks in advance for your help

    Reply
  20. Hi, this is great; however, I have done everything, but when I send a message from -05 to -06, the serial monitor of the -06 reads ?????. Same amount of ? as letters sent. Also, the -06 wont send a message to the -05 and the LED on the -06 blinks around every 4 seconds.

    Reply
  21. Hey Martyn,
    I have followed this tutorial and have successfully connected them. I want to be able to press a button on one arduino and in turn turning off a doorstrike on the other. How would i do that? as i really have no clue how to code using the arduino. I wouls also like to implement a timer to allow the person pressing the buttono time to enter the door.

    Any help is appreciated.

    Reply
  22. Hey I’m back,
    As said in my previous post above I am using a 12v door strike. Would the use of a relay for the doorstrike affect the code? If so, what do I need to add.

    Reply
  23. Here is my code for the HC-06 you may compare the code from the HM-10 tutorial as i pulled code form there:
    #include

    char c=’ ‘;
    byte LEDpin = 4;

    void setup()
    {
    Serial.begin(9600);
    Serial.println(“Enter AT commands:”);

    // HC-06 default baud rate is 9600
    BTSerial.begin(9600);

    pinMode(LEDpin, OUTPUT);
    digitalWrite(LEDpin,LOW);
    }

    void loop()
    {
    if (BTSerial.available())
    {
    c = BTSerial.read();

    // 49 is the ascii code for “1”
    // 48 is the ascii code for “0”
    if (c==49) { digitalWrite(LEDpin,LOW); }
    if (c==48) { digitalWrite(LEDpin,HIGH); }
    Serial.println(c);
    }
    {

    if (BTSerial.available())
    Serial.write(BTSerial.read());

    if (Serial.available())
    BTSerial.write(Serial.read());
    }
    }

    Reply
    • ****EDIT****

      #include

      char c=’ ‘;
      byte LEDpin = 4;

      void setup()
      {
      Serial.begin(9600);
      Serial.println(“Enter AT commands:”);

      // HC-06 default baud rate is 9600
      BTSerial.begin(9600);

      pinMode(LEDpin, OUTPUT);
      digitalWrite(LEDpin,LOW);
      }

      void loop()
      {
      if (BTSerial.available())
      {
      c = BTSerial.read();

      // 49 is the ascii code for “1”
      // 48 is the ascii code for “0”
      if (c==49) { digitalWrite(LEDpin,LOW); }
      if (c==48) { digitalWrite(LEDpin,HIGH); }
      Serial.println(c);
      }
      {

      if (BTSerial.available())
      Serial.write(BTSerial.read());

      if (Serial.available())
      BTSerial.write(Serial.read());
      }
      }

      Reply
  24. Hi,can i control using one master to multiple slave, i use several button in master ,when click one button ,then corresponding slave active and give response,can it possible,please help me.

    Reply
    • This is kind of possible using BT 2 but it is not ideal.

      With BT 2; the master can only connect to one slave at a time. This means to use more than one slave you need to disconnect the current connection, then connect to the new slave.

      Reply
  25. Hi there, quick thing. I managed to have the modules communicating. However I am sending a char value from the HC-06 and it is printed wrong in the serial of the HC-05
    0 is printed as €
    1 is printed as ø

    the Serial monitors of both module’s sketches are set to 9600 and both the BTSerial are 38400 and I am using the following to send/read the value

    //write char value on HC-06
    BTSerial.write(‘0’);

    // read on HC-05 // c is type char
    c = BTserial.read();
    Serial.println(c);

    what could be the issue? thanks!

    Reply
  26. Hi
    I’ve followed your guide but when I’ve tried to connect the -05 and -06 it doesn’t work. For the -05 I’ve typed AT+CMODE=1 and got the response ok. I then unplugged the power source and then plugged it back in but they’re still not connecting,
    I was wondering if you’ll be able to help?

    Reply
  27. I followed all the steps and the connection is established automatically when i power up both arduinos. The baud rates on both devices is same. However when I try to send messages they dont get replicated in the other serial port. I get funny characters instead. How do I solve this?

    Reply
    • This is likely a mismatch between baud rates. There are a couple of places where they need to match:
      – software serial and the BT module
      – hardware serial and the serial monitor

      If using HC-05s, remember that they use 2 different baud rates. One for AT Command mode and a different one for data mode.

      Reply
  28. im having a few difficult problems. the BT moduel does not respond to my AT command at all. i have followed the instructions to the best of my knowledge. i have a video where you can see exactly what im doing. it does not let med show it here how ever. if i could get an email adress i could send it to i would be greatfull.
    contact me on: fridtjof.hals@hotmail.com

    Reply
  29. Hi Martyn (and everybody else),

    is it mandatorily necessary to cycle the master´s power to pair him the first time with a new slave?

    Though I resetted both devices (AT+ORGL), deleted the List of authorized devices (AT+RMAAD), set one as master (ROLE=1) and both to CMODE=1, they do not pair, unless I cut master from the power.

    Is there any AT command to make he master pair with a new slave without cycling the power? This would save me a lot of hardware modifications…

    Thank you in advance,

    Daniel

    Reply
  30. Martyn,
    I’m at my wits end. Please let me explain, I’m a 73 year old gent that has gotten into Arduino and BT. I have several HC-05s and I can not get them to pair with my phone when in Master mode (AT+ROLE=1). However if I go back and set AT+ROLE=0, my phone will pair.
    I have been trying to this for what seems an eternity.
    Can you or anyone else tell me what I’m missing?
    Thanks in advance for your time and thanks for all the info you have posted on the HC-05.
    Best,
    Lou Mancuso

    AT > OK
    AT+ORGL > OK
    AT+RMAAD > OK
    AT+VERSION +VERSION:2.0-20100601 > OK
    AT+ADDR +ADDR:98d3:b1:fd85b0 > OK
    AT+NAME=Master1 > OK
    AT+UART +UART:38400,0,0 > OK
    AT+ROLE +ROLE:0 > OK
    AT+ROLE=1 > OK
    at+CMODE? +CMOD:0 > OK
    AT+BIND=98d3,11,fc6c78 > OK

    Reply
    • This actually sounds right.

      Two master devices will not pair unless one of them auto switches to slave mode. The ability to auto switch is not very common and not available on slightly older versions of Android.

      Android devices tend to be in master mode by default and generally require software for them to act as slave devices.

      Reply
  31. Bonjour Martyne,

    Je tente actuellement de relier via BLE 2 Arduino Uno R4 WIFI chacun équipé d’un bouton et d’une Led.
    Le bouton de l’un doit allumer la Led de l’autre.
    Cela fonctionne dans un sens mais pas dans l’autre :-(
    Pourrais-tu m’apporter un éclairage à ce sujet stp ?

    Reply

Leave a Comment