ADCs and DACs: Sampling the Real World
A 12-bit ADC gives you 12 bits of resolution and, if you are careful, about 10 bits of truth.
🎯 By the end you'll understand
What a successive-approximation converter physically does, and why its sample-and-hold capacitor makes source impedance a first-class design parameter rather than a detail. The difference between resolution, accuracy, and effective number of bits, and how to build an error budget out of reference error, quantisation, settling, and noise. Why the ESP32-C3's ADC is the way it is — attenuation ranges, non-linearity, eFuse calibration, and the part of the range you must not use. How averaging helps and where it does nothing at all. And Beacon's battery-sense path worked end to end, from the divider to a state-of-charge number you would be willing to print on a dashboard.
KEY INSIGHTAn ADC does not measure voltage. It compares charge.
The datasheet's "12-bit" is a statement about the converter's internal arithmetic, not about your measurement. Bits are cheap. Truth is the product of a reference, a settling time, a source impedance and a noise floor — and any one of them can throw away half your bits before the first comparison is made.
A learner wires Beacon's battery divider — two 1 MΩ resistors from the cell to ground — straight onto an ESP32-C3 ADC pin, prints the number, and gets 2.63 V for a cell that a multimeter says is at 3.85 V. Not noisy. Not drifting. Just wrong, by a third, consistently. They swap the resistors, try another pin, change the attenuation setting, and eventually post on a forum that the ESP32's ADC is broken. It is not broken. It is doing exactly what a switched-capacitor sample-and-hold does when you feed it from half a megohm: it is dumping its sampling capacitor's charge onto your node, reading the collapsed voltage, and reporting it faithfully. The chip is honest. The circuit was never asked the right question.
Every reading from a SAR ADC is the answer to: what voltage ended up on my sampling capacitor at the instant the switch opened? Everything you care about — resolution, accuracy, source impedance, settling, aliasing — is a way of asking whether that voltage was the one you meant. Design the path so it is, then the bits are worth having.
CORE CONCEPTInside a SAR converter: a binary search against a reference
A successive-approximation-register ADC has four parts, and once you have seen them the entire behaviour of the thing follows.
A sampling switch and a hold capacitor
A transistor connects the input pin to a small on-chip capacitor — a few picofarads to a few tens of picofarads. It closes for the acquisition time, during which the capacitor is supposed to reach the input voltage, and then it opens. Everything after this instant works on the frozen charge; the input pin is irrelevant until the next acquisition. If the capacitor did not reach the input voltage during acquisition, the conversion is exact — and exactly wrong.
A reference voltage
The converter has no idea what a volt is. It only knows fractions of its reference. Every output code means "this fraction of VREF". A reference that is 3 % high makes every reading 3 % low, in perfect 12-bit detail. This is why a converter's accuracy is usually its reference's accuracy in disguise.
An internal DAC and a comparator
The DAC generates a trial voltage; the comparator says "higher" or "lower". In practice the DAC and the hold capacitor are the same array of capacitors, switched between the reference and ground — which is why SAR converters are called charge-redistribution converters and why they are so power-efficient.
The successive-approximation register
A binary search. Try half of full scale; keep or discard that bit; try a quarter; and so on. An N-bit conversion takes N comparisons, one clock each, plus the acquisition time. This is why SAR converters have a hard, predictable conversion time, and why their resolution costs latency linearly rather than exponentially.
The other architecture worth knowing is the sigma-delta (ΣΔ) converter, and the contrast sharpens what a SAR is. A ΣΔ modulator does not do a binary search. It runs a crude 1-bit comparator at a very high rate — often megahertz — inside a feedback loop that arranges for the average of its bit stream to track the input. A digital filter then decimates that stream down to, say, 16 or 24 bits at a few hundred samples per second. Because the decimation filter is a genuine low-pass, a ΣΔ converter has anti-aliasing largely built in, and because the loop shapes quantisation noise up out of the band of interest, it can reach far more bits than its comparator suggests. The price is latency (the filter has a settling time, typically several conversion periods after a channel change) and continuous current draw. That trade decides the choice:
| SAR | Sigma-delta | |
|---|---|---|
| Typical resolution | 8–18 bits | 16–24 bits |
| Latency | One conversion — microseconds, and the result belongs to a single instant | Several output periods after a step or channel change; the result is an average over a window |
| Anti-aliasing | You must provide it (Lesson 2.3) | Mostly built into the decimation filter, though the modulator still aliases near multiples of its own clock |
| Multiplexing many channels | Natural — one conversion each | Awkward — the filter must re-settle on every channel change |
| Power | Scales with sample rate; can be sub-microamp at low rates | Runs a fast loop continuously; the ADS1115 draws around 150 µA converting |
| Fits | MCU-integrated, event-driven, one-shot reads on a battery device | Weigh scales, thermocouples, precision instruments, anything slow and small |
Beacon wakes for tens of milliseconds once a minute and needs one number. That is a SAR's shape of problem, and the SAR is already inside the ESP32-C3 for free. The interesting question is not which architecture; it is whether the free one is good enough, and answering that needs an error budget.
DEFINITIONResolution, accuracy, and the number in between
Three words that get used interchangeably and mean entirely different things.
How finely the converter can divide its range. An N-bit converter over a full-scale range FS has LSB = FS / 2N. For the ESP32-C3 at 12-bit with 12 dB attenuation, FS ≈ 2500 mV and one LSB is 0.61 mV. Resolution is a property of the arithmetic and is free. It says nothing about whether the code is correct.
How close the reported voltage is to the true voltage. It is built from offset error (every code shifted by a constant), gain error (the slope wrong, usually the reference's fault), integral non-linearity, INL (the transfer curve bending away from a straight line) and differential non-linearity, DNL (individual code widths unequal; DNL worse than −1 LSB means a code that never appears — a missing code). Accuracy is a property of the silicon, the reference, and your calibration.
The resolution a perfect converter would need in order to be as bad as yours. If your total error window is E over a full scale FS, you have achieved roughly log₂(FS / E) bits. A 12-bit converter whose readings are uncertain by ±20 mV over a 2500 mV range is delivering log₂(2500/40) ≈ 6 bits of truth, and the remaining six are decoration. Publishing the raw 12-bit number in that state is not precision, it is theatre.
Build the budget the way you would build a mechanical tolerance stack. Some errors are systematic: they are the same on every reading, so they add arithmetically and averaging does nothing to them. Reference and gain error, offset, INL, and the settling error we are about to meet are all systematic. Others are random: thermal noise, coupled interference, and the residual jitter of the comparator. Random terms add in quadrature (root of the sum of squares) and shrink as √N when you average N samples.
Two limits people run into. First, averaging shrinks random error only. Sixty-four samples through a reference that is 3 % high gives you a beautifully stable reading that is 3 % high. Second, quantisation error is not automatically random. If the input is perfectly quiet and sits inside one code, all 64 samples return the identical code and the average is that code — no improvement at all. Averaging buys resolution only when there is at least about one LSB of noise to dither the input across code boundaries. This is the honest version of the "oversample for free bits" claim: each 4× in sample count buys one bit, provided the noise floor is at least an LSB. Beacon's battery node, filtered to a 3.2 Hz bandwidth by its 100 nF capacitor, is quiet enough that this condition often fails — which is worth knowing before you write an averaging loop and expect miracles.
HOW IT WORKSThe charge the ADC steals from your circuit
Here is the mechanism behind the 2.63 V reading in the opening. When the sampling switch closes, the on-chip capacitor CS — call it 10 pF; the exact number is rarely published and 10 pF is a reasonable working figure for an MCU SAR — is connected to your node. If your node has almost no capacitance of its own, the two capacitors share charge instantly, and the shared voltage is
Vshared = Vnode × Cnode / (Cnode + CS). The node then refills from the source through RS with τ = RS·(Cnode + CS) while the switch is still closed, and after it opens, the node recovers with τ = RS·Cnode before the next sample.
Take Beacon's divider with nothing but the pin's own stray capacitance on it — call it 20 pF for the pad, the trace and the pin. Then Cnode/(Cnode + CS) = 20/30 = 0.667. The 1.925 V on the tap collapses to 1.28 V the instant the switch closes. Can the source refill it during acquisition? The refill time constant is 500 kΩ × 30 pF = 15 µs, and a typical acquisition window is around a microsecond, so it recovers 6 % of the deficit. The converter reads about 68 % of the true value — 1.32 V instead of 1.925 V, which doubles back to 2.64 V at the cell. That is the forum post, predicted from first principles, to two significant figures.
The fix is a local reservoir: put a capacitor at the pin that is enormous compared with CS, so the sample capacitor takes its charge from the reservoir rather than from your high-impedance source. Beacon's 100 nF is 10,000× CS. Now the sharing ratio is 0.9999 and each sample dips the node by only 0.19 mV — a third of an LSB. The catch, and it is the reason this section exists, is that the reservoir has to be refilled by that same 500 kΩ, with τ = 50 ms. Sample slowly and it always catches up. Sample fast and it never does, and you arrive at a steady state where every reading is low by an amount that depends on your sample rate:
| Sample rate | Steady-state droop at the divider tap | Error referred to the cell | Verdict |
|---|---|---|---|
| 10 Hz | 0.23 mV | 0.5 mV | Invisible |
| 100 Hz | 1.1 mV | 2.1 mV | Tolerable |
| 1 kHz | 9.9 mV | 20 mV | Now larger than every other error term |
| 10 kHz | 92 mV | 184 mV | The gauge is fiction |
Read that table again with the failure mode in mind: nothing changed in the hardware. The same board, the same resistors, the same capacitor, and a firmware author who moved an averaging loop from 10 samples to 1000 samples has shifted the battery reading by 180 mV. There is no message in the log. The reading is smooth, stable, repeatable, and wrong, and it is wrong in the direction that makes a healthy battery look flat. This is the single most common way an MCU ADC lies to a battery-powered product.
The third option is a buffer. An op-amp follower (Lesson 2.2) presents perhaps 10 Ω of output impedance to the ADC, so the sample capacitor charges in a fraction of a microsecond and the source never notices. It also costs quiescent current — even a "nanopower" part like the TLV8802 idles in the hundreds of nanoamps, and a common jellybean like the MCP6001 draws around 100 µA, which is 12× Beacon's entire sleep budget if you leave it powered. On a mains-powered instrument the buffer is the obvious answer. On Beacon it is the wrong one, and the reservoir capacitor plus a slow read is the right one. Knowing why the textbook answer is wrong here is the point.
SEE ITThe ADC truth machine: what your battery gauge actually knows
The cell is at exactly 3.850 V, sitting on the flat middle of a LiPo discharge curve where 40 mV is roughly 10 % of state of charge. The divider halves it to 1.925 V. Choose the converter, the reference quality, how the node is driven, the sample rate, and how many samples you average. The widget builds the error budget, converts it into an effective number of bits, and then does the thing that matters: it maps your voltage uncertainty onto the discharge curve and shows you how much of the battery you have actually measured.
Three experiments worth doing before you move on. One: leave everything at Beacon's real configuration but slide the sample rate from 10 Hz to 10 kHz and watch a hardware-free 180 mV of error appear. Two: set the source to "divider straight to the pin" and then try to rescue it with 16 bits and 64× averaging — the effective-bits readout barely moves, because the settling error is systematic and every extra bit is spent resolving a wrong number more finely. Three: take the winning configuration and switch the reference from calibrated back to raw. One toggle, and a gauge that could call the charge to ±10 % now reports "somewhere between a quarter and three-quarters full". The reference is usually the most expensive part of an ADC and the last thing anyone thinks about.
IN PRACTICEThe ESP32-C3's ADC, honestly
Espressif's own documentation is refreshingly blunt: the on-chip ADC is not a precision instrument, and its raw output needs calibration to mean anything in millivolts. Here is what you need to know before you design a measurement around it.
Two units, and one of them is not really available
The ESP32-C3 has ADC1 on GPIO0–GPIO4 and ADC2 with a single channel on GPIO5. ADC2 shares hardware with the Wi-Fi radio: when Wi-Fi is running, an ADC2 read can fail outright. Any measurement you care about goes on ADC1. Beacon has Wi-Fi in its future for OTA (canon: opt-in OTA over Wi-Fi at 4.9), so this is not a hypothetical.
Attenuation sets the input range, in four steps
The core reference is around 1.1 V; an input attenuator scales your signal down into it. ESP-IDF's suggested measurement ranges for the C3 are roughly 0–750 mV at 0 dB, 0–1050 mV at 2.5 dB, 0–1300 mV at 6 dB, and 0–2500 mV at 12 dB. Note the name: the setting was called ADC_ATTEN_DB_11 until ESP-IDF 5.2 renamed it ADC_ATTEN_DB_12. Old blog posts and old code use the old name, and the compiler will tell you.
The ends of the range are not usable
Below roughly 100 mV and above the top of the suggested range, the transfer curve flattens: the readings compress and eventually stop moving altogether while the real voltage keeps rising. There is no error flag. Design so your signal lives comfortably inside the suggested window with margin at both ends — which is exactly why Beacon's divider halves a 3.0–4.2 V cell into 1.50–2.10 V rather than trying to use more of the range.
And not every ADC1 pin is a legal place to put a divider. GPIO2 is one of the C3's strapping pins and must read high at reset. Beacon's divider pulls its sense node to ground through the lower 1 MΩ whenever the enable switch is off, so a divider on GPIO2 would latch the wrong boot mode at every power-up — and even with the switch on, half of a 3.7 V cell is 1.85 V, below the 0.75 × 3.3 = 2.48 V that pin needs to read high. The battery sense goes on GPIO4, which is in the RTC domain, on ADC1, and not a strapping pin. GPIO3, the other obvious candidate, already carries the accelerometer's motion interrupt.
Calibration lives in eFuse, and you must ask for it
Each chip is characterised at the factory and the coefficients are burned into eFuse. On the C3 the driver applies them through the curve-fitting scheme — a polynomial correction, not the simple two-point line fit used on the original ESP32. You get it by creating a calibration handle and passing raw counts through adc_cali_raw_to_voltage(). Skip that call and you are reading uncorrected counts scaled by a nominal factor, which is the ±3 % case in the widget.
Non-linearity survives calibration
Curve fitting removes the bulk of the chip-to-chip gain and offset spread. It does not make the converter linear to the LSB, and Espressif does not promise it will. Treat the calibrated result as good to roughly a percent and verify on your own units before you promise a customer better. If a design genuinely needs a tenth of a percent, that is a signal to add an external converter with its own reference, not to average harder.
// Beacon: VBAT/2 arrives on GPIO4 = ADC1 channel 4. // Divider is 1 MΩ / 1 MΩ, so the pin sees 1.50…2.10 V over // a 3.0…4.2 V cell — inside the 12 dB window with margin. #include "esp_adc/adc_oneshot.h" #include "esp_adc/adc_cali.h" #include "esp_adc/adc_cali_scheme.h" static adc_oneshot_unit_handle_t s_adc; static adc_cali_handle_t s_cali; esp_err_t batt_sense_init(void) { adc_oneshot_unit_init_cfg_t unit = { .unit_id = ADC_UNIT_1 }; ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit, &s_adc)); adc_oneshot_chan_cfg_t ch = { .atten = ADC_ATTEN_DB_12, // 0…2500 mV (was DB_11 pre-IDF 5.2) .bitwidth = ADC_BITWIDTH_DEFAULT, }; ESP_ERROR_CHECK(adc_oneshot_config_channel(s_adc, ADC_CHANNEL_4, &ch)); adc_cali_curve_fitting_config_t cal = { .unit_id = ADC_UNIT_1, .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_DEFAULT, }; // Returns ESP_ERR_NOT_SUPPORTED if this chip has no eFuse cal data. return adc_cali_create_scheme_curve_fitting(&cal, &s_cali); } // Call only after the divider has been enabled for ≥ 250 ms // (5τ of 500 kΩ × 100 nF — the rule from Lesson 2.3). int batt_read_mv(void) { int raw = 0, mv = 0, acc = 0; for (int i = 0; i < 16; i++) { adc_oneshot_read(s_adc, ADC_CHANNEL_4, &raw); acc += raw; vTaskDelay(pdMS_TO_TICKS(5)); // 200 Hz: let the reservoir refill } adc_cali_raw_to_voltage(s_cali, acc / 16, &mv); return mv * 2; // undo the 1:2 divider }
The line that most engineers omit is the delay inside the averaging loop, and it is there for the reason the widget shows: sixteen back-to-back reads on a 500 kΩ / 100 nF node drain the reservoir faster than it refills. Five milliseconds between reads holds the steady-state droop to about 2 mV at the tap, 4 mV at the cell. The whole burst then takes 80 ms — and since the divider has to be enabled 250 ms before the first valid sample anyway, those 80 ms sit at the end of a wait you were already serving. A "faster" loop would be quicker, wrong, and no earlier.
COMPARISONGoing the other way: DACs, and the one the ESP32-C3 does not have
A DAC does the inverse job: a code in, a voltage out. Inside, it is usually a resistor ladder or a switched-capacitor array tapping the same reference. The specifications rhyme with the ADC's — resolution, INL, DNL, offset, gain, settling time — with two additions that matter in practice: output drive (many integrated DACs cannot source more than a milliamp and need a buffer to drive anything real) and glitch energy (at a major code transition, such as 0x7FFF to 0x8000, the internal switches change state at slightly different times and the output spits a narrow spike that no amount of resolution fixes).
Now the part that surprises people migrating from the original ESP32: the ESP32-C3 has no DAC at all. The classic ESP32 and the ESP32-S2 have two 8-bit DACs; the C3 and the S3 have none. If you need an analog output on a C3, you generate it with PWM and filter it — which turns out to be a neat closing exercise in everything this stage has taught.
The LEDC peripheral divides an 80 MHz clock, so duty resolution and frequency trade directly: resolution bits ≈ log₂(80 MHz / fPWM). At 78 kHz you get 10 bits; at 5 kHz, 13 bits. Take 78 kHz and 10 bits, then low-pass it. The peak-to-peak ripple after a first-order RC is approximately Vripple ≈ V·D(1−D)/(f·RC), worst at 50 % duty where it is V/(4·f·RC).
With R = 10 kΩ and C = 100 nF (τ = 1 ms): ripple = 3.3/(4 × 78 125 × 10−3) = 10.6 mV peak-to-peak, and the output settles to 1 % in 4.6τ = 4.6 ms. Ten bits of duty resolution is 3.2 mV per step, so the ripple is three steps wide — the last two bits are meaningless. Push C to 1 µF (τ = 10 ms): ripple falls to 1.06 mV, below one step, but settling grows to 46 ms. There is the whole trade in two lines: a PWM DAC's resolution and its speed are the same knob, and it is Lesson 2.3's corner frequency wearing a different hat.
When is that acceptable? For anything slow and monotonic — a bias voltage, a contrast setting, a reference for a comparator, an analog dimming input to an LED driver — a PWM DAC is genuinely fine and costs two passives. When is it not? Audio, closed-loop control faster than a few tens of hertz, or anything where the ripple lands in a band you are also measuring. In those cases you add a real DAC over I2C or SPI (an MCP4726 or similar) and pay for it in current and BOM. Beacon needs no analog output at all, which is the best outcome: the cheapest converter is the one the requirements let you delete.
BEACONThe battery-sense path, end to end
Beacon's schematic v0 (assembled in Lesson 2.9) carries a battery-voltage sense chain that now has a complete justification, part by part. The cell is a 1000 mAh LiPo running 4.2 V down to about 3.3 V. The chain is: cell → a P-channel switch → 1 MΩ → sense node → 1 MΩ → ground, with 100 nF from the sense node to ground, and the sense node on GPIO4 (ADC1 channel 4).
| Part | Value | Why this number |
|---|---|---|
| Divider ratio | 1 MΩ : 1 MΩ (÷2) | Maps 3.0–4.2 V onto 1.50–2.10 V, comfortably inside the 12 dB attenuation window (0–2500 mV) with margin at both ends — away from the compressed top of the range. |
| Resistor size | 1 MΩ each | 2.1 µA at 4.2 V while enabled. A 100 kΩ pair would draw 21 µA — more than half of Beacon's 38 µA average budget if it were ever left on, and this is precisely why the divider is switched at all. |
| Reservoir cap | 100 nF | 10,000× the ADC's sampling capacitor, so each sample dips the node by about 2 µV instead of collapsing it by a third. It also filters the node to 3.2 Hz, which buries the regulator's switching ripple (Lesson 2.3). |
| Enable switch | P-channel MOSFET | Removes the 2.1 µA when the tag is asleep. Over 60 s, a continuously-on divider would add 2.1 µA to an 8 µA sleep floor and cut the canon's 3-year theoretical life to roughly 2.8 years — over a quarter of the sleep budget for a measurement taken once a minute. |
| Settling rule | ≥ 250 ms after enable | 4.6 × (500 kΩ × 100 nF) = 230 ms to 1 %, rounded up for the switch's own turn-on. This is a note on the schematic because it is a firmware requirement created by a hardware choice. |
| Read cadence | 16 samples, 5 ms apart | Holds the reservoir droop near 2 mV at the tap while giving the averaging something to work on. Back-to-back reads at 10 kHz would cost 184 mV at the cell instead, with no warning of any kind. |
What accuracy does this buy? With eFuse curve-fit calibration, the reference and linearity contribute about 1 % of reading — roughly 19 mV at the tap, 38 mV at the cell. Settling droop at 200 Hz is about 2 mV at the tap, 4 mV at the cell. Quantisation is 0.61 mV. The realistic total is around ±45 mV at the cell, dominated entirely by the calibration term. On the flat middle of a LiPo curve that is about ±10 % of state of charge, which is the right answer for a product whose battery message is "fine / getting low / replace soon" and the wrong answer for one that promises a percentage to the digit. Beacon's payload carries battery millivolts (canon: batt mV uint16), and the dashboard is free to bucket them; what the firmware must not do is imply a precision the reference cannot support.
You could replace the divider entirely by measuring the cell through the regulator's own feedback, or by adding a fuel-gauge IC such as the MAX17048, which integrates current as well as voltage and gives a genuinely better state of charge on a flat discharge curve. It costs roughly a dollar and a few microamps — against R5's IDR 150,000 unit BOM cap that is real money, and against an 8 µA sleep floor those microamps are real too. Beacon's answer is no, for this revision, and the reason goes on the sheet: "voltage-only gauge; ±10 % SoC accepted; revisit if the pilot shows users care." That is a design decision recorded with its rationale, which is the only kind that survives a year.
WEAKNESSWhere this bites
The single most common ADC bug in hobby and early-production firmware is voltage = raw * 3.3 / 4095. Three things are wrong with it at once: the full scale at 12 dB attenuation is about 2.5 V, not 3.3 V; the transfer curve is not the straight line that formula assumes; and the chip-specific eFuse correction is being thrown away. The result looks plausible, which is what makes it dangerous — it is wrong by a few percent and by a different few percent on every unit you build, so it passes on your desk and produces a scattered field of battery readings across a 25-unit pilot. Use adc_cali_raw_to_voltage(), and if the calibration handle fails to create, treat that as a hardware fault rather than falling back to arithmetic.
Averaging is the first thing everyone reaches for and it addresses exactly one of the four error terms. It will not touch a reference error, will not touch INL, will not touch a settling error, and — on a quiet, well-filtered node — will not even touch quantisation, because there is no dither to average across. Worse, on a high-impedance node it actively causes error, because more samples per second means more charge stolen per second from a reservoir that refills through half a megohm. If a reading is stable but wrong, more samples make it more stably wrong. Find out which term dominates before you spend CPU on it.
Multiplexed ADCs keep the previous channel's charge on the sample capacitor. Alternate between a low-impedance 0.5 V node and Beacon's 1.9 V divider and each divider reading is pulled toward whatever the other channel was, by an amount that depends on the acquisition time and the order of the scan. The symptom is a reading that changes when you add or remove an unrelated channel from the sequence, which sends people hunting for firmware bugs in the wrong file entirely. The fixes are a reservoir capacitor on every high-impedance channel, a dummy conversion discarded before each real one, or simply not putting a megohm-class source on a multiplexed converter.
KEY INSIGHTHow this connects
- Kirchhoff's Laws and Circuit Analysis by Hand (1.3) — the Thevenin resistance you learned to compute there is the R that decides whether the sample capacitor ever reaches your signal. A divider's source impedance is not a footnote to the ADC; it is the ADC's most important input.
- Filters: RC, Noise, and Bandwidth (2.3) — the same 100 nF is a reservoir here and an anti-aliasing filter there, and the 250 ms settling rule it imposes came from that lesson. One capacitor, three jobs, one constraint on the firmware.
- Decoupling, Grounding, and Return Paths (2.5) — next: the capacitor that supplies charge to a chip rather than to an ADC, and the nanohenries of copper that decide whether it can. The charge-sharing arithmetic you just used on a 10 pF sample capacitor is about to be used on a 100 mA current step.
Downstream, the ADC's reference error becomes a fleet problem in Stage 6: twenty-five tags, each with its own eFuse coefficients, reporting battery voltages that must be comparable on one dashboard. The settling rule becomes firmware structure in Lesson 3.7's state machine, where the divider must be enabled early in the wake and read late. And the choice not to add a fuel-gauge IC becomes a line in Lesson 6.8's cost model, where every dollar is multiplied by 25 and then, if the pilot succeeds, by rather more.
GLOSSARYKey terms introduced
- LSB (least significant bit)
- The voltage of one code step: full scale divided by 2N. For the ESP32-C3 at 12 bits and 12 dB attenuation, about 0.61 mV.
- Sample-and-hold
- The switch and capacitor at an ADC's input. The converter measures the charge frozen on that capacitor, not the pin, which is why source impedance and acquisition time determine accuracy.
- Charge sharing
- The instantaneous redistribution of charge when the sampling capacitor connects to your node: V drops by the ratio CS/(Cnode + CS). The cure is a local reservoir capacitor thousands of times larger than CS.
- ENOB (effective number of bits)
- log₂(full scale / total error window): the resolution your measurement actually delivers once reference, settling, noise and non-linearity are counted.
- INL / DNL
- Integral non-linearity is the bend of the transfer curve away from a straight line; differential non-linearity is the unevenness of individual code widths. DNL worse than −1 LSB means a code that can never appear.
- Attenuation setting
- On ESP32 parts, an input divider that maps a larger signal into the core reference. On the C3, 0 / 2.5 / 6 / 12 dB give suggested ranges of roughly 750 / 1050 / 1300 / 2500 mV.
- Curve-fitting calibration
- The ESP32-C3's scheme for correcting raw counts using factory coefficients in eFuse, applied through
adc_cali_raw_to_voltage(). Removes most gain and offset spread; does not make the converter linear to the LSB. - Dither
- Noise of at least about one LSB deliberately present at an ADC input, without which averaging cannot improve resolution because every sample lands in the same code.
ACTIVE RECALLCheck your understanding
Answer in your own words first, then reveal.
1A 1 MΩ / 1 MΩ divider on a 3.9 V cell is read directly by an ESP32-C3 ADC pin with no capacitor. The multimeter says 1.95 V at the tap; the ADC reports about 1.34 V. Explain the number, and give two fixes with their costs.›
Charge sharing. The node carries only its stray capacitance — call it 20 pF — and the ADC's sampling capacitor is around 10 pF, so on every acquisition the node collapses to 20/(20+10) = 0.667 of its value. During a roughly 1 µs acquisition window it can only refill through 500 kΩ with τ = 500 kΩ × 30 pF = 15 µs, recovering about 6 % of the deficit, so the held voltage lands near 0.69 × 1.95 ≈ 1.34 V. Note the reading is stable: this is a systematic error, not noise, so it survives any amount of averaging.
Fix one: a 100 nF reservoir at the pin, making the sharing ratio 0.9999. Cost: a 50 ms time constant, hence a 250 ms wait after switching the divider on, plus a droop that grows with sample rate. Fix two: an op-amp buffer, which makes the source impedance 10 Ω and removes the problem entirely. Cost: quiescent current — a common part draws around 100 µA, which is more than ten times Beacon's whole sleep budget — plus BOM and board area. On a battery tag, fix one wins; on mains power, fix two.
2Your firmware averages 64 ADC samples taken back to back at 5 kHz on Beacon's 500 kΩ / 100 nF node. Your colleague's averages 16 samples 2 ms apart. Both use the same hardware and the same calibration. Whose battery reading is better, and by roughly how much?›
The 16-sample version, by tens of millivolts. Fast sampling drains the reservoir faster than 500 kΩ can refill it, and the node settles into a steady-state droop. At 5 kHz the droop at the tap is on the order of 50 mV — about 100 mV referred to the cell — and it is systematic, so averaging 64 of them just averages 64 equally depressed samples. At 500 Hz the droop is under 2 mV. The 64-sample loop also finishes faster, which feels like a win and is the trap: the metric that matters is charge removed per second from a node with a 50 ms refill constant, not samples collected. If you want both the averaging and the speed, the answer is a smaller series resistance (a 100 kΩ divider with a switch), not a tighter loop.
3You need to distinguish 10 % steps of state of charge on a LiPo whose curve is about 40 mV per 10 % in the flat middle. Which of these gets you there: 16-bit resolution, 64× averaging, eFuse calibration, or a 100 nF reservoir? Justify by error term.›
You need a total error window under roughly ±20 mV at the cell, so ±10 mV at the divider tap. Take each: 16-bit resolution shrinks the LSB from 0.61 mV to 0.04 mV, and quantisation was never the problem — it buys nothing. 64× averaging reduces random noise by 8×, but on a node already filtered to 3.2 Hz the random term is a fraction of an LSB — also nothing, and on a high-impedance node it makes things worse. eFuse calibration takes the gain error from about ±3 % (±58 mV at the cell) to about ±1 % (±19 mV) — this is the term that dominates, and correcting it is what actually moves the needle. The 100 nF reservoir removes a 30 %-scale settling error that otherwise swamps everything. So: the reservoir and the calibration are both mandatory; the resolution and the averaging are decoration. Getting materially below ±1 % means leaving the on-chip converter for one with its own reference, which is a BOM and current decision, not a firmware one.
4A colleague migrating firmware from an ESP32 to an ESP32-C3 reports that their analog output "stopped working" and that the ADC now reads high at the top of the range. Diagnose both.›
The analog output: the ESP32-C3 has no DAC. The classic ESP32 and the S2 have two 8-bit DACs; the C3 and S3 have none, so the DAC calls either fail to compile or the port silently dropped them. The replacement is LEDC PWM plus an RC filter, and the trade is explicit: resolution bits ≈ log₂(80 MHz / fPWM), and the filter's time constant sets both the ripple (≈ V/(4·f·RC) at 50 % duty) and the settling time (4.6τ). Ten bits at 78 kHz with 10 kΩ and 100 nF gives about 10 mV of ripple and 4.6 ms of settling.
The ADC reading high near the top: two candidates, and they are distinguishable. If the code was written for the original ESP32's attenuation ranges, the input may now be outside the C3's suggested window for the chosen attenuation, where the transfer curve compresses and eventually clips without any error indication — readings stop tracking the real voltage. If instead the code still calls the old ADC_ATTEN_DB_11 constant or scales raw counts by 3.3/4095, the range assumption itself is wrong. Check the attenuation setting against the C3's own suggested ranges (about 0–2500 mV at 12 dB), confirm the signal sits inside it with margin, and confirm the result is passing through the curve-fitting calibration rather than a hand-written formula.