Thermometer using STM32 Nucleo
I will create an application to realize a thermometer using an inexpensive thermistor as a sensor element with an STM32 MCU (Nucleo board). It is a simple program, but it can be developed into various other arithmetic applications using floating point arithmetic with the Cortex-M3 core MCU.
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.
The STM32F103RB in the NUCLEO-F103RB is a Cortex-M3 core MCU and does not have a hardware DSP (Digital Signal Processor) for floating-point operations, but the operations are performed by software. This can be achieved without any special awareness of it. Although this may not be a problem for relatively low-speed applications such as the temperature calculation in this sample, for applications that require high-speed floating-point operations, such as an FFT analyzer, a high-end MCU such as the Cortex-M4 with a hardware DSP would be advantageous.