Voltage divider/ADC for measuring battery voltage - Page 1
Forum section: electronics / projects

author: pugglewuggle
author_rank: Contributor
timestamp: April 17, 2014, 04:43:03 pm
content: Hi,I've looked all over the internet over the last week and have seen all sorts of example circuits with different reasoning behind them for accomplishing this task. Here is my situation:I want an onboard battery/input voltage monitoring capability for my device... right now I'm looking at using the onboard 12-bit ADC of the Atmel SAM4S ARM MCU. The power supply voltage can range anywhere from 9V to around 36V so I need to be able to measure this range. I'll be using an AVref of 2.5V. This doesn't need to be super accurate, really just to the hundredth of a volt, which 12-bit provides for just fine at this range plus a little LSB error which is okay.There are a couple of factors I'm considering that I need to know if I should take into account or just ignore in this design:Loading effect on the divider output voltage from ADC impedance/resistanceHow am I to calculate the signal output impedance from the divider (Theverin resistance?) that's required to work with the ADC's internal impedance (rather relates to point 1)? not sure where to start on this one due to what isn't obvious to me in the SAM4S datasheetAny help and and other thoughts is greatly appreciated.

author: qno
author_rank: Frequent Contributor
timestamp: April 17, 2014, 06:32:20 pm
content: One option is to put a capacitor of about 10 nF in the mid point of the voltage divider.Most microcontrollers A/D have a nasty sample pulse.Do not expect more than 0.1V accuracy

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 17, 2014, 07:14:34 pm
content: You could also use a relatively strong voltage divider with a Pch on the high side to turn it on or off. Turn it on to make your measurement, turn it off when done. The divider would have a low enough output impedance to not significantly affect the A/D measurement, and then when you turn it off the current consumption drops to zero. It does take an extra microcontroller pin though.

author: dannyf
author_rank: Super Contributor
timestamp: April 17, 2014, 07:20:42 pm
content: QuoteOnly if your adc's input impedance is comparable to the divider's output resistance - typically not the case, but check your device datasheet for sure.In case that is the case, use a capacitor to minimize charge transfer.QuoteThat would be the parallel of the two resistors. I usually use anything less than 1/10th of the adc's input impedance - anything less than 100k should work most of the times.QuoteYou will have a hard time to shut off that P-ch, without the use of another level shifter.I usually just turn it on / off on the low-side (directly via a pin, or via another switch).

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 17, 2014, 07:49:23 pm
content: Quote from: dannyf on April 17, 2014, 07:20:42 pmWith his voltages, yes. Could always use an N/P pair though. uC switches the N, N switches the P. I would hesitate to use low-side switching in this application due to the voltages at play. As soon as the resistor divider is cut off from ground, it's going to pull up to battery voltage, and start leaking through the uC's clamping diode.

author: pugglewuggle
author_rank: Contributor
timestamp: April 17, 2014, 08:41:17 pm
content: @Suicidaleggroll I have considered this and the only question I had is why everyone uses a P-channel instead of an N-channel before ground? Also, I'm a little concerned about leakage but I understand that to be usually just a couple of nA so it's really not a big deal, even in battery-powered systems (I think?)@dannyf I don't know the input impedance nor can I find it in the datasheet. Can you look for me and see if I'm just missing something? The ADC electrical characteristics start on page 1131. Here's the link:http://www.atmel.com/images/atmel_11100_32-bit-cortex-m4-microcontroller_sam4s_datasheet.pdfRegarding an N-channel on the ground side of the divider, can I just run the low side of the divider into a GPIO pin and drive it to ground/set it to output high when not using?

author: dannyf
author_rank: Super Contributor
timestamp: April 17, 2014, 08:44:40 pm
content: QuoteSo you are using theh clamping diodes as they are supposed to be used.Why worry about it?

