How to prevent a battery emptying itself into A0 pin ?

Hello,

This is my first post here.
I’m a newbie both in Arduino programming and in electronic in general. Maybe my question will be obvious for you…but it is not for me, and I’m looking for a solution that would prevent me from doing my whole PCB again if possible ! (if I can just solve my issue through programming diffrently my arduino, that would be perfect…If I need to change the PCB…well, it’s a pain…)

I’ve designed a project, gathering information everywhere on Internet, and here especially, in which my Arduino UNO is powered with a 220V AC / 9V DC transformer, and is monitoring a relay. Each time I loose the 220V power, the relay is turning down, and then the Arduino is powered thanks to a 9V battery (the relay has 2 functions : switching the power source between the battery and the transformer, and telling the arduino which is the source. At restart, I know thanks to the relay position what is the power source).
Depending on the power source, I launch some actions (closing or opening safety water valves).

I’ve built as well in my circuit a part that is monitoring the battery level, through a tension divider.

When I’ve tested my system, it worked perfectly…problem is that I’ve tested it over a few minutes…
Plugging in my final system definitively, after having engraved my PCB which communicate with the UNO, I realised that the battery was emptying itself in 3 days…although it was supposed not to be used by anything in the circuit when 220V power is present (which is the case 99.9% of time).
=> a transistor is blocking the battery monitoring (no current is supposed to go through the tension divider until the transistor is activated
In my sketch, I activate the transistor before reading the tension at the tension divider, and then close it again.

According the scheme, and knowing from now on that the battery is emptying, I’ve guessed that I have a leakage current through A0 pin…(I probably should have put the transistor between P6 and R4, see below…but unfortunately, too late >:( )
(If it’s not the cause, then I don’t understand why the battery, which was new, has emptied itself so fast)

Is there any solution to avoid this ‘leakage’ ?

Here is attached my scheme !

on the top left-hand side : P4 is the Arduino’s ground
P5 is the battery negative pole
P6 is the battery positive pole
on the right-hand side, R4 and R5 are the tension divider. Resistor values are designed to monitor a +5V on A0 with a 30mA current (to make sure that battery is loaded enough before reading its tension).

P24 is connected to A0 arduino’s pin
P25 is the pin I put at HIGH value before reading A0, and then back to LOW when it’s done.
=> it’s opening/closing Q1 transistor

Any idea would be highly appreciated !
Thanks,
larbalette

You've switched the divider on the low-side

It must be switched on the high side via a level-shifter.

Better use a very high impedance resistive divider (several megaohms) and add a 10nF
capacitor to it to reduce the output impedance. Then the current drain is negligible.

Thanks MarkT.

You mean that I should have put the transistor between battery’s positive pole and the first resistor of the divider ?
As I said, I’m a newbie in electronic…
What is a level-shifter ? a quick search with Google doesn’t help, it looks to be more a concept to achieve many targets than something really precise ?

Regarding the high impedance resistive divider, I’ve designed it on purpose to drive 30mA (300 Ohms) because I’ve understood that to be reliable, the tension has to be measured when the battery is sollicitated…if not (too low current <=> too high total resistor), the tension may not be a good measure of remaining battery’s life.
Was it wrong ?
Since I check the battery only once a week during a few micro-seconds (the time it takes to read the tension), it was not supposed to be an issue…

The arduino ADC analog input current is 250 uA. How long do you think it would take to drain your battery
at that rate ?
See section 32.11, Fig 32-39 on page 544
ATmega328 Datasheet (complete)

I like your idea for checking the battery charge under load. It is really the only way to get an accurate reading. I think the main problem you are having is that you are switching the divider on the low side. With this arrangement the input pin is above Maximum input voltage when the sample is off. I scoured the datasheet Rasch provided the link to and I can not find a technical description of the input circuitry of the analog input of the chip, so I can not make an informed statement as to what will happen when the input voltage on the input pin exceeds 5.5 volts as specified as max (Vcc+0.5). I am assuming that there is a breakdown or protection happening and that will discharge the battery. If the input current is very low the battery should last much longer than 3 days. You might want to measure the battery current when the sample is off to confirm this. Now for the fix.... You need to design a high side switch and disconect the battery when not sampeling. This could be done with a Fet (best option) or relay (absolutely 0 standby current) or other switching means. I look forward to seeing your results and fixes.

R

Thanks for all your replies.
The battery has probably a capacity around 600mAh. I would say that under 250 uA, it will last 100 days to drain it !

I like the explanation of rdfeil. Something is probably happening because of this tension above 5.5V (i did't realized that i made a mistake with this low-side arrangement). I will measure the current when not sampeling and will keep you posted.

Would it be ok if i just move the current transistor i have from its current position (low side) to high side ?
Or will it generate other issues ? (And that's why you're rather talking about Fet, relay and other fancy things ?)

Well, if you made a PCB without having the circuit properly reviewed, bad luck!

You need a P-FET switching the battery supply to the divider, preferably a "logic" type which will switch at a lower voltage. Its gate is pulled up to its source by a resistor - 1k will do just fine and will draw 9 mA when the gate is pulled to ground by your present transistor.

There are protection diodes on the IO pins, so that if the input goes one diode drop above Vcc (or below ground), the diode is forward biased.

So when you switch the transistor off, the voltage at A0 rises up to about 5.6V and then the internal diode is conducting, and it runs the battery down.

As suggested, use a high-side switch. A logic level P channel MOSFET won't have any appreciable voltage drop, but a PNP in saturation will only drop about 200mV or less with just a few mA of current.

You don't need to draw much power from a little 9V battery to get a good test. A few mA will do.

See the above two posts and pick the one that is best for you. You can not just move your current transistor to the high side as it will effect the switching (emitter above ground) and the base current will be added to the divider current when active (based current exits on the emitter). Both a P channel fet with gate bullup or a PNP bjt with a base resistor will work fine as stated above. I will keep reading your results...

R

There are protection diodes on the IO pins, so that if the input goes one diode drop above Vcc (or below ground), the diode is forward biased.

How did you know that ? I don't see the diodes on the UNO schematic. Where are they ?

raschemmel:
How did you know that ? I don’t see the diodes on the UNO schematic. Where are they ?

I think he ment internal to the micro but I don’t see the reference in the data sheet either. Poly, where did you find this info?

R

rdfeil:
I think he ment internal to the micro…

Correct.

…but I don’t see the reference in the data sheet either. Poly, where did you find this info?

Latest ATmega328P datasheet. 14. I/O-Ports / 14.1 Overview / Figure 14-1. I/O Pin Equivalent Schematic. In other words, the first diagram in the I/O-Ports section. The datasheet for all AVR processors will have a very similar section / diagram.

All I/O pins have protection diodes to both V and Ground as indicated in Figure 14-1.

I saw that in the datasheet but considering it was in the section dedicated to digital I/O I didn't connect that it would also apply to analog I/O. Thanks for the info. I may be an older dog, but I can still learn a new trick....

R

rdfeil:
I saw that in the datasheet but considering it was in the section dedicated to digital I/O I didn’t connect that it would also apply to analog I/O. Thanks for the info. I may be an older dog, but I can still learn a new trick…

One of the more significant lessons regarding the ATmega chips - “analog” pins are digital pins - with the exception of course, of A6 and A7 on the SMD device.

Nevertheless, all I/O pins including A6, A7, oscillator and presumably, Aref will have this protection, the only exception being the uniquely designed Reset pin which is used for high voltage programming. This of necessity will have a completely different structure on the chip.

As I was in a hurry with my previous post (getting to Church), I did not go on to point out that a PNP transistor was an alternative to a high-side FET and that either the resistor used to switch on a PNP transistor or the pull-up on the gate of a FET could be used as the “load” for the battery so that the effect of the switched-on FET or saturated transistor would be quite negligible and the actual voltage divider can then use much higher resistors in the order of 10k.

If you use a PNP bipolar or a P channel MOSFET (logic level), you'll also need an NPN to drive them. Because the base or gate is driven in relation to the Lithium battery voltage.

OK, so using these diagrams, in respect to my previous explanation,

The “load” illustrated here can be the voltage divider, but using two values such as 10k each (if the battery voltage will not exceed 10V) so that when saturated, Q2 will have negligible voltage drop and the divider will be quite accurate.

R2 can instead be the actual load, so that a value of 1k will draw about 9 mA from a 10V battery which will very effectively saturate Q2 (meaning it will take a significant number of tens of microseconds to switch off again).


Similarly, R2 here becomes the battery load, again about 1k to draw 9 mA or 180 Ohms to draw 50 mA. R1 can be about 10k in either circuit.

Thanks for these diagrams. I better understand what I should do...
anyway, I still don't understand one point :
if I build on this diagram below, I've understood that I should read the tension with A0 pin between LOAD resistor's poles ? (U on the diagram)
Is that correct ?

for sure, there won't be any current through A0 until I switch Q3 on...

But then, I don't understand how the divider works : how can U be equal to 5V, with a 9V battery ?

You will still need a resistor divider. The "load" in the diagram will be your two resistors.

ah ! yes...with your explanation, I realize that's what Paul__B meant...very clear, thanks.
So it will be this schematic below. One last thing i don't understand : how to size R3 ? what's its role exactly ?