IOT: Exploring the mysteries of HC-05 programming

Sources for the Blog:
Final goal for this Blog:

Well, what is the final goal for this blog entry?  I want to query and change the Passcode and name, this is pretty simple to do once everything FINALLY works!  Also, note the two settings in the bottom right hand corner of the serial monitor.  As you can see I have a passcode of 1234, really secure right?

Let’s change the passcode to the equally unsafe 0000 (four zeros):

image

Discussion:

These HC-05/06 devices are quite interesting, but can be a enigma to work with.  So why not just say the heck with it and use the excellent and low cost Particle Photon which uses WiFi? Or the somewhat expensive Red Bear Labs Nano or Micros with Bluetooth Low Energy (BLE).  Well, it is because I have the usual technical brain focus freeze on Bluetooth 4.0 and BLE.  And unlike the brain freeze from drinking a delicious icy drink on a warm day, a cup of coffee doesn’t make it go away, actually the cup of coffee makes it worse.

I have been interested in creating a Bluetooth programmer so there is no need for a cable to program the Arduino, which saves the cost of a cable.  And of course, having a device like the HC-05 and not have a way to program it, just can’t happen on my development desk.

And that is why I have stuck with this idea so long.  Find out how to program the Arduino using a Bluetooth device, and to learn how to program the HC-05.

Historically, after attempting a number of articles, blogs or tutorials, I finally got to the “Make” article titled: DIY Arduino Programming Shield, which I ignored because of the word “Shield”, and I didn’t want to build anything.  Turns out that you can just use wires and no breadboard if you follow the article instructions.  Why is it always the last thing you do that is the right thing?  Oh, because it would be stupid to continue if you solved the problem right? Also, to use the HC-05 as an Arduino Programmer is somewhat more difficult, so I won’t discuss what I did in this article, but the referenced Make article tells you how to make it happen, it’s pretty simple.

Let’s drill down into the HC-05 devices, first of all is it possible to program them using available tools?  By this I mean if you have an Arduino Board laying about (Launchpads likely will work as well). 

But in this journey, how many ways can you incorrectly connect leads from the HC-05 to the Arduino UNO or device you are using.  I started out using an Arduino Nano, spent a great deal of time getting the hook-up to not work (I say that because it didn’t work over time and tutorials).  I switched to the Arduino UNO and still took a number of hours to get it to finally work!  As you can see from the “goals” section above, I was able to change the passcode, which is handy.

Finally, just before I was really going to give up, I had received my fix from Amazon in the shape of connector wires, dupont wires that were not attached as a flat cable and were male to female.  I then took the HC-05 by it’s metaphorical neck and carefully shoved the colored wires on the leads from the HC-05 and then connected it to the Arduino Uno.  Then it worked. The UNO was now a HC-05 programmer, and this means that in the right configuration the HC-05 can act as an Arduino Programmer.

Technical stuff

For the programming to work, the KEY pin must be held to 3.3 volts during the boot cycle for this to work. There is a better way to do this using a pattern on the Vcc and Key, but just holding it at 3.3 volts during the Arduino Boot appears to work.

Get your HC-05 out and connect it up in the following manner, note that you will need to make sure the key pin on the HC-05 low, after powering up the device.

  • Arduino D9 (TX) --------- HC-05 (RX)
  • Arduino D8 (RX) --------- HC-05 (TX)
  • Arduino Vcc (5V)--------- HC-05 (Vcc)
  • Arduino 3.3v       --------- HC-05 (Key)
  • Arduino Ground ---------- HC-05 Ground

Your code may look like the following (there a few references that work on the web, The Makezine is the one I likely stole this code from).  Although unlikely you may need to install the Software Serial library.

/******* Copy Code Starting Here *******/

#include <SoftwareSerial.h>

#define pinRX 8
#define pinTX 9
//SoftwareSerial can set pins other than D0 and D1 for serial input
//Useful for being able to program without removing the
//Bluetooth wires from D0 and D1
// RX=8, TX=9
// HC-05 TX to Arduino Digital Pin 8 (RX) and HC-05 RX to Arduino Digital Pin 9 (TX)
// HC-O5 KEY to 3.3 V
SoftwareSerial mySerial(pinRX,pinTX);
char myChar ;

void setup() {
  //Set the initial baud rate
  Serial.begin(9600); 
  //Try to send an initial AT command, which should be
  //followed by an OK , or a heartbreaking blank line if things didn't work out
  Serial.println("AT");
  //To program you can use 38400, but I think that you can set this to any baud rate
  mySerial.begin(38400);
  mySerial.println("AT");
}

void loop() {
  while (mySerial.available()) {
    myChar = mySerial.read();
    Serial.print(myChar);
  }

  while (Serial.available()) {
    myChar = Serial.read();
    Serial.print(myChar); //echo
    mySerial.print(myChar);
  }
}
/**********Stop code copy here******/

Conclusion

Now that I can program the HC-05, I am going to experiment with it to see how it interacts with the Windows 10 Virtual Shield for Arduino.  There is much more to the interfacing with the HC-05 than just the name and passcode, for instance using these devices to set the HC-05 LED state, use the HC-05 PIO ports that are normally unused and so forth.

But the best thing is that now I can use the more expensive devices without the concern about cost to my readers, eventually I will figure out how to use these inexpensive devices on Lilypads and so forth.  I AM FREE, for the moment anyway.

If you have an HC-05, this blog works, give it a shot, easy breezy.  Now I can get my other stuff working. YIPPEE!

Comments

  • Anonymous
    January 08, 2016
    NEED Your Help to be able to change the hc-05 name and pass using android app like to be able to enter "AT" mode and return back to pairing mode????