# How do I measure current with a microcontroller

OK, having given myself a crash course in op amps, I think I've got it figured out.

I am looking to build an in circuit battery monitor, and I wish to measure the current being drawn from each individual cell in a series-parallel battery pack.

I think the circuit below will do all I want it to do, and I now think I've got the resistor values right to give me the correct gain. And I think I've got the right bias voltage to force the output positive over the desired range of -2.5 to +7.5 Amps.

I have been informed that I was nearly correct. 4 diodes in series gives me a handy two point FOUR volt reference, and as much current as I care to waste in the power shunt resistor, to supply the microcontroller, as well as provide an offset bias for the voltage sense module. A hefty cap holds enough charge to drive the microcontroller during its wake time as well as the IR-llLED derived opto-Xceiver, to allow me to maximise the value of the power shunt resistor. Hopefully 1mA continuous should be enough.

What I need is for AN1-7 to read across the range of -2.5 to +7.5 (Amperes through shunts/fuses @ 10 mA/div) and An0 to read actual voltage across the range of 2.8 V to 4.4 V. @ <2 mV/div.

Unless I am completely mistaken this (slightly modified, and broken into its functional blocks) circuit should let me monitor the precise health and charge status of every individual cell in a battery pack, identifying those not delivering their fair share of the load, and/or which self-discharge, dragging down cells connected in parallel.

By keeping a record of each cell's performance over multiple charge and discharge cycles, the right software would be able to maintain a charge map of the battery and recommend cell swaps to keep the battery perfectly in balance, whenever cells drift out of spec, or when a dud cell is replaced.

Big question, do I have it right, or have I made some fundamentally stupid error?

simulate this circuit – Schematic created using CircuitLab

• This monitoring of the individual currents of (groups of) cells seems overly complex to me. Why would you not just connect all cells or paralleled cells in series and only measure the voltages across those cells. That is much easier and will also allow you to monitor the cell's health. As long as the cells are identical and balanced and they are loaded by the same current, that should do the trick. It's what is used in many devices. If you need to tap-off certain voltages from within the pack I would not do so but use a DCDC converter to make that voltage so that the power comes from all cells – Bimpelrekkie May 21 '16 at 9:58
• The idea is to be able to see a single cell on its way out, – Black Betty May 21 '16 at 11:01
• use Analog multiplexers for reducing number of pins needed – ElectronS May 21 '16 at 11:10
• @FakeMoustache The "tapping" is simply the nature of the beast. It allows absolute measurement to be taken from each cell rather than relative to battery ground. Better granularity, and one set of component values fits all. – Black Betty May 21 '16 at 11:30
• @ElectronS but significantly adds to the complexity for just one more pin. If necessary, voltage sense can be farmed off to a separate board with only one analogue multiplexer at the expense of slightly more complex code in the master controller. – Black Betty May 21 '16 at 11:56

resistor tolerances make this method of measuring currents impractical (R1-4). You need a different method of level shifting and amplifying the current sense signal. Start with something like this - https://www.maximintegrated.com/en/app-notes/index.mvp/id/4438

Diodes are horrible voltage references -- the voltage on each diode changes by about -2 mV/C. Best use a real voltage reference.

Your circuits will discharge the Li-Ion cells unequally (the resistors in divider feeding the opamp) -- you'll damage the cells if left alone to discharge.

• 1% not good enough? Remember at the end of it all, I've got software to do any final adjustments. I've been told diodes work fine for a reasonably constant current. My biggest concern is that the current I'm drawing won't be sufficient to fully bias the diodes to begin with. – Black Betty May 24 '16 at 9:50
• Where's the imbalance? All circuits are identical to within component tollerances? And the current draw on those dividers is only 10s of microamps anyway, even all in parallel, they're still only drawing a fraction of a milliamp in total from a 20+ A/Hr pack. The single biggest current power sucker is the diode stack, given that the opto only runs for milliseconds. Like I said my biggest concern, is that I'm going to have to waste milliamps keeping the diodes biased. The application is for an e-bike, so discharge over time (several months to over a year) should not be an issue. – Black Betty May 24 '16 at 10:33
• Real voltage references also use much less power than the diode stack. It looks like you're using fuses as shunts; how accurate is their resistance? – pjc50 May 24 '16 at 13:29
• Aware of that, but due to the nature of this beast, I can't use a common supply. I should have mentioned, component cost is a very large concern. If I can manage good enough with 50 cents worth of decent precision components, rather than $2, and the same saving at$1 to $4 per sense circuit, calibrate and compensate for errors in the software of the ludicrously overpowered for the task 2$ CPU why the hell not? $15 beats the hell out of$40. I'll be needing at least 10 of these boards, so my prices will be even better. Perhaps as little as \$10 per board. A mosfet and a bit of jiggery (cont) – Black Betty May 24 '16 at 16:27
• pokery with the IR-LED to turn it on does away with the self discharge concern entirely. Low side takes one big mother, 60A plus. Seven x 10-15A on the high side, and we can really go town, cutting individual cells in and out of circuit to keep their charges perfectly balanced at all times. – Black Betty May 24 '16 at 16:41