Transparent change of Arduino's power supply

Hello all,

I got an issue with my project that I've solved through programming.
But I was wondering if there could have been any solution with an hardware electronic solution (because for some other projects, I may not have a programming solution).

Here is the problem's description :
I'm powering my Arduino Uno through a 220V-9V transformer (T1 below), which is sourcing the 220V from my apartment's electric power supply...
in case I loose this 220V power (disjunction in my appartment for example), a DPDT relay K1 (9V coil) is switched off, and the arduino is then powered from a 9V battery.
My goal was to avoid a reboot of the Arduino board because of the power's interuption during the relay's switching time (and I am monitoring D2 pin to know the power source, doing different things accordingly)
For that purpose, I've added a big capacitor (2mF) C1 to ensure that the Arduino is still powered during the switch.

It's working perfectly well when the power is switching from the battery to the transformer (220V is back)
but it's not working when the power is switching from the transformer to the battery (220V is lost)

The reason seems to be that the transformer has some internal big capacitors as well, and it looks like these capacitors are delaying the moment the relay's down...and once the relay finally switches, the C1 capacitor I've added has already been emptied to compensate T1 loss and power the arduino...
the remaining load is then not enough anymore, and the Arduino stops before the battery powers it.

Any idea on how I could have solved this ? How to force K1 switch as soon as T1 is giving signs of weakness ?
Here is below a quick schematic of my circuit :

The normal solution is two diodes to isolate power sources. You arrange that the backup battery
is a lower voltage than the mains supply and the mains supply takes precedence. You can
even add some sort of trickle-charging to the battery if its rechargable too.

You should use a comparator and a transistor for that.If you look for the schematic of an arduino Uno for example you will find that circuit implemented there.The idea is you sense the voltage on the input and agains a reference voltage and if a difference is found the output of the comparator changes and the FET switchs

Hello MarkT, HugoPT,

thanks !
Am I abusing if I ask for a schematic ? (or point it out to me from an example, the Arduino one for example ?)

the 2 diodes, I don't understand how it works ?
The comparator & transistor, maybe I can get it...I need to think about it !

I'm quite a newbie in electronic...your answers almost make sense for me...but not enough to be able to translate it into a schematic all my myself !

Anyway, if you can't, I'll try to work on it with your feebacks
thanks again !

the 2 diodes, I don't understand how it works ?

If you just attached a 9V power source and (latest after discharging a little) a 8.9V battery to your setup without anything, current would flow from the power source to the battery and bad things would happen. But you know that, therefor you did want you did.
Diodes let currents flow only in one direction, so use a diode to prevent the path from the power source to the battery. Do the same for the path from the battery to the power source, to prevent current flowing from the 8.9V battery to the 0V switched off power source.

Alternatively, design your project in a way that it runs on the full voltage range of a LiPo and use something like this:
It will charge the LiPo in a controlled way while the USB port is powered and power from the battery else. Not sure if untervoltage protection of the battery is already there, you should check (and report :slight_smile: ). But the 8205 IC suggests it has protection.

In this small part you can see how arduino switchs power from VIN pin to USB power safety.You could use the same logic to do what you need to do.



I would try the diode approach. But use 2 or 3 in series on the batterie side. That will ensure that the voltage of a new 9V block is lower than the regulated? wall wart. Since the regulator on the Uno turns the excessive voltage into heat anyways, no extra batterie power is used by the diodes.


Do you mean that such a schematic would work ? (and in that case, of course I could have spared the use of a relay, and a simple transistor would have informed me whether the arduino is powered through the wall adapter or through the battery)

But I still don't get why, in this schematic, the wall adapter would take the precedence on the battery ?
because I really need the battery to be a backup "in case of"....if the day I need it, its down...bad luck !
There won't be any leaks from the battery ?

(I've removed here the C1 capacitor...probably I could remove diode D1 as well)

You need to accurately measure the "9" volts from your power supply when it is powering the arduino. As long as it is greater than the voltage of the battery then all is well and a single diode in the battery line will suffice. PSU units invariably output slightly in excess of their rated output.

Ok. Last question : diodes have leakage current. For example one I've just looked at has a leakage current of 5 uA...
Overtime, won't it damage the battery (which is an alcalin one) to support this leakage coming from the wall adapter ?

In a word "no"

At what differential voltage is your data-sheet specifying the leakage current. I think you'll find it specified at the maximum rated reverse voltage. So, if your diodes are rated at 100 volts and being operated at around 2 volts (or so) reverse bias then the leakage will be either absolutely or virtually zero

Diodes are universally used in electronics as "directional control" devices so worry not.

Makes sense. Must admit I'm far from beeing the best datasheet reader !
Thanks, I'm clear now. And again i've learned a lot.

Ok. Last question : diodes have leakage current. For example one I've just looked at has a leakage current of 5 uA...
Overtime, won't it damage the battery (which is an alcalin one) to support this leakage coming from the wall adapter ?

You are probably confusing the leakage current used to define reverse breakdown voltage
from the leakage of an ordinary diode at 9V reverse bias, which will be sub-nanoamp typically.

Overtime, won't it damage the battery (which is an alkaline one) to support this leakage coming from the wall adapter?

Actually, quite the reverse.

Whist most alkaline batteries carry a warning not to recharge, some are deliberately specified to be rechargeable and almost all are rechargeable to some extent, so a few microamps would serve to "top up" the battery and make it last longer as a backup.