Battery Monitoring - Voltage Divider - Page 1
Forum section: electronics / projects

author: Nikos A.
author_rank: Regular Contributor
timestamp: July 02, 2019, 03:44:20 pm
content: Hi everyone,I want to monitor the battery level with my ESP32 using a voltage divider and ADC.. ESP32 is going to be at deep sleep mode for long periods (1h) before waking up to transmit data.I want the voltage divider ON only while ESP32 is ON, in order to prevent wasting energy on the restive load.Looking for a solution I found that I can implement the aforementioned using a n-channel and p-channel low Q mosfets and controlling the N-channel Gate.Is there a single chip that integrates both n-channel and p-channel mosfet intended for this purpose?Is there another solution to control the voltage divider?Thanks in advance

author: splin
author_rank: Frequent Contributor
timestamp: July 02, 2019, 07:30:19 pm
content: You could drive the p-channel mosfet via a capacitor directly from a GPIO. The capacitor needs to be sized to keep it's charge voltage sufficiently low to fully turn on the mosfet after accepting the 7.6nC total gate charge required to turn the mosfet on.The mosfet has .9V maximum threshold voltage. Assuming Vbatt and ESP32 supply voltage have a minimum of 2.3V, the capacitor can charge to a maximum of 1.4V with 7.6nC; Q= CV so capacitance must be > 5.4nC. The gate current of the mosfet will be negligible once the mosfet has turned on but the pull up resistor will continue to charge the capacitor - so increase its value, and/or the capacitance, to keep the mosfet on long enough to take your measurement with the ADC.Alternatively you might be able to use a zener instead of the capacitor, if you could find a suitable value that works with all combinations of battery and supply voltages and min and max midget gate threshold voltages. Obviously the capacitor/ pull-up resistor time constant will determine the minimum time between measurements.

author: David Hess
author_rank: Super Contributor
timestamp: July 03, 2019, 03:29:06 am
content: Q91 could be replaced with an I/O pin driven charge pump.

author: Mr.B
author_rank: Unknown
timestamp: July 03, 2019, 03:38:17 am
content: Can you not just use higher value resistors to achieve lower current drain without the complicated switching?2M2 and 4M7

author: Dubbie
author_rank: Unknown
timestamp: July 03, 2019, 04:08:15 am
content: Heh. I just designed this exact same circuit yesterday for the same purpose.I just used a BSS138 and a BSS84.I did consider using a GPIO instead of the N-Channel, but I wasn't confident enough to run with it.

author: David Hess
author_rank: Super Contributor
timestamp: July 03, 2019, 04:17:43 am
content: The advantage of the charge pump, other than removing the need for the level shifting transistor, is that a stuck I/O pin in either state disconnects the divider. It also removes any need to regulate the gate voltage although a zener shunt for protection is still a good idea no matter what method is used.It works at 5 volts but diode voltage drops make it unsuitable at 3.3 volts and lower.

author: Nikos A.
author_rank: Regular Contributor
timestamp: July 03, 2019, 05:48:39 am
content: Thank you for your answersQuote from: Mr.B on July 03, 2019, 03:38:17 amYes I can but I am looking for a better (more optimized) solution.Quote from: splin on July 02, 2019, 07:30:19 pmI like this approach, I am going to test on the breadboardQuote from: David Hess on July 03, 2019, 03:29:06 amI've never used a charge pump but thank you for letting me know about this technology!!

author: Mr.B
author_rank: Unknown
timestamp: July 03, 2019, 06:55:13 am
content: Quote from: Nikos A. on July 03, 2019, 05:48:39 amMore optimized?I tend to question your approach.I think you are over complicating the solution.3v7 / (2M2 + 4M7) = 536 nanoampsThis is less than peanut power.The MCU uses a fairly standard sample and hold ADC. The low impedance issue only exists while sampling.To overcome this issue, simply put a 0u1 capacitor across the 4M7 resistor.I have done exactly this on numerous projects using ESP8266 and ESP32.

author: Dubbie
author_rank: Unknown
timestamp: July 03, 2019, 07:32:46 am
content: Does such a high r value affect accuracy through leakage?

