Note 02 of 02: 笔记 02 / 02:A Switched Battery Divider and ADC Settling电池采样开关与 ADC 建立时间
Calculating the settling, current consumption and error sources of the Claude Chime battery-voltage divider.从 Claude Chime 的受控分压电路出发,计算建立时间、采样耗电与电压换算误差。
In the software handoff I wrote for Claude Chime, the battery-sampling sequence is to drive ADC_ENABLE high, wait 5 to 10 ms, take several readings and then switch the divider off. The conversion uses a single ratio, so why insist on waiting? The answer sits with the capacitor beside the ADC pin.
给 Claude Chime 写软件交接文档时,电池采样那段我写的顺序是:拉高 ADC_ENABLE,等 5 到 10 ms,多读几次,再把采样支路关掉。分压公式就一个系数,为什么还非得等?答案在 ADC 引脚旁边那颗电容身上。
Divider and Switch分压与开关
In the schematic, the battery feeds the divider through an AO3401 high-side switch, with 15 kΩ and 22 kΩ doing the dividing. Once ADC_ENABLE goes high, a 2N7002 turns on and pulls the AO3401 gate low, and only then is the branch connected. The divider midpoint reaches ADC_BAT through a further 1 kΩ resistor, and a 100 nF capacitor hangs from the ADC side to ground.
原理图里,电池经过 AO3401 高侧开关给分压支路供电,15 kΩ 和 22 kΩ 负责分压。ADC_ENABLE 一拉高,2N7002 导通,把 AO3401 的栅极拉低,这条支路才接上。分压节点再经过一颗 1 kΩ 电阻接到 ADC_BAT,ADC 这一侧还挂着一颗 100 nF 的对地电容。
Setting aside the switch's on-resistance and the ADC input current, the settled output is VADC = VBAT × 22 / 37, so the reading is scaled back up by 37 / 22 ≈ 1.682. A 4.2 V battery puts the ADC node's target at about 2.497 V. At the instant the branch connects, though, the 100 nF capacitor has to charge first, and the voltage takes a while to climb to that target.
先不管开关的导通电阻和 ADC 的输入电流,稳定以后分压输出是 VADC = VBAT × 22 / 37,反算时乘上 37 / 22 ≈ 1.682。电池 4.2 V 时,ADC 节点的目标电压约 2.497 V。可支路刚接通那一下,100 nF 电容得先充电,电压要过一阵才能爬到这个目标。
RC Settling TimeRC 电路的建立时间
To find the resistance the capacitor sees, set the ideal voltage source to zero in the circuit model. The 15 kΩ and 22 kΩ resistors then sit in parallel, in series with the 1 kΩ, for a Thevenin resistance of about 9.919 kΩ. With 100 nF, the time constant is about 0.992 ms. Starting from 0 V, the first-order fraction of error remaining is exp(-t/τ).
算电容看到的等效电阻,先在电路模型里把理想电压源置零。15 kΩ 和 22 kΩ 于是并联,再串上 1 kΩ,戴维南等效电阻约 9.919 kΩ。配上 100 nF,时间常数约 0.992 ms。电容从 0 V 开始充,一阶模型里剩下的误差比例就是 exp(-t/τ)。
| Wait | Remaining error | Equivalent error magnitude at a 4.2 V battery |
|---|---|---|
| 1 ms | About 36.5% | About 1.53 V |
| 5 ms | About 0.647% | About 27.2 mV |
| 10 ms | About 0.00419% | About 0.176 mV |
| 等待时间 | 剩余误差比例 | 换算到 4.2 V 电池端的误差幅值 |
|---|---|---|
| 1 ms | 约 36.5% | 约 1.53 V |
| 5 ms | 约 0.647% | 约 27.2 mV |
| 10 ms | 约 0.00419% | 约 0.176 mV |
These are ideal RC figures for settling from zero. Five and ten milliseconds are only a few milliseconds apart, yet they leave very different amounts of error. Taking 32 readings straight after switching on and averaging them may simply average a voltage that is still on its way up.
这是理想 RC 计算,只算从零开始建立的误差。5 ms 和 10 ms 只差几毫秒,建立程度却差得很远。接通以后马上连读 32 次再取平均,平均的很可能是一段还在往上爬的电压。
Setting the RC settling error alone at 0.1% takes about 6.91τ, or 6.85 ms. The real wait also has to account for component tolerances, the MOSFET turning on, the load from ADC sampling and the accuracy actually needed. The 5 to 10 ms in the handoff can be checked against the node waveform on an oscilloscope and real ADC readings before a final duration is chosen.
单独把 RC 建立误差定在 0.1%,大约要 6.91τ,也就是 6.85 ms。实际等多久,还得算上阻容容差、MOS 的开通过程、ADC 采样带来的负载和测量精度要求。交接里写的 5 到 10 ms,可以再拿示波器看节点波形、对着实际的 ADC 读数核一核,再定下具体时长。
Sampling-Circuit Current Consumption采样电路的电流消耗
At 4.2 V, the 37 kΩ divider left permanently connected draws about 113.5 μA, roughly 2.72 mAh a day. With the switch, it works only inside the measurement window; switched on for 10 ms each second, for example, the divider resistors alone average about 1.14 μA.
4.2 V 下,37 kΩ 的分压支路要是一直接着,电流约 113.5 μA,一天下来约 2.72 mAh。加了开关以后,它只在测量窗口里工作,比如每秒接通 10 ms,光算分压电阻,平均电流就降到约 1.14 μA。
That is not the whole bill, though. The AO3401 gate has a 10 kΩ pull-up, and when the 2N7002 conducts, that pull-up plus the series 1 kΩ forms another current path. Under an ideal on-state approximation it draws about 382 μA at 4.2 V, more than the divider itself. Each recharge of the capacitor, GPIO drive and off-state leakage add their share too. The power of the whole sensing circuit has to count all of these branches together; the divider current alone tells only part of it.
不过账还没算完。AO3401 的栅极挂着 10 kΩ 上拉,2N7002 一导通,这颗上拉加上串联的 1 kΩ 又是一条电流路径。按理想导通近似,4.2 V 下约 382 μA,比分压支路本身还大。电容每次充电、GPIO 驱动、关断时的漏电也各有一份。整套采样电路的功耗,得把这些支路一起算进去,光拿分压电阻的电流说事是不够的。
The time spent on repeated conversions belongs inside the enabled window as well. Wait 10 ms and then spend more time on several conversions, and the real duty is above 1%. With that worked out, choosing the sampling rate and knowing how much charge each extra reading costs becomes straightforward.
多次转换花的时间也算在接通窗口里。等 10 ms 以后再花时间做好几次转换,实际占空比就不止 1%。这样算清楚,采样频率定多高、为了多读几次付出多少电量,心里就有数了。
ADC ConversionADC 换算
The ESP32-S3's ADC also involves attenuation settings and calibration. Espressif's calibration documentation provides raw-code-to-voltage calibration and discusses input capacitance and multisampling for noise reduction. Software should first get an input voltage that matches the current ADC configuration and only then apply the divider ratio, instead of forcing every configuration through one fixed full-scale formula.
ESP32-S3 的 ADC 还牵涉衰减设置和校准。Espressif 的校准文档提供了原始码到电压的校准接口,也讲了输入电容和多次采样对降噪的作用。软件应该先按当前 ADC 配置拿到输入电压,再乘分压系数,不能拿一个固定的满量程公式套所有配置。
Errors need separating too. Averaging can knock down some random noise, but the steady low bias from incomplete RC settling does not go away on its own, and divider-ratio and ADC-conversion errors can stack on top of each other. Comparing a few stable voltages against a multimeter, one each near the low end, the middle and full charge, shows discrepancies far more readily than calibrating at a single point.
误差也得分开看。平均能压掉一部分随机噪声,RC 没建立完带来的持续偏低却不会自己消失;分压比的偏差和 ADC 换算的偏差还可能叠在一起。拿几个稳定的电压点对着万用表,低端、中间、接近满电各查一处,比只校一个点更容易看出问题。
When I tested this board, I had no ESP32 core board at hand. What I could do was drive ADC_ENABLE high by hand and measure the divider node, and at that stage the voltage matched the calculation; the digital reading was left to the client's firmware, and it was their side that reported the battery reading worked. Working the branch through from start to finish makes it clear why the handoff had to carry both a control sequence and a conversion ratio. Switching on, settling, sampling and calibrated conversion all have to cooperate before the battery reading is fit to decide whether the crab gets to jump again.
测这块板时,我手边没有 ESP32 核心板,能做的就是手动拉高 ADC_ENABLE,量分压节点,到这一步电压和计算是对得上的;后面的数字读数交给客户固件,电量读取正常也是对方反馈回来的。把这条支路从头到尾算一遍,交接时为什么要把控制顺序和换算关系一起写下来,就很清楚了。开关接通、电压建立、ADC 采样、校准换算,几步配合好,读出来的电量才值得拿来决定螃蟹下一下还跳不跳。