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. BackgroundThe 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. Resources
SetupThe breadboard configuration is simple:This is the recommended connection and matches the default HC05 library configuration:
echo.ino to see how the library is initialized for this wiring configuration:HC05 btSerial = HC05(A2, A5, A3, A4); // cmd, state, rx, tx ProcedureInstallTo install the library on a Linux system, usegit to clone the repository. The following example steps assume that you have already installed and run the Arduino IDE at least once:$ cd ~/sketchbook/ libraries $ git clone https://github.com/jdunmire/HC05.git Open and uploadNow start up the Arduino IDE and open theecho example. It is found in the File->Sketchbook->libraries->HC05->Examples menu. Upload the sketch.Observe Debug OutputWhen you have theecho example uploaded, open the Arduino Serial Monitor (Tools->Serial Monitor ). Set the serial speed to 57600 baud . You should see messages like these displayed:findBaud Trying 9600... x Trying 19200... x Trying 57600... x Trying 115200... x Trying 38400... Found. No Connection. waiting... 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.BT ConnectionNow 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: Echo Server- type something 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.No Connection. waiting... OK 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: No Connection. waiting... OK No Connection. waiting... Wrap-up The echo sketch doesn't do much, but it is always a good starting point for confirming that the library configuration matches your wiring. Exercises Here are some things you can try on your own:
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.
Contact me about the exercise by commenting on my Google+ post or by opening an issue at the GitHub repository for the library. |