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 a PIC18F248 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 PIC18F248 that data can be sent and received via Bluetooth to the PIC microcontroller, controlling various peripherals.
Finally, a custom designed and implemented app is produced to send and receive data from the PIC18F248 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 PIC18F248/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 PIC microcontroller 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 microcontroller USART I/O pins (and a number of peripherals to enable testing of control via Bluetooth). The firmware for the microcontroller (using CCS C compiler) 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 PIC microcontroller 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 and #fuses
#use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8, ERRORS, STREAM=PCserial)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS, STREAM=blueTooth) //-> HW UART pins
char recChar; //the received character from the serial port via keyboard
int8 toggleLEDCount, toggleLED;
void main() {
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); //prescaler 8
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
fprintf(PCserial,"\n\rstart\n\r");
while(TRUE){
if (toggleLEDCount==1) {
toggleLEDCount = 0;
toggleLED++; //10 counts of 0.2sec = toggle every 2 secs
}
if (toggleLED>=10) { // toggle LED to show program running
output_toggle(PIN_B0);
toggleLEDCount = 0; toggleLED = 0;
set_timer1(3035); //prescaler=8, 10Mhz, preload=3036 -> Freq 5hz, period=0.2sec
}
if (kbhit(blueTooth)) {
recChar = fgetc(blueTooth);
fprintf(PCserial,"blueTooth=%c \n\r",recChar);
}
}
}
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 PIC 18F248 enabling Bluetooth serial communication. The major limitation of the code in snippet 1 is that without interrupt driven capture of incoming serial data, multiple byte data streams cannot be reliably received by the microcontroller.
The following code snippet 2 demonstrates the use of the RDA interrupt available on USART capable PIC microcontrollers with an appropriate interrupt routine and a circular buffer to receive incoming serial data over the HC-05 bluetooth connection. This strategy enables reliable multiple byte data transmission (up to 38400 baud rate was tested successfully):
Code Snippet 2:
// #include and #fuses
#use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8, ERRORS, STREAM=PCserial)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS, STREAM=blueTooth) //-> HW UART pins
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
char recChar; //the received character from the serial port via keyboard
int8 toggleLEDCount, toggleLED;
#int_timer1
void timer1_interrupt() { /* Timer1 has wrapped around to 0. */
toggleReadCount=1;
set_timer1(3035); //prescaler=8, 10Mhz, preload=3036 -> Freq 5hz, period=0.2sec
}
#int_rda
void serial_isr() {
int t;
buffer[next_in]=fgetc(blueTooth); //getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit);
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main() {
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); //prescaler 8
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_RDA);
fprintf(PCserial,"\n\rstart\n\r");
while(TRUE){
if (toggleLEDCount==1) {
toggleLEDCount = 0;
toggleLED++; //10 counts of 0.2sec = toggle every 2 secs
}
if (toggleLED>=10) { // toggle LED to show program running
output_toggle(PIN_B0);
toggleLEDCount = 0; toggleLED = 0;
set_timer1(3035); //prescaler=8, 10Mhz, preload=3036 -> Freq 5hz, period=0.2sec
}
if (bkbhit) {
fprintf(PCserial,"\r\nBuffered data => ");
counter=0;
while(bkbhit) {
recChar = bgetc();
fputc( recChar,PCserial );
tempBuffer[counter]=recChar;
counter=counter+1;
}
fprintf(PCserial,"\r\nbytes=%u", counter);
}
}
}
The Downloads Section at the bottom of Testing/Experimental Results has the full CCS C source code used for communication with the PIC micrcontroller via the HC-05 bluetooth module to control LED's, speed of a DC fan motor and receive data from a LM35 temperature sensor (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