Battery Monitor - Proper Way - Page 1
Forum section: electronics / beginners

author: vitormhenrique
author_rank: Unknown
timestamp: July 20, 2015, 07:55:58 pm
content: Hello Guys,Quick question: If I have a 5v battery, can I monitor the battery voltage straight to the arduino analog pin? Or should I use a resistor divider?I know if the battery voltage would be greater than 5V we would need a voltage divider to not fry arduino.But If I direct link the a battery (that has less than 5V) to the analog pin, what is the internal resistance of the atmega chip? how much current it will draw? I don't want to consume to much energy (obviously) measuring the battery voltage.So I can think of three ways to build the circuit:Plug the battery directly on the atmega chipPug a resistor in series directly to the atmega chip, to possibly reduce the current drawnMeasure the voltage between a voltage divider, but I'll end up losing resolutionThank you very much,Vitor Henrique

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 20, 2015, 09:17:48 pm
content: Is there a separate 5V or higher supply powering the Arduino? If so, you can attach the battery directly to an analog pin and set the Arduino reference to Vcc. The analog input will have a very high input impedance, so you don't need to add a series resistor to reduce current.

author: retrolefty
author_rank: Super Contributor
timestamp: July 20, 2015, 09:22:26 pm
content: Not sure I've ever seen a 5 volt battery before, but assuming you have such a beast..There are two conditions you need to be concerned about and deal with.1. Wiring +5vdc directly to an analog input pin is not a problem as long as the micro has at least +4.5vdc or so applied to it's power pins (Vcc, Avcc). So as long as you have a power plug or switch that removes the 5vdc battery to both the micro's Vcc pins as well as the analog input pin you should have no problems.2. If you do ever have a condition where the +5vdc to the analog pin is active but the Vcc is removed from the micro then the input pin's clamping protection diode will conduct with possible chip damage resulting. One fix is to wire a 10k ohm series resistance between the 5 volt battery and the input pin which will limit the current to a non-damaging level.

author: apis
author_rank: Super Contributor
timestamp: July 20, 2015, 10:10:16 pm
content: Not an expert but these are my thoughts if it helps.In general you want the source resistance to be low (lower than 10k). If necessary use a voltage divider to make sure battery voltage is less than Vcc, but try to make the output resistance of the voltage divider lower than about 10k (the output resistance is equal to the divider resistors in parallel). A voltage divider will drain the battery continuously though, but if the current through the divider is low (less than 10mA) you could ground it through one of the I/O pins. Just put that output pin low when you are about to measure the voltage.So for example with a divider with 10k and 90k output resistance would be 9k and the current draw would be about 5V / (10k+90k) = 50 uA wich is no problem to sink with one of the IO pins.For better resolution you could also use an op amp circuit like the one in answer 1 in the following page to scale the input battery voltage range into the range of the ADC:http://electronics.stackexchange.com/questions/18264/subtracting-two-voltages-using-an-op-amp

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 20, 2015, 11:11:39 pm
content: Quote from: apis on July 20, 2015, 10:10:16 pmA low source resistance is only required for fast moving signals at high acquisition rates. For monitoring a battery, which I suspect will change voltage very slowly and will be read at long intervals, the source resistance can be much higher. A divider of even 1Meg total would probably work in this situation.However, from what has been described, it doesn't look like a divider will be necessary as long as the processor's Vcc remains higher than the maximum battery voltage.If protection frombattery connected with no Vccis required, asretroleftydescribed in his second point, the series protection resistor could actually be much higher than 10k, for the same reasons as above.

author: apis
author_rank: Super Contributor
timestamp: July 20, 2015, 11:26:22 pm
content: Quote from: MLXXXp on July 20, 2015, 11:11:39 pmYes, that seems to be true <10k is to ensure a high sampling speed. If the battery voltage is less than 5V a series resistor on the input might be simplest/safest.