author: Zero999
author_rank: Super Contributor
timestamp: July 03, 2019, 07:57:50 am
content: I believe the leakage into the ADC input is tiny. Check the data sheet.Even higher value resistors can be used, if the potential divider is buffered with a FET input op-amp,

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 03, 2019, 11:43:56 am
content: You can definitely use much greater values for the resistive divider, several hundreds kohm or such. Add a bypass cap between the analog input and ground to make up for it (it will additionally low-pass filter VBAT, which probably makes sense).A solution I already used for this, requiring no additional transistor, is the following: if your MCU has IOs that can be configured as open drain, use one to control the resistive divider. Connect the OD IO to the lower end of the resistive divider (instead of connecting it to ground), and connect the battery directly to the higher end of the divider. When not measuring VBAT, just set the IO to '1' (which will make it HI-Z): then the current draw through the analog input should be negligible. When you want to measure VBAT, set the IO to '0', which will switch the lower end of the divider to ground. You can then measure VBAT after a short settling time. You can disconnect the divider again once the measurement is done, you don't even need to do it only when you're putting the MCU to sleep.Edit: for the solution above, it will of course depend on the voltage range of VBAT vs. VDD of your MCU. When the divider is disconnected, you'll get a power draw of (VBAT-VDD-Vf)/Rup, Vf being the Vf of the internal clamp diode of the IO (~0.3V to 0.6V) and Rup being the value of the upper resistor of the divider. So whether it's worth it compared to other solutions really depends on those parameters.Otherwise, you can still use an OD IO instead of Q91 to save an extra transistor.

author: Siwastaja
author_rank: Super Contributor
timestamp: July 03, 2019, 12:06:18 pm
content: Indeed much greater values can be used, but don't go too far - even PCB surface contamination leakage could cause issues.I have seen people intuitivelythinkthat if you have a leakage resistance of, say, around +/-1GOhm, then having a measurement impedance of, say, 10Mohm would be fine as it would be "two orders of magnitude less". But this two orders of magnitude less would translate into generating a 1% measurement error, which probably is already significant for a battery SoC measurement.(Surface leakage seems to be hard to quantify reliably.)The same goes for the ADC input leakage current, even if it's tiny, you need to be at least 3 orders of magnitude away from it to keep the error below 0.1%. Of course, if the leakage is constant, it can be calibrated out, but how do you know it's constant?All this being said, I'm still almost 99% sure you could just increase the divider values to solve the problem in a simpler way.Note that while the ADC's DC leakage is small, it consumes considerable charge every time you measure. There is a cheap, passive way, and a slightly more expensive, active way to deal with this:1) a capacitor at the ADC pin (say, 10nF, or a BOM reuse power decoupling cap of 100nF),2) an opamp driving the ADC input.1) suffers from the fact that the driving source needs to replenish the cap after each measurement, and if your resistor divider values are big, this takes time. Otherwise, the results start to drift.For example:With a ADC sampling capacitance of 20pF, a 10n capacitor would cause approximately an error of 20/10000 = 0.2 percent (the chance of voltage in the 10n cap)duringsampling. So we don't want to go much below 10nF, it starts making a difference.With a 10n cap and a 2M2 + 4M7 divider talked about earlier, the RC time constant is 1/(1/2200000+1/4700000)*10*10e-9 = 15 milliseconds. After 5 time constants, i.e., 75ms, the capacitor is charged to 99.3% of the final value, assuming it started from zero, of course, which is a fair assumption to handle the edge cases like startup. Of course, in practice, the ADC reading only diminished 0.2 percent of the capacitor, so there's less charge needed to be replenished. In any case, this means you can only trig the ADC every 75ms. Put it in a free-running conversion mode at 10ksps by accident, and you'll likely seeseverelydrifted measurements!Circuit-wise, such a capacitor is simplest, by far, and often analog filtering is desirable. This is often the best for battery measurement, unless you need to measure and trace voltage spikes, i.e., sample at higher rates. If this is the case, you'll need an opamp to buffer the divided signal for the ADC.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: July 03, 2019, 04:28:20 pm
content: If using an op-amp buffer, you can get ultra-low-power op-amps that have an enable/select pin that can put it in to a standby mode, where quiescent current is typically in the order of nA.Although, one caveat with those is that after asserting the enable/select pin it can take tens of milliseconds for the output to stabilise, so not too useful for frequent sampling.

author: Dubbie
author_rank: Unknown
timestamp: July 03, 2019, 05:49:40 pm
content: Thanks for the tips everyone. I think I will switch to high value resistors + cap for my design. I have no need to sample quickly as it is just battery voltage I am measuring. I just didn’t want my device to destroy batteries if they were left on the shelf for 6 months. I think I should be fine.

author: floobydust
author_rank: Super Contributor
timestamp: July 03, 2019, 05:55:52 pm
content: Final note, the ESP32 has a sloppy ADC spec accuracy is +/-6% unless you do calibration, and there is no spec for ADC input current/capacitance that I could find. They just say use a 0.1uF cap.

