How it works
An n-bit ADC divides its reference voltage into 2ⁿ steps. The size of one step, the least significant bit (LSB), is the finest voltage change it can detect.
Real ADCs also have offset, gain error and noise, and some (such as the ESP32's) are noticeably non-linear near the ends of their range, so calibrate against a known voltage for accurate measurements.
LSB = Vref / 2ⁿ
V = code × Vref / 2ⁿ
code = V / Vref × 2ⁿWorked example
A 12-bit ADC with a 3.3 V reference has 4,096 steps of 3.3 / 4096 = 0.806 mV. A reading of 2048 corresponds to 2048 × 0.806 mV = 1.65 V, half the reference.