Thermistors are elements whose resistance changes with temperature, and those with NTC characteristics are suitable for thermometers. The NTC thermistor has a resistance value that decreases slowly as the temperature rises, but the change is not linear and is non-linear.
The thermistor used in this project is 103AT-2 with a B constant of 3435K and a thermistor resistance R25 of 10kΩ at the reference temperature T25 (25°C). The equation below shows the relationship between thermistor resistance R and temperature T. The temperature in this equation is absolute temperature (K).
Temperature and resistance are functions of the natural logarithm, and a logarithmic table of the graph shows a gradual decrease in resistance as temperature rises. It can be confirmed that the resistance values in the datasheet and the values calculated by the above formula are almost identical.
To measure temperature with MCU, the resistance of the thermistor must be known. To know the resistance value, MCU reads the analog value converted into a voltage and converts it into a resistance value in MCU program.
In this sample, a thermistor R and a resistor Ro (of the same value as R25 of the thermistor, 10kΩ) are connected in series between MCU voltage Vdd (3.3V) and GND as the upper limit voltage. Since the intermediate voltage V varies with temperature change, this voltage is read by AD conversion and converted to a resistance value using the following formula.
Once the resistance of the thermistor is known, it can be input into an equation to convert it to temperature, but this equation requires arithmetic operations using the natural logarithm. Because of the nonlinear nature of this operation, it is not performed with ordinary integers, but with floating-point variables.
To achieve this operation, you must first include "math.h" to use the natural logarithm log function. Next, calculate the temperature in a floating-point variable. The resulting calculated value is sent to the monitor via serial communication and displayed as a decimal point. The thermistor resistance-temperature relationship is absolute temperature, so subtract 273.15 to convert to Celsius.
In this sample program, the voltage (ADCValue_IN0) corresponding to the resistance value is captured by AD conversion, but since continuous conversion is specified in this sample program, it can be acquired at any timing and is converted to the resistance value in line 141. Line 142 sets the number of digits to be displayed.
Line 143 performs a floating-point operation using the log function to determine the temperature, and line 144 sets the number of digits to be displayed via USART.
Thermistor resistance and temperature are displayed in 0.5 second cycles. It can be confirmed that the values are almost the same as those in the thermistor temperature-resistance characteristics table. Pinch the thermistor with your finger and you will see the temperature rise to near body temperature.
This sample program is a simple one, but deals with an example using floating-point arithmetic on a Cortex-M3 core with STM32F103RB. Other applications using non-linear operations such as SIN and COS functions are also applicable.