author: Yansi
author_rank: Super Contributor
timestamp: July 20, 2015, 11:27:46 pm
content: BS. The maximum source resistance is defined exactly by the sampling time and sampling capacitor charged voltage error < 1LSB. You can calculate that from the sampling cap capacitance and overall circuit impedance (sample switch resistance, etc..).If I remember right, for the AVR is suggested to have <1kohm in the datasheet. You can check yourself.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 20, 2015, 11:34:27 pm
content: From the ATmega328P (etc.) datasheet:Quote

author: apis
author_rank: Super Contributor
timestamp: July 21, 2015, 12:09:46 am
content: Anyway, best solution is probably to have source resistance < 10k and make sure that battery isn't connected to AD when power is off, after all, draining a battery to 0 will destroy it and usually make a mess (not sure what the application here is?). Can be done with a transistor or with a two pole power switch as retrolefty wrote. Maybe add a resistor in series so total input source resistance is > 500 Ohm just to make it foolproof if there is a problem with the switch or during prototyping etc.

author: vitormhenrique
author_rank: Unknown
timestamp: July 22, 2015, 05:43:26 am
content: Guys!Thank you all for the responses!@retrolefty you are correct, the battery is not actually 5V, I but 5V as an example, because i know the battery voltage will not be higher than vcc (5V).My case I want to measure an alarm the user when a lipo battery (one cell) get's in a low voltage state.The battery will go to a step up circuit to 5V and feed the atmega, I want to alarm the user when it get''s to a point that it should be charged again. As the processor might turn off due to the low battery I guess is better to put a resistor in series to to make sure that that case is also covered!Again thank you all for the help!

author: kripton2035
author_rank: Super Contributor
timestamp: July 22, 2015, 06:02:05 am
content: it would be less waste of energy if you power the atmega directly under 3.3V using the 3.7-4.2 volts the battery providesinstead of stepping up the whole thing to 5v. just my 2 cts.

author: mikerj
author_rank: Super Contributor
timestamp: July 22, 2015, 06:53:05 am
content: Quote from: MLXXXp on July 20, 2015, 11:11:39 pmNot necessarily true. The input leakage current will determine the maximum usable source resistance at low frequency or DC levels.The OP hasn't said what he is using for the ADC reference voltage either. Obviously you can't power the AVR Vref from the same supply you are trying to measure, and you can't regulate the battery supply to the AVR and measure the battery voltage directly with no divider.One trick that may be useful is to power the AVR (and ADC reference) from the battery supply, and then measure an e.g. 2.5v voltage reference on one of the ADC pins. Some simple maths on the ADC result will yield the battery voltage.

author: apis
author_rank: Super Contributor
timestamp: July 22, 2015, 07:42:54 am
content: Quote from: mikerj on July 22, 2015, 06:53:05 amI think the mega is nice enough to have an internal bandgap voltage reference, so you only need an external reference if you really care about accuracy.

author: mikerj
author_rank: Super Contributor
timestamp: July 22, 2015, 09:49:42 am
content: Quote from: apis on July 22, 2015, 07:42:54 amCan you select Vdd as the ADC reference and measure the internal reference on one of the ADC channels? If so then this is probably the best way forward for the OP if he is determined not to have a potential divider for some reason.

author: tautech
author_rank: Super Contributor
timestamp: July 22, 2015, 10:27:51 am
content: Quote from: vitormhenrique on July 20, 2015, 07:55:58 pmThis has been my preferred device:http://www.maximintegrated.com/en/products/power/supervisors-voltage-monitors-sequencers/ICL7665.html

author: Jeroen3
author_rank: Super Contributor
timestamp: July 22, 2015, 10:52:41 am
content: Best practiceis to use a high impedance resistor divider + small capacitor to reduce theabsolute maximumbattery voltage to the level of internal Vref.Use a at least 100k as R1 and a few dozens of nF. Since you'll not be sampling at Ksps the ADC impedance can be neglected.

