This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:practical:hardware:itt:avr:thermistor [2025/08/26 11:15] – ingmar05 | en:iot-open:practical:hardware:itt:avr:thermistor [2025/09/02 12:45] (current) – raivo.sell | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== Thermistor ====== | ||
| + | |||
| + | ===== Theory ===== | ||
| + | |||
| + | [{{ :: | ||
| + | |||
| + | A thermistor is a type of resistor whose resistance varies with temperature. There are two types of thermistors: | ||
| + | |||
| + | The thermistor' | ||
| + | |||
| + | |||
| + | {{: | ||
| + | |||
| + | where:\\ | ||
| + | * T< | ||
| + | * R< | ||
| + | * B - parameter B. | ||
| + | |||
| + | Parameter B is a coefficient, | ||
| + | |||
| + | |||
| + | 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' | ||
| + | |||
| + | |||
| + | ===== 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. // | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // 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, | ||
| + | 160, | ||
| + | 257, | ||
| + | 375, | ||
| + | 501, | ||
| + | 619, | ||
| + | 720, | ||
| + | 799, | ||
| + | 859, | ||
| + | 903, | ||
| + | 934, | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | The conversion table and function are already in the library of the HomeLab. In the library, the conversion function is named // | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // 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, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | // Setting the ADC | ||
| + | adc_init(ADC_REF_AVCC, | ||
| + | |||
| + | // Endless loop | ||
| + | while (true) | ||
| + | { | ||
| + | // Reading the 4 times rounded values of the voltage of the | ||
| + | // thermistor | ||
| + | value = adc_get_average_value(ADC_CHANNEL, | ||
| + | |||
| + | // 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, | ||
| + | |||
| + | // Displaying the text in the beginning of the third row of the LCD | ||
| + | lcd_gfx_goto_char_xy(5, | ||
| + | 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. | ||