How do you handle ADC measurements for a sensor that has a nonlinear response?
Many sensors — NTC thermistors, photodiodes, pH probes, gas sensors — have inherently nonlinear voltage-to-measurement transfer functions. The ADC faithfully digitizes the voltage, but converting that voltage to a meaningful physical quantity (temperature, light intensity, pH) requires linearization in software.
Lookup table with interpolation (most common in embedded): Store a table of ADC values and corresponding physical measurements at known calibration points, then use linear interpolation between adjacent entries for intermediate values. For an NTC thermistor, a 20-30 entry table spanning -40 to +125 degrees C with interpolation provides better than 0.1-degree accuracy across the full range. The computation is a single integer subtract, multiply, and divide — no floating point required. This approach handles arbitrary nonlinearities, uses modest memory (60-120 bytes for a 30-entry 16-bit table), and executes in deterministic time.
Mathematical model: Apply the sensor's characteristic equation directly. For NTC thermistors, the Steinhart-Hart equation (1/T = A + B*ln(R) + C*(ln(R))^3) provides excellent accuracy with just three calibration coefficients. For RTD sensors, the Callendar-Van Dusen equation is standard. The advantage is minimal memory usage (just the coefficients), but the disadvantage is significant: these equations require floating-point logarithms and divisions, which can take hundreds of microseconds on Cortex-M0/M3 cores without an FPU. On Cortex-M4F or M7 with hardware FPU, the computational cost is acceptable.
Piecewise linear approximation: Divide the sensor range into segments and apply a different linear equation (slope + offset) per segment. This is a middle ground — simpler than full interpolation, more memory-efficient than a dense LUT, and avoids floating-point math. The segment boundaries should be placed where the nonlinearity is strongest (the curve changes direction most rapidly) to minimize error. Always validate the linearization against known reference points during production calibration, not just against datasheet typical curves.
Source: ADC Q&A
