How to measure negative and positive temperature using LM35 Code + Proteus Simulation
This post will provide a working example of how to interface a temperature sensor LM35 with PIC microcontroller. LM35 is linear temperature sensor with temperature measurement range from -55 to +150oC. With every 1oC temperature change its output changes by 10mV.
You may download code and Proteus Simulation from the download section at the bottom of this post. I am using MPLAB X IDE, C30 Compiler and Proteus Simulation on v8.0. This code is written in C Language and will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing the code.
In this post I supposed that you know following things if not then please go through following posts before proceeding with this.
· How to Blink an LED. (Click here)
· How to interface an LCD. (Click here)
· How to configure ADC module. (Click here)
· Digital Voltmeter. (Click here)
The science behind measuring temperature with this sensor is very simple but slightly tricky. Actually according to datasheet has following behaviour as mentioned in table given below. You may have noticed that in case of negative temperature we have -550mV which not measured by ADC directly with one positive supply. You may not use an Inverting Amplifier in this case because output voltage range is from -550mV to 1500mV. So the solution is very simple you use two diodes on GND terminal of sensor for measuring negative temperature also. This circuit is shown in figure 1. Now you need two ADC channels one at output pin and second at GND pin. At the end you will subtract to get the voltage covering the range of -550mV to 1500mV.
|Figure 1. LM35 Single Supply configuration.|
In the following figure 2. I used a zener diode of 2.7V for creating a reference of 2.7V for a getting a good resolution for ADC module. How come I Know that 2.7V is good for this sensor? This sensor has maximum output with this circuit is 2.56V that’s why I used 2.7v reference voltage. Vout of this sensor is connected with AN1 (Analog channel 1) whereas GND pin of sensor is connected with AN12 (Analog channel 12). I connect LCD with PORTB.
|Figure 2. LM35 Temperature sensor Schematic.|
In the following figure 3. Animation shows that when I increase/decrease temperature shown in Green box the temperature also changes on LCD. And the value displayed in green box is same as that we measured through ADC and is displayed on LCD with a tolerance of ±0.5oC.
|Figure 3. LM35 Animation of Proteus Simulation.|
In the following figure 4. This is main function in which I first initialize LCD, then I configure ADC module with external reference voltage. And a loop which only calls a function named DisplayTemperature() which is responsible for displaying measured temperature through LM35 on LCD.
|Figure 4. Main function LM35.|
In the following figure 5. We have DisplayTemperature() which has variable named “VoltageOutputCh1” and “VoltageOutputCh12” having values of measured voltage through an ADC channels 1 (AN1) and 12 (AN12) respectively. Multiplying both voltages with 1000 is just to make my voltages into millivolts. Now at this stage we have temperature as difference of two voltages. To find out the temperature as a single voltage we have to subtract the voltages using formula “VoltageDifference = VoltageAN1-VolageAN12”. This gives negative voltage for negative temperature and positive voltage for positive temperature. This formulas is shown in line no. 36 of the code. Now for displaying negative sign and temperature I used if statement. Which displays the negative sign and then I reverse the formula because I don’t like to change negative value into positive for displaying using bit operations. This technique saves two additional steps and flow of code is remains the same.
|Figure 5. LM35 Temperature displaying function.|
Now at this stage we measured the value of temperature. For displaying this value on LCD we have function named WriteLongAsFloatUptoFiveFigures(). This function will display variable “Temperature” by passing it as a parameter.
You can download Code (MPLAB X and C30 compiler) and Proteus (v8) Simulation. Clickhere
That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.
All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission.
© Copyright 2013 IbrahimLabs. All rights reserved.