coffee/barista/ntc.c
2025-10-13 21:09:06 +02:00

45 lines
1.3 KiB
C

/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
* SPDX-License-Identifier: GPL-3.0-or-later */
#include <math.h>
/* Steinhart-Hart Thermistor Coefficients, used to convert resistance into
* temperature.
*
* The current NTC sensor has 102kOhm at 24C but I don't know the specific
* model, so the coefficients are computed for this NTC sensor instead:
* https://www.tme.eu/Document/f9d2f5e38227fc1c7d979e546ff51768/NTCM-100K-B3950.pdf
*
* The table seems to match what I would expect. Their R2 resistor is 6.8 kOhm,
* which yields a cuttof temperature of around 98.5 C at exactly half voltage
* (where the ADC would have more precision).
*
* In any case, we can calibrate the original NTC sensor by taking three
* temperature points. See:
* https://www.thinksrs.com/downloads/programs/therm%20calc/ntccalibrator/ntccalculator.html
*/
#define C1 0.7740577674e-3
#define C2 2.073449619e-4
#define C3 1.263502259e-7
/* Return the temperature in celsisus */
float
ntc_temp(float R)
{
/* Computing the log is slow, we may want to build a table */
float logR = log(R);
float T = (1.0 / (C1 + C2*logR + C3*logR*logR*logR));
float Tc = T - 273.15;
return Tc;
}
/* Return resistance in Ohms */
float
ntc_resistance(int Vo)
{
float R1 = 6.8e3; /* Resistor for voltage divider */
float R2 = R1 * (1023.0 / (float)Vo - 1.0);
return R2;
}