Programming PIC microcontrollers with Bluetooth communication
One can transfer byte data (serial) via bluetooth with PIC microcontrollers using microchips HC-05 bluetooth
module. The serial communication is done using the EUSART module found on many PIC microcontrollers. I prefer
to use the simplest PIC microcontroller with this feature, the PIC16F690. To get started one can
program the PIC16F690 with the following test program.
Programming the PIC microcontroller
sertestBT.asm is a simple test program for the PIC16F690 written in assembly to tryout
bluetooth serial data transfer. The program sends a byte via the EUSART module to the HC-05 for serial transfer.
The program delays around 1/4 second, then increments the byte by 1 and send the incremented byte. The data
transfered starts at 0 and increases to 255, then cycles through again, etc. Once can also program the chip
in C or another compiler for the microchip product. The microcontroller is connected to the HC-05 as shown
below:
Note: In this setup, the HC-05 is in its default mode as slave.
Reading the serial data sent from the HC-05
Using a Cell Phone:
To check if the PIC microcontroller and HC-05 module are correctly
sending data, one can read the data with a cell phone. Install one of the various free aps
available from Google Play Store that can read Bluetooth transmission, i.e. Blue Terminal.
Then, connect to the HC-05 with the default password 1234. Once connected, read the data
in HEX format. You should see the HEX data increase by one number every 1/4 sec. If you
can read the HEX data, then you know that the HC-05 connection is working.
Reading the data with PC in Linux:
For me, the best way to communicate via bluetooth with the HC-05 is to use serial data transfer
with the driver /dev/rfcomm0. An easy way to set up the driver is to install a "Bluetooth Manager",
for example blueman. Run the bluetooth manager to search which bluetooth devices are available to
connect to. Then, choose the hc-05 and connect. The manager will make a serial /dev/rfcommx file available
to use. The first bluetooth device connection will be /dev/rfcomm0. The next device connected will be
/dev/rfcomm1, etc. As far as I can see, one can use /dev/rfcomm0 the same way as /dev/ttyUSB0 is used
for usb serial port transfer. Below, are listed some examples of reading the serial data from the
PIC16F690 code sertestBT.asm , after /dev/rfcomm0 is in your root
directory:
- Using Nodejs: This is probably the easiest way to transfer serial data from the HC-05 to
a PC. Here is a sample program: bluetest.js. Run the program in root
by typing sudo node bluetest.js. The program will ask for the number of bytes to be read.
Nothing will be printed until all the bytes have been read. Then, after the bytes have been read,
they are printed on the screen and saved in the file dataout.txt. I believe that once the program
is started, the serial buffer is flushed. Then, the first byte sent will be the first byte recorded.
- Using C (gcc) in Linux: A sample program to transfer the serial data in linux gcc is
rfcommserial3.c. To compile the code type gcc rfcommserial3.c -lm.
To run the executable type sudo ./a.out since you need to be in super user mode to access the
/dev/rfcomm0 file. As the code is setup now, first the buffer is flushed. Then the code reads
256 bytes. After all the bytes have been received the values are printed on the screen.
- Reading the data directly from rfcomm0: One can also just read the rfcomm0 file directly.
The example code rfreadch.c reads the serial byte data directly. The code is
setup to read 20 data points and print the values to the screen. First it reads the data stored in the
serial port buffer (i.e. the buffer is not flushed). If there are less than 20 values in the
buffer, the data will be read and printed as they are received.
Siegel's Home Page
Physics Department|
College of Science