author: Mr.B
author_rank: Unknown
timestamp: July 03, 2019, 07:57:07 pm
content: Quote from: floobydust on July 03, 2019, 05:55:52 pmYes it does have a sloppy spec.The OP says that the MCU will sleep for an hour at a time.This infers that the battery monitoring will be done once only after the MCU wakes up each time.This means that the 2M2 + 4M7 divider, with 0u1 capacitor is perfectly suitable for the application.With the ADC accuracy being +/-6%, telling the exact battery voltage is difficult.In the projects I have done using ESP8266 and ESP32, I was quite happy with 'About Full', 'About Half', Getting Low' indications...

author: Nikos A.
author_rank: Regular Contributor
timestamp: July 04, 2019, 07:05:43 am
content: Thank you for your answers!! I've convinced to proceed with high resistor values and cap for my project also

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 04, 2019, 12:46:47 pm
content: Note that using a measurement of the battery voltage to estimate its state of charge is per se a difficult/slippery problem and highly depends on the type of battery being used.I don't think I have seen what type of battery you're going to use, but for instance a +/-6% error on a single-cell LiPo, assuming you have set your divider to give a full scale at 4.2V, will yield a ~250mV error, which is IMO way too much for a LiPo battery to get even just a remote idea of its approximate SoC.

author: Siwastaja
author_rank: Super Contributor
timestamp: July 04, 2019, 12:50:48 pm
content: For li-ion (except LFP), voltage measurement tends to be good enough, because the curve has a large span, and it's almost linear from about 100% to 10%: 4.2V-3.2V is 27% of the nominal voltage, a massive difference between full and empty!For all other battery types, voltage measurement is almost useless, unless combined with a lot of other information.6% error is of course too much for any battery type, including the easiest (li-ion), but such error can be easily calibrated out. Do the bog standard two-point calibration and calculate offset and gain correction and that's it. For best result, average the battery voltage in software (for example, cumulative average filter) over a minute or two, and apply nonlinear lookup table to transform from voltage to a percentage (you'll find battery measurement curves online, so no need to measure yourself), and you have a very capable system.

author: Dubbie
author_rank: Unknown
timestamp: July 04, 2019, 07:22:29 pm
content: That was my plan exactly siwastaja. Thanks for confirmation.

author: GamingUnleashed
author_rank: Newbie
timestamp: June 09, 2022, 08:26:52 am
content: does anyone know how much current is needed to be available for a esp sensor to read the voltage ?unsure if I should be using k or m Ωwanting to monitor 4x 12v batteries in 48v banksto keep an eye out for dead cellshad a bit of a melt down when 1 cell died and the other batteries boiled away @ 17v100k with 25k,11k,7.1k,5.3k = 1.5Ma1m with 250k,111k,71.4k,53,3k = 150μathen of-cause I could do 10m but I am assuming that will lead to false readingsI did buy those 25v voltage sensors from ebay but they just popped smoke when trying to read more than 1 battery while using the same esp they had 30k + 7.5k smd resistors , so just going to make a VD in series and try again

author: gamalot
author_rank: Super Contributor
timestamp: June 09, 2022, 08:44:49 am
content: Use a high-value resistor in the voltage divider circuit and put a capacitor in parallel with the low-side resistor.

author: macboy
author_rank: Super Contributor
timestamp: June 09, 2022, 02:15:06 pm
content: Quote from: GamingUnleashed on June 09, 2022, 08:26:52 amFirst, some people here get their knickers in a bunch when units and prefixes are used incorrectly.Volts is V not vAmpere is A not aMega is M not m (m is definitely milli, and milliohms is used as much as Megaohms so don't swap M/m).With those cheap "sensors" (expensive voltage dividers), did you connect one across each battery then connect them all together (i.e. connect their grounds together)? That is a recipe for flash/bang/smoke. Every one needs its ground at the '-' terminal of the battery bank, not the '-' terminal of each battery. So one measures battery 1; the next measures battery 1 and 2; the next 1,2, and 3, etc. Then you do the simple math to calculate the voltage of each individual battery.As gamalot said, you can use quite high value resistors and put a cap across the lower one (effectively from the ADC input to ground) to provide a low impedance for the ADC to read accurately. 1 MOhm is fine. Using 0.01 µF to 0.1 µF would be fine. As above, each voltage divider connects to the same ground reference, not directly across each battery.

author: Terry Bites
author_rank: Super Contributor
timestamp: June 09, 2022, 03:31:26 pm
content: Leakage currents in MOSFETS can give rise to significant errors high Z dividers