Objective
This project uses an RN41 Bluetooth® module interfaced with an Xpress development board. The RN41 module used in this project is part of a MikroElektronika Bluetooth® Click Board. The output of the Xpress Module's on-board potentiometer is connected to the ADCC. That signal is then sent to the RN41 serially via a EUSART connection. This provides a complete foundation for sending analog information via Bluetooth communication, helping you get up and running with your next remote sensor application.
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Additional Files
Files |
---|
Additional Resources:
Procedure
1
Create a new project
Create a new project in MPLAB Xpress for a PIC16F1855 using the MPLAB Xpress Development Board called analogReadSerialWrite.
Instructions are below if this is your first project.
2
Open the MPLAB Code Configurator (MCC).
Instructions are below if this is your first project.
4
Setting the EUSART
The Bluetooth module will communicate with the PIC16F18855 via serial communication. Therefore, we will take advantage of the EUSART Peripheral to send the RN41 initialization commands serially. Choose EUSART from the Device Resources menu:
Once the window is open, a few settings need to be changed.
- Check the Enable Transmit box to enable the PIC16F18855 to send information out across the EUSART serial line.
- Check the Enable EUSART Interrupts because interrupts will be used in the written portion of this program to help with the communication timing between the PIC16F18855 and the RN41.
- Check the Redirect STDIO to USART box. This allows the use of the Standard IO library which makes code writing an easier process by adding additional functions such as printf, which is used heavily in this program.
- Increase the Baud Rate to 115200 because this is the speed of communication for the RN41 (found on page 7 here).
- Check the Enable Continuous Receive because the PIC will be receiving more than one piece of information from the RN41.
5
ADCC setup
As mentioned in the introduction, this program will take data from an analog potentiometer and feed it to the Bluetooth module. Therefore, we will also need an ADCC module to convert the analog potentiometer values to digital signals. Find the ADCC under Device Resources:
We need to change a few settings in the ADCC.
- Change the Clock Source to FRC, which will allow the ADCC to run even if the CPU is in sleep mode.
- Change the Result Alignment to right.
Because the information coming from the ADCC is 10-bits, it must use two 8-bit registers. This changes the order of the output information from the ADCC to right alignment, changing the output values from the leftmost 10 bits of a 16-bit value to the right 10 most bits. What this means for you is that your program can expect values from 0 to 1023 (20 to 210) as opposed to values from 27 to 216, which will be much more confusing to deal with. Check your finished window with the one below:
6
Set the Analog Input Pin
You may notice that we only changed two things in the ADCC window, but three places were circled. The third-place highlights an I/O pin name change, specifically the ADCC input channel connected to the potentiometer. To update this, navigate to the Pin Module under Project Resources:
First, close the locks in the pin configurator window as shown. To close a lock, simply click the open lock. The lock will turn green to highlight the pin is selected.
To make the user-written program easier to write, we will also change the names of a few pins by changing the Custom Name in the pin configurator window:
7
Generating Code
Once you have finished the preliminary setup, choose the Generate option at the top of the page to generate your settings and import them into the MPLAB Xpress. To check that everything uploaded, check your Project box under the Header Files:
You will notice that there are two additional files here: RN41.h and RN41.c. The additional files are part of the Project and Source Files download listed earlier. These two files include a series of functions written to set up all the preliminary settings necessary to communicate with the Bluetooth module. The Bluetooth module is controlled by serial commands sent from the PIC16F18855, and therefore these commands must be added into the project.
8
Linking the MCC generated code to your application in main.c
Next, the following code needs to be entered into the main.c file that was generated by the MCC. Double click main.c to make sure you are in the correct file. You can also check by looking at which tab is highlighted in the top of the main window:
One word of caution, if the delay functions increase above ~50 μs, it will cause the program to get stuck and stop working. This is due to the byte transfer speed (115200 kps).
Once you have completed writing the program, then it is time to program the microcontroller. Choose the Make and Generate Code button at the top of the IDE window to generate a .hex file.
Then, drag this into your Xpress board that shows up as a USB mass storage device to your computer:
Results
When you have completed this process, it’s time to test it. This project demonstrates this project working with a free Android App called Bluetooth Terminal.
This app allows a user to connect the RN41 to a phone and see the data being sent. Here are the results in a screenshot:
As you can see, the RN41 is sending the current 10-bit value coming from the ADCC; to change this value, turn the onboard potentiometer.
Conclusions
Now that you have become successful with the Bluetooth chip at an introductory level, you are at the jumping off point for more interesting things. The potentiometer is only one of a myriad of analog sensors that can be used to bring physical world information into the digital world. Use the base of information covered in this tutorial to send other analog information, such as the temperature or weather, into the digital world over greater distances provided by Bluetooth communication. This is a great stepping off point for development in the Internet of Things, or simply if you do not want wires connecting things over long or complicated distances.