Testing of the HC-05 Bluetooth module consisted of a number of stages. Initially, a USB-TTL converter was used to provide connection of the HC-05 to a PC via USB cable. This enabled using a Windows PC terminal program to send AT commands to the HC-05. Therefore, basic operation of the HC-05 (i.e., did it power up correctly and return device ID etc) could be checked before moving on to custom circuitry and software. This is important as the quality of HC-05 boards from ebay suppliers can sometimes be low (although I have experienced no trouble to date with the HC-05's purchased).
Secondly, the HC-05 is then tested within a circuit interfaced to the Arduino Nano connected to various peripherals. The Bluetooth communications being conducted via free "apps" available for Android based smart phones. This enables testing of the firmware within the Arduino Nano that data can be sent and received via Bluetooth to the Arduino Nano, controlling various peripherals.
Finally, a custom designed and implemented app is produced to send and receive data from the Arduino Nano via bluetooth. This staged process allowed the development and testing to proceed smoother in that errors and bugs could be isolated to the current phase.
Initial Testing with USB-TTL converter
The HC-05 accepts AT commands (when in command mode) which is useful to test if the module is powering up correctly in the first instance. A USB-TTL converter (I used a $2 converter purchased from ebay) makes it convenient to connect the HC-05 to a PC via USB (see the Schematics Section for the simply wiring required). The USB-TTL converter takes power from the USB socket (no other power supply necessary), and the particular converter I had also output 3.3V which again is convenient to power-up the HC-05.
The initial step with the USB-TTL converter is to downloaded the necessary USB drivers (if using Windows) which I sourced from the Prolific Technology Inc site (2) which at the time of download was a file titled "PL2303_Prolific_ DriverInstaller_v1.12.0.zip". Unzip the file, click the installer and follow the usual steps for installing the driver software. When the USB-TTL converter is then connected to the USB port of the PC, Windows should do the usual driver installation steps.
The next step is to use a suitable serial port (COM) terminal emulation program to send commands to the HC-05 via the USB-TTL converter. I used "Terminal" which is very useful for debugging serial communication and is available for free (3) (download the zip file, unzip, and use the installer as usual for a Windows program).
The last photograph in the Photographs Section shows how the HC-05 is connected to the USB-TTL converter and then to the PC (Schematic Section for wiring). In order to send AT commands to the HC-05 via the USB-TTL connection, the small-button on the bottom right-hand side of the HC-05 must be pushed when powering up (i.e. push and hold the button and then insert the USB cable into the PC USB port, when the LED on the HC-05 slowly flashs (~every 2 seconds) the button can be released.
With the USB-TTL connected to the HC-05 and the PC (and the HC-05 is in AT command mode) start the Terminal emulation program on the PC. Connect to the appropriate COM port (baud rate 38400, 8 bits, no parity, 1 stop bit). Enter the letters "AT" (without the quotes) into the transmit field (ensure the CR+LF is selected as equal to CR) and then click send. The letters "OK" should be received back indicating that the HC-05 is in AT command mode.You can then try other AT commands such as "AT+ADDR" which will return the device address of the HC-05.
Note that by using the push button on the HC-05 on power-up causes the HC-05 to enter "mini" AT command mode, which only allows a subset of the AT commands to be used (and a baud rate of 38400 is set and cannot be changed). There are a number of other methods with which to put the HC-05 into "full" AT command mode (which is necessary to use the NAME command for example) (4) , but this is not necessary for basic usage.
Success with getting the HC-05 to enter AT command mode and return information to AT commands means that the HC-05 is operational and ready for use in the PIC microcontroller circuit. Note the default settings for the HC-05 for normal communications (i.e. while not in AT command mode) is 9600 baud, 8 bits, no parity, and 1 stop bit.
Testing Arduino Nano/HC-05 with 3rd Party Phone Apps
When the HC-05 has been checked to be operational (see previous section detailing testing with USB-TTL converter and serial port emulation program), the module can be interfaced to the Arduino Nano and bluetooth communications checked using free Smart Phone apps that are specifically for HC-05 to microcontroller communication. I am using Android based Smart Phones and via Google Play successfully used apps titled "Arduino BlueControl" by "BroXcode" and "Arduino BlueTooth" by "Circuit Magic" (there are many others that are also likely suitable).
The Schematics Section details the wiring connections necessary to interface the HC-05 to the Arduino Nano I/O pins (and a number of peripherals to enable testing of control via Bluetooth). The firmware for the Arduino Nano is relatively straight forward as the HC-05 "looks like" a normal RS-232 serial communications port. The following code snippet 1 demonstrates basic functionality for the Arduino Nano to receive bytes of data from one of the free Bluetooth HC-05 phone apps previously mentioned, and then echo the received data to PC via RS-232.
Code Snippet 1:
#include
// default hardware serial port/pins for connection to PC
// the connected PC/serial monitor allows user data entry
// and display of sent/received data via bluetooth
// setup for connection to bluetooth module
const byte rxPin = 3;
const byte txPin = 2;
SoftwareSerial BTSerial(rxPin, txPin); // RX TX
void setup() {
// define pin modes for tx, rx for bluetooth module
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
BTSerial.begin(9600); // instantiate "start" bluetooth
Serial.begin(9600); // instantiate "start" serial port
Serial.print("started"); // output prompt to show program running OK
}
String messageBuffer = ""; // buffer to accumulate received characters
String message = "";
void loop() {
// loop for receiving data via bluetooth module
while (BTSerial.available() > 0) {
char data = (char) BTSerial.read();
messageBuffer += data;
if (data == ';') { // ";" character used to denote "end of message/string"
message = messageBuffer;
messageBuffer = "";
Serial.print(message); // output to serial monitor/connected PC display
}
}
// loop for sending data via bluetooth module (data entered via serial
// monitor/connected PC
while (Serial.available() > 0) { // receive data from the serial monitor/connected PC
char data = (char) Serial.read();
messageBuffer += data;
if (data == '~'){ // "~" character used to denote "end of message/string"
message = messageBuffer;
messageBuffer = "";
Serial.print(message); // echo to serial monitor
BTSerial.print(message); // send to bluetooth terminal
}
}
}
The above code snippet, coupled with one of the free Android phone apps enables rapid testing if the HC-05 has been successfully interfaced to the Arduino Nano enabling Bluetooth serial communication. The code snippet shows the use of "delimiters" (i.e., the ";" for receiving and "~" for sending) together with the concept of a message buffer. This enables the program to handle incoming individual characters to form strings which can then represent "commands". If the application only needs to receive individual characters, which will be interpreted via appropriate IF THEN ELSE statements to determine necessary actions, the use of "message buffers" could be depensed with.
The Downloads Section at the bottom of Testing/Experimental Results has the full source code used for communication with the Arduino Nano via the HC-05 bluetooth module to control LED's. See xxx.html for details on the Arduino Nano receiving data from a thermocouple (with a real time graph of the data on the Android phone app) as demonstrated working in the Video Section.
Custom Developed Phone App
There are many available phone apps (focusing on the cost "free" apps) that do provide the ability to send/receive data with many having "buttons" and other user interface elements that could be utilised to start/stop LED's, motors etc. While this is useful for initial development and testing of custom circuits incorporating Bluetooth communication functionality, the ability to create a custom app that is tailor-made to specific needs is highly desirable. I focus on Android based smart phones using the free Android Studio IDE, as this enables producing your own bluetooth customised app "relatively" easily, and does not require any initial cost outlay (assuming you have a PC and Android based smart phone).
The first step is installing the Android Studio IDE which provides the software development environment necessary for implementing, testing and uploading a custom app to an Android based smart phone. Android Studio is developed by Google and is free with download and installation instructions located at this reference (5) . There are numerous online tutorials teaching how to use Android Studio and the fundamentals of Android app development (6) ,(7) . For those with prior programming experience, the Android Studio IDE and associated tools makes app development relatively straight-forward. While for those first time programmers, the Android Studio IDE and Java is a good starting point for learning in any case.
After having "mastered" the concepts of Android Studio and the framework behind Android based apps, the next step is dealing with incorporating Bluetooth functionality into an app. Again, there are numerous online tutorials and recommend the following reference as a starting point, which enables simply pairing the Android smart phone with the test circuit and toggling LED's (8) .
Once completing this simple toggling of LED's on and off via Bluetooth and Android phone app, extending the concepts to control motors and other peripherals is then just a straight-forward use of the various user interface elements provided by the Android Studio IDE, in-conjunction with the appropriate firmware code within the PIC microcontroller to act upon received data to control peripherals as desired.
I won't include any further detailed instructions about the Android Studio IDE, developing apps for Android based phones and or incorporating bluetooth communications funtionality, as there are already numerous excellent online sources, and the depth and breadth of necessary information really depends upon the readers prior experience and requirements. However, for those with particular questions, please use the comments section below and I will attempt to answer your queries.
Downloads