Saturday, 20 July 2013

Temperature Sensor using thermistor (PTC) and 16 bit PIC microcontroller. (Code + Proteus Simulation)

This post would provide a working example of how to interface thermistor or how to use thermistor as temperature sensor. This thermistor is PTC (Positive temperature coefficient) and has approximately linear behavior from -55 to 200 oC with tolerance that I found in Simulation is different for negative and positive temperatures form ±2oC (When temperature is less than 0oC) to ±10oC( when temperature goes beyond 200oC).

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 you may get help for changes.

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 thermistor is change in resistance with temperature so we can find resistance by using potential divider formula and then with a temperature and resistance relationship we would be able to find temperature. The formula is as follow: this formula is also given in its datasheet.

Proteus Simulation:

            In the following figure 1. I used a fix resistor (R3 = 10kΩ) that is pull-up by Vcc (5 volts) and another 10kΩ resistor R3 in between them I used a 1kΩ thermistor. This creates a potential divider and we can measure the value of resistor by using potential divider formula. From figure is is cleared that I am increasing temperature as shown in green window is same as displayed on LCD. I used LCD on PortB while using ADC module to find resistance change due to temperature. The Potential divider output is given to ADC channel zero on AN0.

Figure 1. Proteus circuit.

In the following figure 2. Animation shows that when I change temperature (Increase or decrease) the value of temperature is shown on LCD.

Figure 2. Proteus circuit simulation.


In the following figure 3. This is main function in which I first initialize LCD, then I configure ADC with Channel 0. And a loop which only calls a function named DisplayTemperature() which is responsible for displaying measured temperature from thermistor on LCD.

Figure 3. Main function.

In the following figure 4. We have DisplayTemperature() which has a string that is printed directly and a variables named “R3”, “R2” and “Vcc” whose value is fixed for this design as 10kΩ , 10kΩ and 5.0 volts respectively (you may change them according to your requirement). “VoltageOuput” is potential divider value with the formula “voltageoutput = (R2 + RT2)/ (R3 + R2 + RT2)) * Vcc”. Rearranging this formula for value of RT2 you will get the shown in snapshot of code in figure 4. Where RT2 is “TemperatureResistance”.

Figure 4. Display Temperature.

Now at this stage we measured the value of thermistor resistance and now we have to use the formula given in datasheet and is shown on top of this post the constants A, B, C, D values are shown in that formula. By using this formula we measured temperature through change in resistance (Thermistor). Now after finding temperature we have to display it on LCD this is done by passing temperature as a parameter to the function WriteLongAsFloatUptoFiveFigures().

Reading Suggestions:


You can download Code (MPLAB X and C30 compiler) and Proteus (v8) Simulation. Click here

For all topics Click here.

That’s all for this post hope you will learn please comment if you have any questions.


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.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...