Using the HC-05 Bluetooth Library
This exercise details the use of the echo example included in the HC05 library. The echo example echoes characters back to a bluetooth (BT) terminal. Debug messages are sent to the Arduino Serial Monitor.This exercise was updated in June 2014. The default library configuration now uses a software serial port.
The HC-05 Bluetooth to Serial module is a low cost, widely available (search ebay or Amazon) device for creating serial data connections. When looking for an HC-05 to use with this exercise, and the HC05 library , watch carefully to make sure that the device offered is the HC-05, which is programmable as master or a slave, and not the HC-06 which can only be one or the other.
The HC-05 is a surface mount module and runs at 3.3v. Many vendors offer the module mounted on a carrier board with pins and, for some, 5v to 3.3v level translation. Of course the carrier boards add to the price, but it remains reasonable. The unit purchased for this exercise was under $11. It arrived in less than a week from New York (many of the vendors ship from China which can take up to a month to arrive).
I wrote the HC05 library Arduino library to make it easier to to work with the HC-05. The library handles the command/data mode switching and provides blocking for write() calls if there is no connection. The HC05 library can be easily configured to work with hardware or software serial ports and includes a debug mode.
HC-05 Bluetooth to Serial Module
If you buy one mounted on a carrier (recommended), make sure it has a 6 pin connector that includes the KEY and STATE signals. Keep in mind that the base HC-05 module is a 3.3v device, so if you are using a 5V Arduino (such as the UNO), then you will need to get a carrier with on-board regulator and 5v to 3v level shifters, or add those components yourself.
I used an Arduino UNO v3 while developing this library. The library was designed to work with other members of the Arduino family, it simply has not been tested with other family members.
A solderless breadboard with a selection of jumper wires. The colors I used were chosen somewhat arbitrarily and don't really matter. Use what you have.
A terminal program on a computer or smart phone that has a Bluetooth interface. I used Blue Term on an Android phone for my testing.
The breadboard configuration is simple:
This is the recommended connection and matches the default HC05 library configuration:
HC05:Key to UNO:A2
HC05:State to UNO:A5
HC05:TX to UNO:A4
HC05:RX to UNO:A3
HC05:GND to UNO:Gnd
HC05:5V to UNO:5V
Look at line 14 of echo.ino to see how the library is initialized for this wiring configuration:
To install the library on a Linux system, use git to clone the repository. The following example steps assume that you have already installed and run the Arduino IDE at least once:
Open and upload
Now start up the Arduino IDE and open the echo example. It is found in the File->Sketchbook->libraries->HC05->Examples menu. Upload the sketch.
Observe Debug Output
When you have the echo example uploaded, open the Arduino Serial Monitor (Tools->Serial Monitor). Set the serial speed to 57600 baud. You should see messages like these displayed:
The findBaud function tries different serial speeds until it gets a response from the HC05. It prints an 'x' if HC05 does not respond and 'Found' when it does. It stops testing after it detects a response.
The No Connection. waiting... message comes from the btSerial.println() function called at line 27 of echo.ino.
At this point the echo example is waiting for a BT connection to be established.
Now use your BT terminal to connect to the HC05. There are so many different ways to do this that I have to leave it to you to figure out.
Once the connection has been established you should see this message on the terminal:
If you don't see it, try pressing the reset button on the Arduino UNO.
Back on the Arduino Serial Monitor you will find that 'OK' has been printed at the end of the 'No Connection' line. This indicates that a connection has been established and that the echo sketch is ready to echo back any characters it receives over the BT interface.
At this point anything you type at the BT terminal should be echoed back a character at a time.
When you turn off or disconnect the BT connection from the BT terminal, the Arduino Serial Monitor will show that the echo sketch has gone back to waiting for a connection:
The echo sketch doesn't do much, but it is always a good starting point for confirming that the library configuration matches your wiring.
Here are some things you can try on your own:
Try using the hardware serial port. Comment out line 38 of HC05.h and use this wiring diagram:
You can't get debugging messages on the Arduino Serial Monitor with this configuration, and the HC05 must be disconnected while you upload the sketch.
Use the nameChange example sketch included with the library to change the name the HC05 broadcasts. If you are going to use multiple HC05s in your project, this makes it much easier to identify them.