This python script is used to generate a ready-to-compile C source and header file containing a thermistor temperature calculation algorithm, using a lookup table method. It is intended for use by small resource-constrained microcontrollers.
LICENSE: MIT
- create and edit json file with your table parameters (see example.json)
- run the generator script
- copy the generated source files (.c and .h) to your project
- call the function from your program when you need to compute the temperature from ADC value
Thermistor
Vref ----/\/\/\/----+
|
+----> to ADC input
|
GND ----/\/\/\/----+
Rpulldown
Field | Description |
---|---|
board | name of your board or project (cosmetic) |
thermistor | thermistor part number (cosmetic) |
Tstart | lowest temperature of table |
Tstop | highest temperature of table (exclusive) |
Tstep | temperature step size |
Tnominal | thermistor nominal temperature (from data sheet) |
Rnominal | thermistor nominal resistance (from data sheet) |
Rpulldown | value of the pull-down resistor |
beta | thermistor B value (from data sheet) |
counts | max counts of the ADC |
NOTES:
- All temperatures are in C.
- Cosmetic fields are shown in generated comments but have no other function.
- The size of the table is adjusted by tweaking
Tstart
,Tstop
, andTstep
. You can adjust the tradeoff between how well the curve fits and how much memory is needed for the table. - For values outside the range of the lookup table, the algorithm interpolates linearly, using the end segments. The error of the calculated temperature will increase the further the actual measurement is outside the table range.
Assumes python3.
./generator myboard.json
Add the source files to your project. Include the header where needed. Call the function when needed to compute temperature from ADC value.
API | Definition |
---|---|
prototype | int16_t adc_to_temp(uint16_t adc); |
parm adc |
unsigned 16-bit ADC value for thermistor circuit voltage |
returns | signed 16-bit temperature in C |
#include <stdint.h>
#include "thermistor_table.h"
...
...
uint16_t adcval = read_adc(TEMP_SENSOR_CHANNEL);
int16_t temp_in_C = adc_to_temp(adcval);
...
You can do a quick check of the lookup table using the simple test.c
program. Edit the source of test.c
to cover the range you want to check.
Then compile and run with:
gcc -o test test.c thermistor_table.c
./test
It will print a table. You can examine the output and verify the ADC to temperature curve is correct.