author: apis
author_rank: Super Contributor
timestamp: July 22, 2015, 11:09:23 am
content: Just an idea, if one power it directly from the battery without regulation couldn't one just measure the internal vref?

author: apis
author_rank: Super Contributor
timestamp: July 22, 2015, 11:38:10 am
content: Quote from: Jeroen3 on July 22, 2015, 10:52:41 amA buffer cap on the analog input would reduce the source resistance the adc sees, but why would you want to reduce the battery voltage so that max battery voltage matches internal vref?Vref is 1.1 V so if Vcc is 5V you would only utilize 5% of the ADC range in the case of a single cell lipo battery like here?EDIT: I see you can set AVcc to be the internal ref as well, so that makes sense, but one can also use Vcc as AVcc and then measure the value of the internal reference.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 22, 2015, 03:39:03 pm
content: Quote from: mikerj on July 22, 2015, 06:53:05 amYes, you have to take into account leakage currents and input impedances for just about any type of electronic input. My point was that you could go quite a bit higher than the recommended <10k source impedance in this particular situation, involving a slow moving (almost DC) signal and long intervals between sampling.For instance, the ATmega328P and Atmega32u4 used in the most popular Arduinos both are specified to have a typical ADC input resistance of 100M.

author: Yansi
author_rank: Super Contributor
timestamp: July 22, 2015, 03:51:28 pm
content: No you cannot, omg, dude.The low impedance needed is there for a reason. And as long as you can't change the sampling time much on AVR, you will measure rubbish!the src impedance together with S&H circuit switch and the capacitance creates an RC circuit, which must be charged fast enough, fort the error to be small enough, usually calculated for an error of less than a LSB!The interval in between taking samples doesn't matter. What matters is the sampling time, the time the sampling capacitor is connected to your voltage source.The partial solution is to use external cap on the ADC input, if High-Z source.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 22, 2015, 04:22:48 pm
content: Quote from: vitormhenrique on July 22, 2015, 05:43:26 amThere have been some comments about alternate methods of powering the Arduino and monitoring the battery, but in case you want to stick the what you've stated above, note the following:The difference in voltage between a LiPo cell at full charge and discharged is quite low (about 4.2V-3.2V=1V). To accurately determine that your battery has reached the desired "alarm" voltage will require your ADC reference to be quite precise a stable.If you're using a voltage boost circuit to provide 5V Vcc and using Vcc as the ADC reference, and you're software calculates based on a 5V reference, the actual output voltage of the regulator circuit will have to be quite close to 5V (probably within 1%) to obtain an accurate enough battery reading. Otherwise, you may end up giving the "alarm" long before the battery needs to be charged, or too late.If you can't guarantee a precise absolute known value for the supply/reference voltage, but for a given unit this voltage, though not necessarily exactly 5V, is always the same when the battery reaches the "alarm" voltage, you can calibrate each individual unit and store the necessary calibration values in EEPROM. You might want to do this even if you're making only one unit. For multiple units, each one will need it's own separate calibration, though

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 22, 2015, 04:38:32 pm
content: Quote from: Yansi on July 22, 2015, 03:51:28 pmYoucanchange the sampling time on an AVR!Insingle conversionmode the sample time is the time between when the last conversion ended and the start of the next conversion. As long as a given input is selected, the internal switch that allows charging of the sample/hold capacitor via the source is always on until it is opened to "hold" the voltage for the conversion. It then closes again immediately after the conversion. Therefore, the sampling time required to charge the capacitor can be infinite, as long at the voltage you're sampling doesn't change.

author: apis
author_rank: Super Contributor
timestamp: July 23, 2015, 07:27:54 pm
content: Quote from: MLXXXp on July 22, 2015, 04:38:32 pmI cant find anything about it in the datasheet but sounds reasonable, you assume the ADC isn't used for other things meanwhile though... I don't understand why you'd wan't to add more source resistance than necessary for protection (500 ohm), seems like it will only make life more complicated?

