====== Thermistor ====== ===== Theory ===== [{{ ::examples:sensor:thermistor:ntc.jpg?200|NTC thermistor}}] A thermistor is a type of resistor whose resistance varies with temperature. There are two types of thermistors: positive temperature coefficient of resistance and negative temperature coefficient of resistance. The resistance of thermistors with a positive temperature coefficient of resistance increases when the temperature grows, and with a negative coefficient, the resistance decreases. The respective abbreviations are PTC (//positive temperature coefficient//) and NTC (//negative temperature coefficient//). The thermistor's resistance dependence on temperature is not linear, and this complicates its usage. For accurate temperature measurements in wider temperature fluctuations, the Steinhart-Hart third-order exponential equation is used as the thermistor's resistance is linear only in a small temperature range. The following simplified Steinhart-Hart equation with B-parameter exists for NTC thermistors: {{:examples:sensor:thermistor:sensor_ntc_equation.png?100|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 stable enough, constant only in a certain range of temperature, for example, in the ranges 25–50 °C or 25–85 °C. If the temperature range measured is wider, the data sheet should be used for retrieving the equation. Usually, a voltage divider is used for measuring the resistance of a thermistor, 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 according to the change in the resistance of the thermistor. If the voltage is applied, current goes through the thermistor, which heats up the thermistor due to the thermistor's resistance and therefore alters the resistance again. The fault caused by the heating up of the thermistor can be compensated with calculations, but it is easier to use a thermistor that has a higher resistance and therefore heats up less. With restricted resources and with fewer demands on accuracy, previously calculated charts and tables for temperatures are used. Generally, the tables have ranges of temperatures and respective values of resistance, voltage, or analogue-digital converters. All exponential calculations are already done, and the user needs to only find the correct row and read the temperature given. ===== Practice ===== The Sensor module of the HomeLab is equipped with an NTC-type thermistor, which has a 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 the supply, and the other one is connected to the analogue-digital converter channel two. A typical 10 kΩ resistor is also connected with the same pin of the microcontroller and earth, and together with the thermistor forms a voltage divider. Since we are dealing with an NTC thermistor, whose resistance decreases as the temperature increases, the output voltage of the voltage divider increases with growing temperature. While using the AVR, it is practical to use a conversion table of values of temperature and an analogue-digital converter to find the correct temperature. It is wise to find the corresponding value of an analogue-digital converter for each temperature degree of the desired range of temperature because the reverse table will be too large due to the amount of 10-bit ADC values. It is recommended to use any kind of spreadsheet program (MS Excel, LibreOffice Calc, etc.) to make the table. //Steinhart-Hart// formula, which is customized for the mentioned NTC thermistors, is able to find the resistance of the thermistor that corresponds to the temperature. Derived from the resistance, it is possible to calculate the output voltage of the voltage divider and use this output voltage to calculate the value of the ADC. Calculated values can be inserted into 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 degrees and end at 100 degrees // 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 }; The conversion table and function are already in the library of the HomeLab. In the library, the conversion function is named //thermistor_calculate_celsius//. An example program of this exercise is a thermometer, which measures temperature in the Celsius scale and displays it on an LCD. // The temperature is displayed on the LCD #include #include #include #include #include // Sensor //#define ADC_CHANNEL 2 // Main program int main(void) { unsigned short value; signed short temperature; char text[16]; // Initialization of LCD lcd_gfx_init(); // Clearing the LCD and setting the backlight lcd_gfx_clear(); lcd_gfx_backlight(true); // Name of the program lcd_gfx_goto_char_xy(1, 1); lcd_gfx_write_string("Thermometer"); // 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(ADC_CHANNEL, 4); // Converting the values of ADC into the Celsius scale temperature = thermistor_calculate_celsius(value); // Converting the temperature into text // To display the degree sign, the octal variable is 56 sprintf(text, "%d\56C ", temperature); // Displaying the text in the beginning of the third row of the LCD lcd_gfx_goto_char_xy(5, 3); lcd_gfx_write_string(text); hw_delay_ms(1000); } return 0; } ==== Task to be implemented ==== - Switch every 3 seconds the unit between Kelvin (K), Fahrenheit (F), and Celsius (C). The temperature must be displayed in correct values, units, and symbols.