author: pugglewuggle
author_rank: Contributor
timestamp: April 17, 2014, 08:48:05 pm
content: Just wanted to clarify - I missed the Zin... it's 1Mohm at 500ksps with the 2pf Cin enabled from the gain mode in use.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 17, 2014, 09:34:11 pm
content: Quote from: pugglewuggle on April 17, 2014, 08:41:17 pmFor the reason dannyf and I are currently discussing.Quote from: pugglewuggle on April 17, 2014, 08:41:17 pmThat wouldn't accomplish anything. Remember, your battery is at up to 36v, and your resistors will be bleeding current through whatever the bottom is connected to. If you set your GPIO low, you'll get (36-0)/(R1+R2) current. If you set your GPIO high, you'll get (36-3.3)/(R1+R2) current (plus a little bit through the clamping diode of the ADC pin). Not going to change much. If you set your GPIO to a high impedance input, you'll get (36-3.3-Vf)/R1 as the resistor continues to dump current through the microcontroller's clamping diode.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 17, 2014, 09:36:54 pm
content: Quote from: dannyf on April 17, 2014, 08:44:40 pmTwo reasons1) They're not "supposed" to be used at all. Dumping current through the clamping diode can have unpredictable results since there's no clear ground path. It can flip bits, lock up the CPU, and do other unpredictable things. The current limit in the datasheet is just a damage limit, you can still get unpredictable and unexpected results with MUCH less current than the limit in the datasheet.2) What's the point in shutting the fet off? You haven't shut off the current draw, you've just redirected it. Instead of dumping current from 36V through R1, R2, Nch, to ground, you're dumping current from 36V through R1, uC clamping diode, to ground. Still wasting nearly as much current as if you just left the Nch on.In this application, a Pch will shut off the current, while an Nch will just redirect it through the mcu's clamping diode, still wasting nearly as much current and possibly causing unpredictable behavior in the mcu.

author: dannyf
author_rank: Super Contributor
timestamp: April 17, 2014, 09:54:39 pm
content: QuoteSo those guys made something into a chip so that they are not "supposed" to be used at all? Then why design them?Quotelike what?QuoteUnless your "clear" is different from my "clear", would the power supply work for you? or the adc? or those thousands of little fets inside of that mcu?QuoteSo can a power supply; or your code; or lightening; or cosmic ray;...Quoteditto.Quotelike?QuoteNo one suggested that you run that much current through the clamping diodes.QuoteI still don't know what those "unpredictable and unexpected results" are.QuoteComplexity for one.QuoteNo argument there.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 17, 2014, 10:26:44 pm
content: Quote from: dannyf on April 17, 2014, 09:54:39 pmTo protect against damage in the case of unforeseen or accidental reverse or over voltage. Not so the designer can be lazy and hope for the best.QuoteLike latching the chip, or reversing current from the power supply, raising Vcc and damaging the mcu itself or other circuitry. They're great for protecting the chip from damage caused by ESD, brain farts, etc., but they shouldn't be "used" as part of a design. It's like relying on the automatic distance checking/breaking system on a car to stop you at every red light.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 17, 2014, 10:49:06 pm
content: Quote from: pugglewuggle on April 17, 2014, 08:41:17 pmLeakage current is different than dumping current through a pin's clamping diode.Leakage current is when you apply a voltage within the 0-Vcc spec of the mcu, and the mcu leaks a little bit of it through. It willl be very small, as you said typically in the pA/nA range, and is more or less insignificant for what you're looking at here.The clamping diode is another animal altogether. When you raise the voltage applied to a pin above Vcc (or below Gnd), there's a diode (or a diode-like combination of substrates) that keeps the voltage from going more than 1 diode drop beyond Vcc or Gnd. When it does, the diode begins to conduct, and starts dumping current into the nearest rail.The reason you don't often see low-side switching for resistor divider voltage monitoring like this is because the low-side switch serves no real purpose. If the switch is on, the resistors conduct current from the battery to ground. If the switch is off, the voltage applied to your ADC pin jumps up to your battery voltage, and the resistors (at least the high side resistor) conducts current from the battery through the mcu's clamping diode to Vcc. The net effect is basically the same. You might drop the current draw by 10-20% (depending of course on battery voltage and mcu Vcc), but you're still going to be pulling nearly as much current from the battery as you were with the switch on (AND you might even damage the mcu, see above). In which case, you might as well just drop the switch and ground the resistors directly. Of course if you do that, you're back to square one...unnecessarily dumping current from the battery through a resistor divider and wasting power.A high-side switch lets you cut off the current draw of the resistor divider completely, without dumping anything through the mcu's clamping diode (since when you cut the resistors off from battery voltage, the voltage applied to your ADC pin just drops to Gnd).

