This is an old revision of the document!


Table of Contents

Thermistor

Necessary knowledge: [HW] Sensors Module, [HW] lcd, [ELC] Voltage Divider, [AVR] Analog-to-digital Converter, [LIB] Analog to Digital Converter, [LIB] Alphanumeric LCD, [LIB] Sensors

Theory

NTC thermistor

A thermistor is a type of resistor whose resistance varies with temperature. There two types of thermistors: positive temperature coefficient of resistance and negative temperature coefficient of resistance. The resistance of thermistors with positive temperature coefficient of resistance is growing when the temperature grows and negative’s resistance drops. They are abbreviated as PTC (positive temperature coefficient) and NTC (negative temperature coefficient).

The tricky part of using thermistors is that their resistance’s dependence of the temperature is not linear. It is linear only in small temperature ranges. For accurate temperature measurements in wider temperature flotation the Steinhart-Hart third-order exponential equation can be used. For NTC thermistors the following simplified Steinhart-Hart equation with B-parameter exists:

The relation between temperature and resistance of a NTC thermistor.

where:

  • T0 - nominal temperature, usually 25 °C.
  • R0 - resistance at nominal temperature.
  • B - parameter B.

Parameter B is a coefficient, which is usually given in the datasheet of the thermistor. But it is enough stable constant only in a certain ranges of temperature, for example at ranges 25–50 °C or 25–85 °C. If the temperature range measured is wider the equation given (if it is there) in the datasheet should be used.

For measuring the resistance of a thermistor usually a voltage-divider is used, where one resistor is replaced with a thermistor and the input voltage is constant. The output voltage of the voltage-divider is measured, which changes with the change of the resistance of the thermistor. If the voltage is applied, current goes through the thermistor which heats up the thermistor due to thermistors resistance and therefore alters again the resistance. The fault caused by heating up of the thermistor can be compensated with calculations, but it is easier to use a thermistor that has higher resistance and therefore heats up less.

When having to use restricted resources and not having to have very high demands on accuracy, previously calculated charts and tables for temperatures are used. Generally in such tables are ranges of temperatures and the values of resistance, voltage or analogue-digital converters which are corresponding to the temperature ranges given. All exponential calculation is done and all the user has to do is to search up the correct row and read the temperature written on this.

Practice

The module of the home lab is equipped with a NTC type thermistor which has 10 kΩ nominal resistance. At temperatures 25-50 °C the parameter B of the thermistor is 3900. One pin of the thermistor is connected to +5 V supply and the other one is connected to the channel 2 (pin number PF2) of the analogue-digital converter. With the same pin of the micro controller and earth is also connected a usual 10 kΩ resistor, which form a voltage divider with the thermistor. Since we are dealing with a NTC thermistor, which’s resistance reduces when the temperature grows; the output voltage of the voltage divider is growing also when the temperature grows.

It is wise to use a conversion table of values of temperature and analogue-digital converter to find the correct temperature when using the AVR. It is wise to find corresponding value of analogue-digital converter for each temperature degree of desired range of temperature because reverse table will be too large due to the amount of 10 bit ADC values. It is recommended to use some spreadsheet program (MS Excel, Openoffice Calc, etc.) to make the table. Steinhart-Hart formula which is customized for the mentioned NTC thermistors allows finding the resistance of the thermistor which corresponds to the temperature. Then using the resistance, it is possible to calculate the output voltage of the voltage divider and then using this output voltage it is possible to calculate the value of the ADC. Calculated values can be inserted to the program as follows:

//
// Table for converting temperature values to ADC values.
// Every element of the array marks one Celsius degree.
// Elements begin from -20 degree and end at 100 degree.
// There are 121 elements in the array.
//
const signed short min_temp = -20;
const signed short max_temp = 100;
 
const unsigned short conversion_table[] =
{                           
	91,96,102,107,113,119,125,132,139,146,153,
	160,168,176,184,192,201,210,219,228,238,247,
	257,267,277,288,298,309,319,330,341,352,364,
	375,386,398,409,421,432,444,455,467,478,489,
	501,512,523,534,545,556,567,578,588,599,609,
	619,629,639,649,658,667,677,685,694,703,711,
	720,728,736,743,751,758,766,773,780,786,793,
	799,805,811,817,823,829,834,839,844,849,854,
	859,863,868,872,876,880,884,888,892,896,899,
	903,906,909,912,915,918,921,924,927,929,932,
	934,937,939,941,943,945,947,949,951,953,955
};

Following algorithm may be used to find the temperature that corresponds to the parameters of the ADC:

//
// Converting the ADC values to Celsius degrees:
//
signed short thermistor_calculate_celsius(unsigned short adc_value)
{
	signed short celsius;
 
	// Covering the table backwards:
	for (celsius = max_temp - min_temp; celsius >= 0; celsius--)
	{
		// If the value in the table is the same or higher than measured 
		// value, then the temperature is at least the same high as the temperature
		// corresponding to the element.
		if (adc_value >= conversion_table[celsius]))
		{
			// Since the table begins with 0 but values from -20, the value
			// must be shifted.
			return celsius + min_temp;
		}
	}
 
	// If the value was not found the minimal temperature is returned.
	return min_temp;
}

The algorithm searches range from the table where the ADC value suits and remembers the lower ranking number of this range. The ranking number marks degrees; the temperature with the accuracy of 1 degree is become when the original temperature is added to the ranking number.

That conversion table and this function are already in the library of the home lab, in the exercise is no need to write them. The conversion function is named as nimeks thermistor_calculate_celsius in the library. The fact that the conversion is valid only when it is used on the thermistor on the module of sensors of the home lab, must be taken into account. If using other thermistors, a self made conversion table and some more complex function, described in the manual of the library, must be used. The example program of this exercise is a thermometer, which measures temperature in Celsius scale and displays it on an alphabetical LCD.

//
// Example program of the thermistor of module of sensors of home lab.
// The temperature is displayed on the LCD.
//
#include <stdio.h>
#include <homelab/adc.h>
#include <homelab/module/sensors.h>
#include <homelab/module/lcd_alpha.h>
 
//
// Main program
//
int main(void)
{
	unsigned short value;
	signed short temperature;	
	char text[16];
 
	// Setting the LCD
	lcd_alpha_init(LCD_ALPHA_DISP_ON);
 
	// Cleaning the LCD
	lcd_alpha_clear();
 
	// Name of the program
	lcd_alpha_write_string("Termomeeter");
 
	// Setting the ADC
	adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
 
	// Endless loop
	while (true)
	{
		// Reading the 4 times rounded values of the voltage of the thermistor
		value = adc_get_average_value(2, 4);
 
		// Conversing the values of ADC into celsius scale
		temperature = thermistor_calculate_celsius(value);
 
		// Conversing the temperature in to text.
		// To display the degree sign, the octal variable is 337.
		sprintf(text, "%d\337C   ", temperature);
 
		// Displaying the text in the beginning of the second row of the LCD.
		lcd_alpha_goto_xy(0, 1);
		lcd_alpha_write_string(text);
	}
}

Extra

en/examples/sensor/thermistor.1265827136.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0