A warning on missing codes in cheap MCU ADCs, and a tip on compensating
Over the last few weeks I’ve been grading my servo board’s measurement chain against a multimeter, one channel at a time. Every current, voltage and temperature the servo reads goes through the CH32V006’s ADC (analog-to-digital converter), so before trusting any of it I wanted to know what the converter itself does. The datasheet’s ADC table lists clock rates, sample times and supply ranges, and says nothing at all about linearity or offset. So I measured it.
Most of it is fine. A few things surprised me though, and one of them is a calibration bit the manual calls reserved that makes every reading worse. This post collects the quirks I would want to know about if I was starting a project today.
I measured all of this on a CH32V006, but most of it isn’t a WCH thing. The sawtooth, the reference that needs a long sample time, and the codes that go missing when the clock runs too fast all come from how a SAR (successive-approximation) converter works, and you’ll find the same kinds of errors on most microcontrollers. Only the hidden calibration bit is specific to this chip.
If you’re new here, OpenServoCore is my effort to turn cheap MG90S-class servos into networked smart actuators with sensor feedback, cascade control, and its own single-wire bus protocol. The measurements below live in the stage 1 notebooks of the notebooks repo, and most of them ran on one rev 2A dev board, board D, which carries a CH32V006 (WCH’s sub-20-cent RISC-V microcontroller).
TL;DR
If you’re about to trust an MCU’s ADC on a board of your own, here is what I’d want to know going in.
- The converter has a sawtooth error of about 2 counts that repeats every 256 codes. On my chip code 1023 is about 10 codes wide, and 1024 to 1032 never show up at all. The wide codes differ from chip to chip. I correct for it offline and keep it out of the firmware.
- Bit 2 of ADC_CTLR2 is marked reserved, but it still runs an offset calibration like the V003’s. Don’t use it. It moved every channel down about 12 counts, and against a meter that made the readings worse.
- The internal 1.2 V reference on IN8 is the only way to read VDD, which is the ADC’s reference. Give it at least 41.5 cycles of sample time. At 13.5 cycles right after the pot it read 4% high.
- Run the ADC clock at 24 MHz. At 48 MHz with the low-power buffer on, about a third of the codes went missing.
- At rest every channel is quiet to about one count rms.
How I Checked
The trick for checking an ADC without a fancy calibrator is to feed it something slow and straight, and read the same thing with a meter. My board has a resistor divider from the supply rail into an ADC pin, so it reads its own rail at about 248 counts per volt. The bench supply walked that rail from 3.7 V to 8.0 V in 20 mV steps, 216 steps in total, with the meter on the rail. At each step the servo streamed 5000 samples at rest, so every point is a mean over a quarter of a second.
The divider and the meter are both far straighter than one count over that range. So when I fit one line through code against volts, whatever is left over that follows the code belongs to the converter.
The meter’s accuracy is from its manual, 0.1% of the reading plus 2 mV on the 20 V range, so absolute numbers below are good to about 0.1% at best. The shape of the error doesn’t care, since a scale error in the meter only tilts the line.
The Sawtooth and the Wide Code at 1023
One line fits all 216 points at 247.54 counts per volt, with 0.89 counts rms left over. That’s already pretty good! But the leftover wasn’t noise. When I plotted it against the code instead of the voltage, it turned into a sawtooth. The plots are in the terminal-chain notebook if you want to see it.
Inside each block of 256 codes the error falls by about 2 counts from start to end, then jumps back up at the next multiple of 256, at 1280, 1536 and 1792 alike. One sawtooth fitted through every block gives -2.04 ± 0.16 counts per block, and taking it out drops the leftover from 0.89 to 0.67 counts rms.
The jumps line up with the converter’s carries. A SAR (successive approximation register) ADC finds each code one bit at a time against an internal DAC (digital-to-analog converter). A lot of SAR converters build that DAC in two halves, the low 8 bits as one group and the bits above them as another, joined through a small coupling capacitor. If the low group weighs a little off against the high one, the same error builds up across every block of 256 codes and snaps back at each carry. That’s my best guess for the sawtooth.
At every carry the code just above it is also nearly or completely missing. 1280 and 1281 never showed up, 1536 showed up five times and 1792 twice, where a typical code got about a thousand reads.
The carry at 1024 is the big one. Out of 1.08 million samples, codes 1024 to 1032 never appeared at all, and 1023 appeared about ten times as often as any other code. One point sat at 1023.0 for all 5000 samples where the line puts the input at 1026, and the next one read 1023.3 where the line puts it at 1031. So the converter holds 1023 over about ten codes of input and then skips straight to 1033. Against the voltage it just looks like a glitch near 4.15 V, but against the code it’s pretty obvious.
A sweep of board D’s pot channel found the same thing further up the scale. 2047 held about eleven codes of input, a bit more than 1023, and 511 and 2559 were wide too. I’ve also seen wide codes on two other chips, both on the pot channel. On rev 2A board A the gaps were 9 codes at 1024, 4 at 2048 and 7 at 3072. On a board from an earlier revision, 1023 was about 5 codes wide and 3072 about 8, and 2048 wasn’t wide at all. So the wide codes sit at the big carries, but which ones and how wide changes from chip to chip. I only mapped the full sawtooth on board D, so I’m assuming its size changes from chip to chip too.
Where it bit me
Before I found the sawtooth, the current-scale notebook had a step of 4.7 counts between two of its laps that I couldn’t explain. The two laps sat at raw codes 1526 and 1564, right across the 1536 carry. Subtracting the map took the step from 4.7 down to 3.6, and a second pass of the same laps went from 4.4 to 3.4. So the converter owns about a count of it, and the rest is still open.
It also matters for a 1S lithium cell. On a swap board the rail is the battery, and my rail tap reads 1023 at about 4.1 V, near the top of a 1S cell. A servo on a nearly full pack would read its own supply through the widest code on the chip.
What I do about it
The notebook writes the map out as a CSV of code and error, and later notebooks subtract it when a count matters. I did think about a lookup table in firmware, and decided against it:
- The gain is about a count, the same size as the noise floor and far from any threshold the firmware acts on.
- The map is different for every chip, so every servo would need its own factory calibration on a fixture. That’s exactly what this project is trying to avoid.
- A wide code throws information away. When 1023 covers ten codes of input, no table can tell you where inside those ten you are.
- The correction would sit in the 20 kHz control loop, and on this chip every instruction in there costs something.
If the 1S rail reading ever matters, the fix is a divider ratio that keeps a full cell away from 1023.
Don’t Run the Hidden Calibration
The V003, the V006’s older sibling, has a documented ADC calibration. You set RSTCAL and then CAL in ADC_CTLR2, the hardware converts an internal calibration voltage (2/4 or 3/4 of VDD, picked with CALVOL), and it stores a calibration code that corrects the conversions after it. In the V00x reference manual those two bits are marked reserved and CALVOL is gone. But the V006 datasheet’s ADC table still lists a calibration time, and the manual’s block diagram still shows a VCAL input on IN10.
So I tried it. Setting bit 2 on the V006 still works. The bit clears itself when it’s done, and after that every channel came out 11 to 13 counts lower. Then I checked both against the meter, with the pot at two positions and a jumper grounding one input.
| reading | the meter says | raw | after CAL |
|---|---|---|---|
| pot, position 1 | 2165.7 | 2169.2 (+3.5) | 2157.0 (-8.7) |
| pot, position 2 | 2186.6 | 2189.6 (+3.0) | 2176.8 (-9.8) |
| grounded input | 0 | 1.3 | 0 (clipped) |
| VDD from IN8 | 3.300 V | 3.297 V | 3.324 V |
The raw converter is the accurate one. A grounded input reads about one count raw, so there was never a 12-count offset to take out. After the calibration the pot reads about 9 counts low, and VDD (more on that in the next section) reads 0.7% high instead of 0.1% low.
The calibration voltage looks like the reason. On my chip IN10 reads 2061 counts raw, which is 0.503 x VDD, and it stays there at every VDD and every sample time I tried. That’s 0.3% above half. After the calibration it reads 2048. So it seems the hardware trims that tap to exactly half scale and pushes the difference into every other channel.
Ok, so the raw converter it is. My firmware never set that bit anyway, I only found out it worked while poking at IN10.
Give the 1.2 V Reference a Long Sample Time
On the V006 the ADC’s reference is VDD itself, there’s no separate reference pin on my package. So every reading is a fraction of VDD, and if VDD moves, every channel moves with it. The one channel that doesn’t is IN8, the internal 1.2 V reference. Since that voltage is fixed and VDD is the ruler, you can turn it around and get VDD:
VDD = 1.2 V x 4096 / vref_raw
At 3.3 V that’s about 1490 counts. IN8 has no enable bit, it’s just there. IN10 can’t do this job, since it reads 0.503 x VDD at any VDD and never sees VDD move.
The catch is the sample time. The datasheet says slow sampling is recommended for the reference and leaves it at that. I first scanned it at 13.5 cycles like my other slow channels, right after the pot, and it read 62 counts high, about 4%. Here is how far it read from its settled value at each sample time, with a 24 MHz ADC clock:
| sample time (cycles) | converted alone | right after the pot |
|---|---|---|
| 3.5 | +86 | +155 |
| 13.5 | +11 | +62 |
| 28.5 | +0.8 | +3.6 |
| 41.5 | +0.1 | -0.4 |
The channel before it matters a lot. My read is that the sample-and-hold remembers some of the previous channel’s voltage, and the reference takes a long time to wash that out. At 41.5 cycles, which is 1.73 µs at 24 MHz, it lands within half a count either way, so that’s what my scan uses now.
With that, VDD from the reference matched a meter on the 3.3 V pin within 0.1% on board D, 3.297 V against 3.300 V. That’s a lot better than the datasheet promises for the reference, which is 1.18 to 1.22 V, about ±1.7%. It’s one chip at one rail though, so I wouldn’t count on it for every chip.
Why bother reading VDD
My board’s 3.3 V comes from a regulator fed by the rail. From a 3.7 V rail up, VDD holds at 3.29 to 3.30 V. But on a 3.3 V rail the regulator runs out of headroom. VDD drops to 3.12 V at rest and to about 3.0 V while driving the motor, and every ratiometric reading goes with it. At that level the motor terminal reading came out 12.6% over the meter. So the reference is the right fix for a rail in dropout, and above dropout it’s mostly a sanity check.
Run the ADC Clock at 24 MHz
I covered this in the duty floor post, so here is just the short version. The V006’s ADC clock is rated from 16 to 48 MHz, and I first ran it at 48. On an earlier board revision I swept the servo through its travel and counted which pot codes ever showed up, scoring only the codes the wiper actually crossed:
| ADC clock | low-power buffer | sample time | pot codes seen |
|---|---|---|---|
| 48 MHz | on | 73 ns | 62.7% |
| 48 MHz | on | 865 ns | 63.2% |
| 48 MHz | off | 73 ns | 97.2% |
| 24 MHz | on | 146 ns | 95.7% |
| 24 MHz | on | 562 ns | 96.3% |
A sample time twelve times longer changed nothing, so it wasn’t the input settling. Turning the low-power buffer off brought the codes back, so the comparator’s buffer just can’t settle a bit trial in one 48 MHz clock. Full power costs noise though, the current channel’s rest noise went from about 1.3 counts to 3.2. At 24 MHz in low-power mode the codes come back and the noise stays low. It’s also the only row inside the datasheet’s limits, since a sample rate over 1 MHz needs VDD of 4.5 V or more, and a 26-cycle conversion at 24 MHz is 0.92 Msps.
The Noise Floor Is About One Count
With the motor driver off, the servo streamed every channel at the 20 kHz control rate for three seconds, three times at each of three supply levels. Every channel sits at about one count rms. The current channel is 1.08 counts, the motor terminal taps 0.61 and 0.65, the rail 1.26, the thermistor 1.15 and the 1.2 V reference 1.28.
There is one bit of structure in it. My telemetry goes out in frames of 16 samples, which is 1250 Hz at 20 kHz, and the average reading at each position in the frame wobbles by about a count. The current channel’s spectrum has lines at 1250 Hz and its harmonics. Both plots are in the rest-noise notebook.
The weird part is that the 1.2 V reference follows the same shape as the board’s taps, about 2 counts peak to peak, and the reference has no pin on the board at all. So my guess is that something inside the chip moves while a frame goes out, the ADC’s reference or its ground, but I haven’t tested that yet. Since it’s a count or so I left it alone. If you need a channel quiet to a tenth of a count, average over whole frames and the pattern cancels out.
Caveats
- Almost everything here is one chip on one board, rev 2A board D. The wide codes showed up on three chips, but the sawtooth was only mapped on one.
- The sawtooth map covers codes 918 to 1980 on the rail channel only. The pot sweep shows 2047 is wide on this chip, but I haven’t walked the rail tap across 2048 or outside that range yet, and whether the current channel carries the same sawtooth is an assumption for now.
- The calibration test was one board, two pot positions, and a meter read off its display.
- VDD from the reference matched the meter at a single rail. I never metered VDD over a whole sweep of rails.
- The 48 MHz table came from an earlier board revision, before rev 2A.
What’s Next
Next on the list is walking the rail tap across 2048 and below 918, and walking the current channel over the same codes to see whether it has the same sawtooth.
All the data, the fits and the map are in the terminal-chain and rest-noise notebooks. If you’ve measured the ADC on your own V00x chips, I’d really like to know whether you see the same sawtooth, and where your wide codes land. And if anyone knows what that reserved CAL bit is really doing on these parts, let me know!