author: apis
author_rank: Super Contributor
timestamp: July 23, 2015, 07:31:58 pm
content: Quote from: vitormhenrique on July 22, 2015, 05:43:26 amAn alternative would be to use the internal comparator, might be simpler if you just want an alarm. Then you need a voltage divider and set it so that battery min voltage == internal v ref voltage.

author: UPI
author_rank: Regular Contributor
timestamp: July 23, 2015, 08:06:58 pm
content: I use the following method to reduce the 5V to a lower value to be sent to the ADC through a 7.5k resistor. We do many reads while on the ADC input and do a bunch of reads/mean to make sure we have a solid read. We also use a precision 4.096 reference on the AREF pin.I can't remember the exact voltage setting, but we set the TL431 to something like 3V and then monitor the remaining voltage dropped across the 499 ohm resistor. In other words, any change of the 5V signal is only reflected in the voltage drop across the resistor as the TL431 stays at a solid 3V.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 23, 2015, 08:26:22 pm
content: Quote from: apis on July 23, 2015, 07:27:54 pmUnless bymeanwhileyou mean during the battery sample time, you can use the ADC for other things between battery readings. However, when you want to read the battery you would have to select the battery's ADC input pin and then delay at least long enough for a sufficient RC settling period, based on the resistor you chose, before starting the conversion.QuoteThe current flowing through the series protection resistor will drain the battery until you disconnect it. The higher the resistor, the slower the battery will drain. This may be important in a low current battery powered situation.If you're only reading the battery occasionally and not using the ADC for anything else, then there probably wouldn't be any additional complication. Otherwise the only complication is adding a small delay.

author: apis
author_rank: Super Contributor
timestamp: July 23, 2015, 08:53:50 pm
content: Quote from: MLXXXp on July 23, 2015, 08:26:22 pmYes, but if you also ensure the battery isn't connected when you turn it off there isn't any drain at all.Quote from: MLXXXp on July 23, 2015, 08:26:22 pmIt also becomes more susceptible to noise.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 23, 2015, 09:03:49 pm
content: Quote from: apis on July 23, 2015, 07:31:58 pmAssuming an ATmega328P, with this method you would have to be able to individually trim the voltage divider for each unit and re-trim it if the processor chip is changed. This is because, according to the datasheet, the internal reference can vary between 1.0V and 1.2V (about +/-9%) from chip to chip. This reference tolerance isn't good enough to allow accurately detecting a low battery condition, using a voltage divider with a single fixed ratio.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 23, 2015, 09:10:58 pm
content: Quote from: apis on July 23, 2015, 08:53:50 pmIf you can ensure that the battery isn't connected when you turn it off, then you don't need a protection resistor at all.QuoteSince the battery voltage changes very slowly, you can add a capacitor between the input pin and ground to dampen any noise.

author: apis
author_rank: Super Contributor
timestamp: July 23, 2015, 09:39:55 pm
content: Quote from: MLXXXp on July 23, 2015, 09:03:49 pmThen it's not good enough to measure the voltage with the ADC either... which means OP will either have to trim it for each unit or add an external reference in either case.Quote from: MLXXXp on July 23, 2015, 09:10:58 pmprecisely

author: MLXXXp
author_rank: Frequent Contributor
timestamp: July 23, 2015, 09:49:58 pm
content: Quote from: apis on July 23, 2015, 09:39:55 pmThe comparator only gives you a go/no-go indication. Being able to read any value of battery voltage via the ADC gives you the option of calibrating, to compensate for reference variation, in software using values saved in EEPROM, instead of requiring a hardware trim. (But hardware trim would still be possible as well.)Depending on the design, it's also possible that Vcc would have the required tolerance, which could be used as the reference.

author: apis
author_rank: Super Contributor
timestamp: July 23, 2015, 10:59:09 pm
content: Looks like the regulator the latest arduino (uno rev. 3) is using is trimmed to ±1.0% accuracy at least, so yes would be better to rely on that if you cant trim/calibrate it.