Objective
This tutorial uses the popular LM35 temperature sensor to enable temperature measurement with the PIC16F18855. The LM35 is a cheap and easy-to-use part that can be useful in applications that may be harmed from excessive heat or even as a fun thermometer side project.
The Project Uses:
Useful Pre- Knowledge:
- Analog Read Serial Write using the Analog-to-Digital with Computation (ADCC) peripheral
- Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) Basics on Xpress
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| |
Procedure
1
Task 1
Create a new project in MPLAB® Xpress for a PIC16F1855 using the MPLAB Xpress Development Board called "analogReadSerialWrite."
If this is your first project, follow the instructions given below.
2
Task 2
Open the MPLAB Code Configurator (MCC).
If this is your first project, follow the instructions given below.
3
Task 3
Because the LM35 temperature sensor is an analog sensor, we must first translate the analog signal to a digital signal before we can read the signal accurately with our microcontroller. The ADCC module is perfect for this translation. Therefore, once you have opened MCC online, choose the ADCC module from under the Device Resources tab:
4
Task 4
Double-clicking ADCC will open a window similar to the one below:
The only changes necessary to make are those described here. First, the clock source FRC allows the ADCC clock to run independently of the system clock. This demonstrates the clock flexibility on the PIC16F18855 chip which can create multiple clock signals for different modules. Second, change the Result Alignment to right so that the bits are aligned in the correct order. This will become important in the written section of our program.
6
Task 6
Choosing this module will open a window similar to the one below:
The changes made to the default window are shown above. Check the box labeled Enable Transmit to allow the EUSART to send out information. This module enables your chip to send words and numbers to the terminal window. While you do not need to change the baud rate, it is highlighted here because it is important to note. This is the speed at which information is exchanged and is necessary to know when setting up your computer terminal. Otherwise, the bytes you send to the computer will show up as gibberish. Lastly, check the Redirect STDIO to USART box to simplify the lines of code you need to write by enabling the STDIO software library.
7
Task 7
Next, we must update our Pin Configurator so that the microcontroller knows which pin is the input to the ADCC (i.e., which pin is connected to the temperature sensor). Choose RC7 as the ADCC input by clicking the Lock icon so that it turns green. Second, choose RC0 as the EUSART output so that it properly sends the signal to the computer terminal via the serial connection. These changes are seen below:
Finally, we will also change the ADCC input channel name to make it easier to type and understand in our written code section. Open the Pin Module under Project Resources and rename the ADCC input pin name to "tempsense":
Once you have set up this page, choose Generate to populate the initialization files in your Online IDE window shown below:
8
Task 8
In order to take the raw data coming from the temperature sensor and translate it into something meaningful, we must write a couple of lines of code.
The temperature sensor sends out a voltage signal directly proportional to the temperature that it senses. Looking at the datasheet for the LM35, we can see that this is 10.0 mV/°C:
However, if the ambient temperature is around 23.8°C, the ADCC sends out a value of 74. So, where did this number come from?
The ADCC populates a 10-bit register, and therefore it has 2^10 or 1024 options for values that it can send out. Next, it has some kind of a Positive Reference and a Negative Reference which defines the range of voltages it will send/receive. In our program, we left Positive Reference defined as VDD (or 3.3 V) and Negative Reference defined as VSS (or 0 V) in the ADCC module window:
Therefore, the step size that it can register is (3.3 V / 1024 steps) = 0.00322 V/step . Now, if the ambient temperature is 23.8°C, it will register as a voltage of 23.8°C *0.010 V/C = 0.238 V. Taking 0.00322 V/step * 0.238 V with rounding equals ~74 steps. Therefore, the integer the ADCC sends to the microcontroller is 74.
This is the number given by calling the function adc_result_t ADCC_GetConversionResult(void) which can be found in the adcc.h header file under Project :
We will use this function in our code, seen below. In this tutorial, we combine it with conversion functions to convert from the integer value back to Celsius, then from Celsius to Fahrenheit. Write or copy and paste the following code into the main.c file under Project:
#include "mcc_generated_files/mcc.h"
/* Use LM35 temperature sensor to send temperature data
to a computer terminal window*/
/* conversion from integer to Celsius equivalent, Vref is 3.3 V,
data comes in as a mV value, must be scaled by 100,
1024 is the 10 bit ADCC value */
#define conversionrawtoc (3.3 * 100) / 1024
//
/*
Main application
*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
uint16_t raw;
while (1)
{
/*delivers a number of 0 to 1023, defined by
(Vref / 1024) = step size in volts */
raw = ADCC_GetSingleConversion(tempsense);
//converts the ADCC value to degrees celcius using scale equation
float temp = raw * conversionrawtoc;
//converts the Celsius value to Fahrenheit
float ftemp = (temp * (1.8)) + 32;
//prints a string to the computer terminal
printf("ADCC reads: %i Celsius Value: %2.2f Fahrenheit Value: %2.2f \r", raw, temp, ftemp);
}
}
/**
End of File
*/
Once this is complete, it is time to program the microcontroller.
9
Task 9
Click on the Make and Program Device icon:
to download the HEX file of your code and then drag it into the Xpress board icon under My Computer:
Once the microcontroller is programmed, open up a terminal emulator window such as Tera Term or Coolterm. You should see a screen similar to the following:
Results
If you see gibberish, make sure that the baud rate of your terminal agrees with that set up in your MCC window. If some is gibberish, you may need to slow the baud rate down to reduce the error margin.