Making HC-05 Bluetooth TX/RX Module work with Arduino Uno

My second post to the site. Woo Hoo! Ok let's get down to business. Finding the proper material to get the HC-05 to work was difficult, but it has finally paid off for the most part. Here's a breakdown of what I learned and how to get it to work.

WIRING the HC-06 to ARDUINO:

The HC-05 has 4 Pins (VCC, GND, TXD, and RXD). I'm using the version that does NOT have a button. Apparently there are 3 different versions! D: Not to panic. This should work for them all. Hopefully...

  1. Connect GND to GND on the Arduino.
  2. Connect VCC to +5V on the Arduino.
  3. Connect TXD to Pin 10 on the Arduino. Next Step is Tricky.
  4. Connect RXD to both GND and TX 1 using a 2.2K Resistor and 1K Resistor, respectively. I did this by connecting a lead to GND with a 2.2K and a lead to Pin 11 with a 1K, then connecting the free ends of the resistors to a lead connected to RXD on the HC-05.

Plain English:
(GND) --> GND
(VCC) --> +5V (Later +5V will change to Pin 7)
(TXD) to Pin 10 (Later Pin 10 will change to RX 0)
GND --> 2.2K Resistor --> (RXD) <-- 1K Resistor <-- Pin 11 (Later Pin 11 will change to TX 1)

Don't ask how this works. It just does. XD

CODING with your HC-05

The first thing you should know is that working setting this unit up can be a pain. There are two separate codes you'll need to get started.

The FIRST CODE allows you to give it a name, ensure it's wired correctly, and set the Baud Rate for the HC-05.

Before you upload to Arduino, unplug +5V, so no power goes to the TX and RX pins. This is important. If not done your upload will hang. Also be sure to use a USB to upload, as this setup won't allow you send a sketch over bluetooth. Now that it's uploaded, reconnect +5V and reset Arduino. If anything is out of sync (BAUD RATE WRONG), you won't be able to communicate with the unit and you may also get weird characters in your Serial. If all is well, it will print several lines, if it isn't working right you will only see "OK" followed by blanks and eventually "Done!" (WIRED WRONG). The Bluetooth has a built-in memory to record the settings you choose. (I've tried using other pins beside 10 and 11, and was NOT able to get it to work! Not sure why this is exactly.) D:

#define ROBOT_NAME "RobotName" 
//Change "RobotName" to what you want it called. Must be ONEWORD!

// If you haven't configured your device before use this
#define BLUETOOTH_SPEED 57600
// If you are modifying your existing configuration, use this:
// #define BLUETOOTH_SPEED 57600

#include <SoftwareSerial.h>

// Swap RX/TX connections on bluetooth chip
//   Pin 10 --> Bluetooth TX
//   Pin 11 --> Bluetooth RX
SoftwareSerial mySerial(10, 11); // RX, TX


/*
  The possible baudrates are:
    AT+BAUD1-------1200
    AT+BAUD2-------2400
    AT+BAUD3-------4800
    AT+BAUD4-------9600 - Default for hc-05
    AT+BAUD5------19200
    AT+BAUD6------38400
    AT+BAUD7------57600
    AT+BAUD8-----115200
    AT+BAUD9-----230400
    AT+BAUDA-----460800
    AT+BAUDB-----921600
    AT+BAUDC----1382400
*/


void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("Starting config");
  mySerial.begin(BLUETOOTH_SPEED);
  delay(1000);

  // Should respond with OK
  mySerial.print("AT");
  waitForResponse();

  // Should respond with its version
  mySerial.print("AT+VERSION");
  waitForResponse();

  // Set pin to 0000
  mySerial.print("AT+PIN0000"); 
//The Pin Used to Pair Is 0000 (Change it to PIN1234 to make it 1234)
  waitForResponse();

  // Set the name to ROBOT_NAME
  mySerial.print("AT+NAME");
  mySerial.print(ROBOT_NAME);
  waitForResponse();

  // Set baudrate to 9600
  mySerial.print("AT+BAUD4"); //Only change if using another BaudRate. Else LEAVE IT ALONE!!!
  waitForResponse();

  Serial.println("Done!");
}

void waitForResponse() {
    delay(1000);
    while (mySerial.available()) {
      Serial.write(mySerial.read());
    }
    Serial.write("\n");
}

void loop() {}

The SECOND CODE sends data from your laptop (or desktop if it has Bluetooth) to your Arduino via the HC-05. You will need to make the changes to the wiring we discussed earlier in the Wiring section. This setup will allow you to upload to Arduino WITH the HC-05 still attached. After you've completed your wiring upload the new sketch. I suggest adding an LED between Pin 13 and GND so you can visually see it working. Press 1 in Serial to Turn on LED and Press 0 to turn it off. If this works you are good to go. If all checks out, go up to the Taskbar, choose Tools --> Port --> /dev/cu.RobotName-DevB. Unplug USB from computer and plug it into a wall outlet. Then go back to Serial and Press 1 and 0 to see if it turns on and off.

