import math
R_PULLUP = 10000
ADC_MAX = 4095
TOPOLOGY = "ntc-low"
R25 = 10000
BETA = 3950
def adc_to_temp_c(adc: int) -> float:
if adc <= 0 or adc >= ADC_MAX:
raise ValueError("ADC reading saturated at rail")
ratio = adc / ADC_MAX
r_ntc = R_PULLUP * ratio / (1.0 - ratio) if TOPOLOGY == "ntc-low" else R_PULLUP * (1.0 - ratio) / ratio
inv_t = (1.0 / 298.15) + math.log(r_ntc / R25) / BETA
return (1.0 / inv_t) - 273.15
# Example spot check at 45 C -> ADC ~ 1241
print(f"{adc_to_temp_c(1241):.2f} C")
Choose a bias resistor near the NTC resistance at your most critical temperature to maximize mV/°C sensitivity and minimize quantization error.
Fast integer-only NTC decoding on STM32 Cortex-M microcontrollers
STM32F0, F1, G0 and F4 microcontrollers feature 12-bit SAR ADCs (0–4095) whose DMA channels can stream thermistor channels continuously into RAM. Evaluating logf() inside a timer ISR bloats flash on Cortex-M0 parts.
The generated 65-entry C99 table consumes only 130 bytes of flash and uses a single 6-bit right shift (`adc >> 6`) plus integer linear interpolation to return temperature in 0.1 °C increments.
At 45 °C with a 10 kΩ B3950 NTC and 10 kΩ pull-up on STM32 3.3 V, R_ntc = 4.37 kΩ, V_adc = 1.003 V (ADC code 1,245), resolving 0.025 °C per LSB.
Frequently asked questions
What STM32 ADC sampling time should I configure for a 10 kΩ NTC divider?
The worst-case source impedance of a 10 kΩ pull-up divider is 5 kΩ at mid-scale and up to 10 kΩ when hot/cold; select at least 28.5–55.5 ADC clock cycles plus a 100 nF filter cap.
Can I connect the NTC pull-up to 3.3 V VDD if VDDA has a ferrite bead?
Always pull the NTC up to the filtered VDDA / VREF+ rail rather than noisy digital VDD so the measurement remains strictly ratiometric.
How much RAM does the generated C99 LUT use?
Zero RAM! Declared `static const int16_t`, the array lives entirely in flash (.rodata) and executes in deterministic O(1) time.