author: dannyf
author_rank: Super Contributor
timestamp: April 18, 2014, 12:03:01 am
content: QuoteMany things you do with a mcu will expose it to the risks listed above, and more.Having a reality check would be helpful.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 18, 2014, 12:25:35 am
content: Quote from: dannyf on April 18, 2014, 12:03:01 amYeah, most of the time those things are beyond the designer's control. But purposely designing the circuit to do something with the mcu that it's not supposed to do, could damage the mcu itself or the rest of the circuit, and on top of that, serves absolutely no logical purpose, is just asinine.I suggested he could use a stronger resistor divider to take care of the impedance problem when feeding an ADC and use a Pch to shut it off so it doesn't drain the battery when it's not in use. You suggested he switch the ground side of the divider instead, which could damage the mcu, the rest of the circuitry on the board, and wouldn't even shut off the resistor divider anyway.Where exactly are you going with this argument?

author: nctnico
author_rank: Super Contributor
timestamp: April 18, 2014, 12:27:22 am
content: How about using a high resistance divider and a large cap? The input leakage of the ADC is mostly dynamic due to capacitive switching. When sampled slowly enough you'll only get an error of the capacitive divider.

author: David_AVD
author_rank: Super Contributor
timestamp: April 18, 2014, 12:42:26 am
content: Perhaps use an R-R opamp to buffer the high impedance voltage divider? Microchip has some opamps (MCP6421 for example) that have pretty low Iq figures. All depends on how much current draw is acceptable.

author: pugglewuggle
author_rank: Contributor
timestamp: April 18, 2014, 01:50:45 pm
content: I've got to agree with the eggroll on the clamping diodes... my understanding is that while they're there they are to be used as a safety measure in the case of external circuit failures or brain farts to prevent total system failure/burning up the chip by feeding it more V than it can handle.@David_AVD and @nctnico... I'd like to keep this simple with passives like resistors/caps if possible. Also, I'd obviously like to minimize waste as much as possible. The high-side P-channel FET sounds like the best plan to me as far as turning the battery monitor on and off for sampling. Can you elaborate more @nctnico?

author: suicidaleggroll
author_rank: Super Contributor
timestamp: April 18, 2014, 03:15:54 pm
content: Quote from: pugglewuggle on April 18, 2014, 01:50:45 pmThe three primary methods, in my experience, are:1) high resistance with a cap to hold the value through the A/D sampling. This is simple and cheap, but you need to balance resistor size with cap size. Make the cap too big and you'll smooth through all transients in your battery voltage (this may or may not matter to you). Make the cap too small and the voltage will drop as the A/D does its sampling. You'll also be limited on how quickly you can sample it, since you need to give the cap time to recharge between samples. You'll also be constantly burning off power from the battery. How much power, and whether or not it's significant, depends on the resistors you choose and the application.2) high resistance with an opamp buffer. This is more complex than #1 and still has the constant current draw, but you eliminate the smoothing error and you can sample it as fast as you want.3) low resistance with a high side switch to shut it off when not in use. This is about the same level of complexity as #2, but you eliminate the smoothing error, the constant current draw, and you can sample it as fast as you want. However, the faster you sample it, the more power you pull from the battery.All three have their place, and I've used all three in different applications. Which one you should use depends primarily on how often you'll be sampling the voltage and how accurate your measurements need to be. If you'll be sampling at 10 Hz or faster, I would lean toward #1 or #2. If you'll be sampling slower, say once per minute, then I would lean toward #3, but that's just me.

author: pugglewuggle
author_rank: Contributor
timestamp: April 18, 2014, 10:41:46 pm
content: Yeah I'll probably do this once an hour or so. I don't care about the transients but I might take a few samples and average them just to make sure no coupled noise screws up the measurement. What do you mean by low resistance and high resistance in orders of magnitude? 10k? 100k? 1M? 10M? Also, what would you say on cap size. They're so cheap I'm just going to put one on the darn thing to make sure it never sags.