import math
R_PULLUP = 10000
ADC_MAX = 4095
TOPOLOGY = "ntc-high"
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 30 C -> ADC ~ 2270
print(f"{adc_to_temp_c(2270):.2f} C")
Choose a bias resistor near the NTC resistance at your most critical temperature to maximize mV/°C sensitivity and minimize quantization error.
Keep ESP32 NTC voltages inside the linear ADC sweet spot
Classic ESP32 ADCs exhibit strong nonlinearity near 0 V (<150 mV) and above ~2.6 V even at 11 dB attenuation. Selecting a divider resistor that centers your target temperature range between 0.5 V and 2.4 V keeps raw readings inside the cleanest calibration zone.
In NTC-high topology (NTC to 3.3 V, pull-down to GND), output voltage rises with temperature—matching intuitive positive-slope telemetry while keeping the thermistor isolated from ground loops.
Use esp_adc_cal / adc_cali_raw_to_voltage() before lookup
Worked example
With a 10 kΩ B3950 NTC wired NTC-high over a 10 kΩ pull-down at 30 °C, R_ntc = 8.06 kΩ and V_adc = 1.827 V (ADC code 2,267)—squarely in the ESP32 ADC linear band.
Frequently asked questions
Why does ESP32 NTC readings jump when Wi-Fi transmits?
Wi-Fi TX bursts cause brief 3.3 V rail dips and ground bounce. Add a 100 nF C0G cap at the ADC pin, use ADC1 (not ADC2), and average 16 samples.
Should I feed calibrated millivolts or raw ADC counts into the LUT?
On classic ESP32, convert raw counts via `adc_cali_raw_to_voltage()` and map `(mv * 4095) / 3300` into the LUT for best linearity; ESP32-S3/C3 ADCs are much more linear natively.
Does NTC-high vs NTC-low change self-heating?
No. Total divider current and I²R power dissipated inside the thermistor depend only on Vref, R_bias and R_ntc, regardless of which resistor sits on top.