NOTE: By making Pin 7 Output initially we keep the bluetooth unit from sending power to the TX 1 and RX 0 on the Arduino (which will make it hang). Once initialized HC-05 will turn back on and flash red. When you open Serial, it should reconnect to Bluetooth (turning solid red) as long as that is the chosen Port. Please note that it WILL disconnect when you close Serial.

char data = 0;                //Variable for storing received data

void setup() 
{
  Serial.begin(9600);         //Sets the data rate in bits per second (baud) for serial data transmission
  pinMode(13, OUTPUT);        //Sets digital pin 13 as output pin
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
}
void loop()
{
  if(Serial.available() > 0)  // Send data only when you receive data:
  {
    data = Serial.read();      //Read the incoming data and store it into variable data
    delay(100);
    Serial.print(data);        //Print Value inside data in Serial monitor
    Serial.print("\n");        //New line 
    if(data == '1')            //Checks whether value of data is equal to 1 
      digitalWrite(13, HIGH);  //If value is 1 then LED turns ON
    else if(data == '0')       //Checks whether value of data is equal to 0
      digitalWrite(13, LOW);   //If value is 0 then LED turns OFF
  }                            
}

I hope that this has been helpful. I'm currently working on a method to upload a sketch to Arduino via bluetooth. I'll let you know if I succeeded in a later post. Let me know if you have any questions.

alexvaughn23:
The HC-06 has 4 Pins (VCC, GND, TXD, and RXD). I'm using the version that does NOT have a button.

No HC-06 modules have, or more particularly need, a button. This is because the HC-06 is in AT mode by default if power is on and it is not in communication. The button is used for HC-05 modules, although even with them it isn't vital, and may even not be such a good idea.

Before you upload to Arduino, unplug +5V, so no power goes to the TX and RX pins. This is important. If not done your upload will hang.

Since you are using software serial, you should not need to disconnect Bluetooth.

(I've tried using other pins beside 10 and 11, and was NOT able to get it to work! Not sure why this is exactly.)

Several other pins are available for software serial and should work just as well. I believe this varies ib different types of Arduino. I would have thought getting into the habit of using pins 10,11 was not a good idea as they are better employed for other purposes.

I suggest adding an LED between Pin 13 and GND so you can visually see it working.

Most Arduinos already have a LED on pin 13. I've never actually used it but I imagine that is all that is needed.

Nick_Pyner:
No HC-06 modules have, or more particularly need, a button. This is because the HC-06 is in AT mode by default if power is on and it is not in communication. The button is used for HC-05 modules, although even with them it isn't vital, and may even not be such a good idea.

Ok, I'll update the name to HC-05. As this is a bit confusing between the two.

Nick_Pyner:
Since you are using software serial, you should not need to disconnect Bluetooth.

Unfortunately, if the bluetooth is already connected to +5V it will send a charge to the TX and RX cables causing the Arduino to stop mid-upload and report an error. The work around is to connect the lead to another pin set it to Output, then set it high when you need to power on the module. If power is received by the TX and RX before the sketch has loaded IT WILL hang.

Nick_Pyner:
Most Arduinos already have a LED on pin 13. I've never actually used it but I imagine that is all that is needed.

Even though you are absolutely right. I suggest adding an LED, because if you've ever tried to see a surface mount LED from across the room, you'll know how difficult that can be.

And as far as using other Pins for Serial. Typically this is the case. However, I believe the HC-05 during AT Mode is preprogrammed to use only those specific pins. Like I've said, I've tried.

Nevertheless, I'll make the changes to the from HC-06 to HC-05, and thanks for the input.

alexvaughn23:
Ok, I'll update the name to HC-05. As this is a bit confusing between the two.

The two are distinguishable by the HC-06 having four pins on the breakout board. I suppose it is is possible for the HC-06 to have six pins, and that is indeed common practice with the HM-10, but you will never see an HC-05 with four pins.

Unfortunately, if the bluetooth is already connected to +5V it will send a charge to the TX and RX cables causing the Arduino to stop mid-upload and report an error.

This is not clear, and doesn't sound right, but I can't comment because, back when I had to disconnect bluetooth I simply pulled it out of a four pin socket. Arduino does not stop mid-load, it won't start. I say again, this uploading thing is only a problem if bluetooth is on hardware serial pins 0,1, thereby causing a sharing conflict with the IDE. If you are having an uploading problem with bluetooth on software serial, something else is causing it.

The work around is to connect the lead to another pin set it to Output, then set it high when you need to power on the module. If power is received by the TX and RX before the sketch has loaded IT WILL hang.

OK, I now understand you are talking about HC-05 and you are describing the procedure for configuring it without a button.

However, I believe the HC-05 during AT Mode is preprogrammed to use only those specific pins.

This is nonsense. All the HC-05 wants is a signal, and neither knows nor cares which pins it is connected to. It doesn't even care if it is sent by hardware or software serial. All problems in that arena are down to you and your coding. I don't know much about software serial but note that not all pins are good for it and I assume it varies between Arduino types. Check the data sheets.