This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:thermistor [2013/03/26 21:15] – external edit 127.0.0.1 | en:examples:sensor:thermistor [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Thermistor ====== | ====== Thermistor ====== | ||
| - | //Necessary knowledge: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [ELC] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| - | [{{ : | + | [{{ |
| A thermistor is a type of resistor which resistance varies with temperature. There are two types of thermistors: | A thermistor is a type of resistor which resistance varies with temperature. There are two types of thermistors: | ||
| Line 29: | Line 33: | ||
| ===== Practice ===== | ===== Practice ===== | ||
| - | The Sensor module of the HomeLab 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. 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 a NTC thermistor, which resistance decreases as the temperature grows; the output voltage of the voltage divider is increasing repectively with growing temperature. | + | The Sensor module of the HomeLab 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 supply and the other one is connected to the analogue-digital converter |
| - | While using the AVR it is practical to use a conversion table of values of temperature and analogue-digital converter to find the correct temperature. 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 any kind of spreadsheet program (MS Excel, | + | While using the AVR it is practical to use a conversion table of values of temperature and analogue-digital converter to find the correct temperature. 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 any kind of spreadsheet program (MS Excel, |
| <code c> | <code c> | ||
| - | // | + | // Table for converting temperature values to ADC values |
| - | // Table for converting temperature values to ADC values. | + | // Every element of the array marks one Celsius degree |
| - | // Every element of the array marks one Celsius degree. | + | // Elements begin from -20 degree and end at 100 degree |
| - | // Elements begin from -20 degree and end at 100 degree. | + | // There are 121 elements in the array |
| - | // There are 121 elements in the array. | + | |
| - | // | + | |
| const signed short min_temp = -20; | const signed short min_temp = -20; | ||
| const signed short max_temp = 100; | const signed short max_temp = 100; | ||
| Line 63: | Line 65: | ||
| <code c> | <code c> | ||
| - | // | ||
| // Converting the ADC values to Celsius degrees: | // Converting the ADC values to Celsius degrees: | ||
| - | // | ||
| signed short thermistor_calculate_celsius(unsigned short adc_value) | signed short thermistor_calculate_celsius(unsigned short adc_value) | ||
| { | { | ||
| Line 75: | Line 75: | ||
| // If the value in the table is the same or higher than measured | // If the value in the table is the same or higher than measured | ||
| // value, then the temperature is at least as high as the | // value, then the temperature is at least as high as the | ||
| - | // temperature corresponding to the element. | + | // temperature corresponding to the element |
| if (adc_value >= conversion_table[celsius])) | if (adc_value >= conversion_table[celsius])) | ||
| { | { | ||
| // Since the table begins with 0 but values of the elements | // Since the table begins with 0 but values of the elements | ||
| - | // from -20, the value must be shifted. | + | // from -20, the value must be shifted |
| return celsius + min_temp; | return celsius + min_temp; | ||
| } | } | ||
| } | } | ||
| - | // If the value was not found the minimal temperature is returned. | + | // If the value was not found the minimal temperature is returned |
| return min_temp; | return min_temp; | ||
| } | } | ||
| Line 94: | Line 94: | ||
| <code c> | <code c> | ||
| - | // | + | // Example program of the thermistor of Sensors module |
| - | // Example program of the thermistor of Sensors module. | + | // The temperature is displayed on the LCD |
| - | // The temperature is displayed on the LCD. | + | |
| - | // | + | |
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include < | + | #include < |
| + | #include < | ||
| + | |||
| + | // Robotic Homelab II | ||
| + | //#define ADC_CHANNEL 2 | ||
| + | |||
| + | // Robotic Homelab III | ||
| + | #define ADC_CHANNEL 14 | ||
| - | // | ||
| // Main program | // Main program | ||
| - | // | ||
| int main(void) | int main(void) | ||
| { | { | ||
| Line 112: | Line 115: | ||
| char text[16]; | char text[16]; | ||
| - | // Setting the LCD | + | // Initialization of LCD |
| - | lcd_alpha_init(LCD_ALPHA_DISP_ON); | + | lcd_gfx_init(); |
| - | + | ||
| - | // Cleaning the LCD | + | |
| - | lcd_alpha_clear(); | + | |
| + | // Clearing the LCD and setting backlight | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_backlight(true); | ||
| + | |||
| // Name of the program | // Name of the program | ||
| - | lcd_alpha_write_string("Termomeeter"); | + | lcd_gfx_goto_char_xy(1, |
| + | lcd_gfx_write_string("Thermometer"); | ||
| // Setting the ADC | // Setting the ADC | ||
| Line 129: | Line 134: | ||
| // Reading the 4 times rounded values of the voltage of the | // Reading the 4 times rounded values of the voltage of the | ||
| // thermistor | // thermistor | ||
| - | value = adc_get_average_value(2, 4); | + | value = adc_get_average_value(ADC_CHANNEL, 4); |
| // Converting the values of ADC into celsius scale | // Converting the values of ADC into celsius scale | ||
| temperature = thermistor_calculate_celsius(value); | temperature = thermistor_calculate_celsius(value); | ||
| - | // Converting the temperature in to text. | + | // Converting the temperature in to text |
| - | // To display the degree sign, the octal variable is 337. | + | // To display the degree sign, the octal variable is 56 |
| - | sprintf(text, | + | sprintf(text, |
| - | // Displaying the text in the beginning of the second | + | // Displaying the text in the beginning of the third row of the LCD |
| - | lcd_alpha_goto_xy(0, 1); | + | lcd_gfx_goto_char_xy(5, 3); |
| - | lcd_alpha_write_string(text); | + | lcd_gfx_write_string(text); |
| + | |||
| + | hw_delay_ms(1000); | ||
| } | } | ||
| + | return 0; | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | ===== Extra ===== | ||
| - | |||
| - | * {{: | ||
| - | |||