import math
R_PULLUP = 10000
ADC_MAX = 1023
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 25 C -> ADC ~ 512
print(f"{adc_to_temp_c(512):.2f} C")
Choose a bias resistor near the NTC resistance at your most critical temperature to maximize mV/°C sensitivity and minimize quantization error.
Measure temperature cleanly on AVR 10-bit Arduino analog pins
ATmega328P boards (Arduino Uno and Nano) provide a 10-bit SAR ADC (0–1023) referenced to the 5 V supply. With a 10 kΩ pull-up and 10 kΩ NTC, each ADC count near room temperature corresponds to less than 0.09 °C—more than enough for thermostats, fan controllers and battery monitors.
Because ATmega328P has no hardware FPU and only 32 KB flash, replacing `log()` with the generated 33-entry `int16_t` lookup table saves over 1.5 KB of program memory and runs in microseconds.
Self-heating at 25 °C, 5 V = 2.5² ÷ 10,000 = 0.625 mW
LUT step = 1024 ÷ 32 = 32 counts (adc >> 5)
Worked example
At 25 °C on a 5 V Arduino, V_adc = 2.500 V (ADC code 512) with −55.3 mV/°C sensitivity and 0.088 °C/LSB quantization resolution.
Frequently asked questions
Does 5 V self-heating skew a 10 kΩ NTC reading?
At 25 °C, 0.625 mW on a small bead with a 2 mW/°C dissipation constant adds ~0.3 °C in still air. You can drive the pull-up from a GPIO pin and power it only during sampling to eliminate self-heating.
Can I store the generated C99 LUT in PROGMEM on AVR?
Yes—add `PROGMEM` to the array declaration and read entries with `pgm_read_word(&ntc_temp_c_x10[idx])` to keep SRAM completely untouched.
Is 10-bit resolution accurate enough at 85 °C?
Yes. At 85 °C a 10 kΩ B3950 with 10 kΩ pull-up still delivers ~0.2 °C/LSB resolution on a 10-bit ADC.