Yesterday’s high-side DC power control circuit was not able to measure current in a very exact way. Each ADC step is about 3.3mv, and with a 0.1 Ω sense resistor, that translates to 33 mA steps (a bit less, now that the MOSFET turns out to have a slighty higher “on” resistance).
So let’s add an op-amp, to amplify the voltage:
That’s a standard way to amplify an input voltage by a factor of 11, so a 0.3V input should generate a 3.3V output voltage, which is nicely full-scale for the ATmega’s analog input when running at 3.3V.
But there’s a little mistake in this setup…
The voltage we’re measuring is not a small voltage above 0V, but a small voltage below 3.3V, so we’re in fact feeding the op-amp a voltage between about 3.0V and 3.3V. Amplified by 11 you get… something the op-amp can’t handle when powered from 3.3V, so it’ll simply return 3.3V all the time. Overflow!
Simple to fix though. Instead of tying that lower resistor to ground, we tie it to 3.3V as reference level. And lo and behold, I’m seeing a roughly 11x larger reading with the same setup as yesterday:
Now, the input voltage swings between about 2.4V and 3.3V, which is just fine as analog input.
The one thing to watch out for is that we’re sailing very close to the edge, or in op-amp speak: “close to the rail”. This circuit is working with an input voltage which is very close to the +3.3V power supply “rail”, and the output of the op-amp also needs to be able to swing up to that same +3.3V level. This requires some care in selecting a “RRIO” type op-amp (i.e. Rail-to-Rail Input and Output) – the chip I used here is the TLV2373, a dual op-aamp. It does fairly well, but the output can’t quite totally reach 0V or 3.3V. I suspect that most op-amps will have this problem: a tiny residual voltage on both sides of the output swing. Such is life, no op-amp is perfect.
Here is the test sketch I used for this experiment:
It’s set up for 4 channels, although this circuit only has one. This could be used as the basis for a 2-channel plug, since both the MOSFET and the op-amp are dual-channel.
The output measurements come in via wireless every 5 seconds, and a simple “1,21s” sent out to it (since this is node 21) will turn on the disk.
But this code doesn’t even come close to what I’d really like to implement: it needs to track power use, only switch off when a device is consistently below a preset level (could be different for each device), implement encryption to prevent unauthorized control, store settings in EEPROM, support configurable behavior after power loss, and power devices up in a staggered mode to reduce the load on the power supply, oh my – not there yet!
I’ve been gradually catching up with this series of posts. If you’re switching any amount of current, you’ve probably discovered that p-channel MOSFETs are several times more expensive than their n-channel counterparts, and underperform them significantly.
It turns out often to be cheaper to switch an n-channel MOSFET (driving the gate relative to the high side of the load). And there are chips that do the job nicely. (The MAX620 has four in one convenient package.) The MAX1614 has low-battery sense built in.
I haven’t been tracking all your requirements. Can you put the current sense in the ground return? Even if you’re doing high-side switching, ground-return current sense is much simpler (and doesn’t have the problems with common-mode rejection that high-side current sensing gets you into). If you do have to do high-side current sensing, Maxim has a bunch of app notes with ideas:
http://www.maxim-ic.com/app-notes/index.mvp/id/746
And (sorry to follow up to my own post) — if you’re switching a 3.3V or 5V supply, USB power switches like the LM3526 are now dirt cheap. They do a lot of the management tasks, as well.
And – I realize belatedly that I’m coming across as a Maxim apologist here. But all the linear manufacturers have high-side current monitors. The TI INA19x and INA21x series are also popular, SOT23 or SC70 (DCK) form factor, fixed gain devices. These are jellybean parts nowadays, go with whatever your supplier can supply.
Thanks for the good tips. I did this circuit mostly as an exercise in thinking out of the box, really. Just wanted to see how far you could go when messing with voltage potentials. I can’t do anything on the low side, because there will be multiple circuits, which I want to monitor and control independently.
High-side current sensor is indeed the way to go, but I’m not quite ready to move away from P-MOSFETs. The one I’ve been playing with is the ZXMP3A16DN8. But I might add an NPN in front to get the JeeNode itself back to ground level.
Some AVRs have a a switrchable builtin x20 gain amplifier for the A/D converter in differential mode, which would have saved you from adding an extra OP-AMP, but unfortunately the ATmega328 doesn’t belong to that group.