Switching to battery power when no mains is available

Hi

So I am trying to determine how to accomplish this task:
When mains power is available the arduino is powered of it, when mains power is no longer available (the voltage is 0), the arduino runs of a battery. This is used so that the arduino still runs when there is a power cut. I would like this to be accomplished externally to the Arduino. Would anybody be able to give me any pointers on how this would be accomplished or point me to resources that are linked to this target?
Thanks in advance :slight_smile:

What kind of battery, a 3.7 V li-ion cell ? Does it have to get charged when mains power is on ?
Is the mains power a 5V power supply via the USB connector ? Or using the power jack. Which voltage is the power supply ? How much current do yo need, are there motors, ethernet shield, or something like that ?

A very simple solution is to use a suitable mains charger to charge a 12v lead-acid battery which is also driving the Arduino. If the charge current fails, the Arduino just keeps on working. No switching is required.

...R

I would like to avoid constantly charging and discharging the battery, as this will be a long term solution. The battery does not have to be charge when the mains is on. The battery is 4x1.5V aa batteries. The mains is 9v from a wall transformer into the power jack. The Arduino requires roughly 50mA.

bexx123:
I would like to avoid constantly charging and discharging the battery,

That's why I made my suggestion. The only time the battery will be discharged is when the mains power fails.

...R

Four normal AA batteries with a diode to Vin is possible. And a wall wart of 9V to the power jack.
However, it is not a very good solution. The voltage of the batteries, minus the voltage drop over the diode, minus the voltage drop over the voltage regulator, results in a voltage that might be too low.

Which Arduino board do you use ?

The Arduino Uno switches the USB power off at 6.6V (or 7.2 at the power jack).
That means that the current from the battery could flow into the USB port of the computer.

The solution by Robin2 is better.

I think Adafruit has boards that charge a Li-ion cell and also boosts the voltage to 5V.

The current is only 50mA ? So you don't want to use Ethernet or a GSM shield ?
How long should the Arduino keep on working ?
What does the Arduino do ? I wonder what could be done with 50mA that needs to keep on going... perhaps writing data to an SD card ?

I would like the circuit to be a simple as possible, which is why I would rather avoid using a charger if possible. The arduino controls some LEDS which are connected via a transistor, so only the base current is supplied by the arduino. The arduino needs to run for 24hrs so 1200mAH.

I think four AA batteries will not work, sorry. The voltage will be too low within 24 hours.

It is possible to use a simpler Arduino board that requires less current.
When the sleep mode is used, an ATmega328P microcontroller runs for a long time:

The less there is on an Arduino board, the current is needed.

I can't reconcile these two statements. They seem to be polar opposites

When mains power is available the arduino is powered of it, when mains power is no longer available (the voltage is 0), the arduino runs of a battery.

and

I would rather avoid using a charger if possible.

What is not simple about a circuit like this

battery charger ----- battery ----- arduino

...R

Ok I think I have not made myself very clear (sorry!).

I am aiming to produce this as a PCB from scratch for myself and a couple of friends, the PCB will have two inputs, one for the power supply (9V) and one for a battery (I will use a 9V rechargeable). When there is no power coming form the power supply, it automatically switches and uses the rechargeable battery. It will not need to recharge the battery. I could accomplish this using a MCU however I am wondering if there is a simpler alternative.

How about a relay connected (coil) to the mains power source. Route the battery power source via a N/C on the relay. If the mains power fails, the relay drops out and the battery power kicks in.

View the above as a concept.
The relay can be replaced by a different form of switch.

I think you have three good options:

1 ) The relay is a good and simple solution.
2 ) Now that you use rechargeable battery, you can use what the Robin2 wrote. Have the 9V charged by the mains adapter and run the Arduio from the 9V battery.
3 ) Or use the 9V battery with diode to VIN. The 9V from the main power supply should be higher, or else the battery will drain. Is it such a common 9V block used in toys, that will last perhaps half an hour.

If it was for myself, I use option 4: throw in a dc-dc converter somewhere :slight_smile:

Thanks for all of the good answers and the summary, I really appreciate the help. I am thinking of going with option 1 or 2, as they are the most compact. If I was to use option 2, would I have any long term problems with the current constantly flowing through the battery? As I understand with option 2, the battery is not constantly charging or discharging as it is essentially always remaining fully charged, is this correct?

Yes, correct.
Most batteries don't mind a small continues charge current.

You might need a circuit to prevent overcharging. For example if you want to use any wall wart unregulated power supply.

So if I was to consume more power with the arduino ie 120mah, this might pose a problem with solution 2. With solution 3 I just need to connect a 10v mains supply (so its volatge is higher than the batteries) and a 9V battery in parallel, with a diode on the 9V battery, as long as the voltage from the mains is higher (there is power), the current will be drawn from the mains. Is this correct?

Yes.
The Uno has already a diode from the power jack to VIN.
If you also connect a battery with diode to VIN, the current can't go the wrong way, and the highest voltage supplies the power.

I don't see a problem with 120mA and option 2.
Perhaps you need a better charging circuit.

How about a relay connected (coil) to the mains power source. Route the battery power source via a N/C on the relay. If the mains power fails, the relay drops out and the battery power kicks in.

View the above as a concept.
The relay can be replaced by a different form of switch.

Have u considered "compacting" the relay option by using transistor switching?
It will bypass the arduino, which as I understand it was your initial preference.

bexx123:
I just need to connect a 10v mains supply (so its volatge is higher than the batteries) and a 9V battery in parallel, with a diode on the 9V battery, as long as the voltage from the mains is higher (there is power), the current will be drawn from the mains. Is this correct?

In general this is correct. However if you want to leave the batteries on a constant charge you will need to choose the type of batteries and the exact charge voltage carefully.

I would make my own voltage regulator perhaps using an LM317 so I could set it very accurately. I think the charge voltage should be just a bit below the voltage of a fully charged battery - that way it can never be 100% charged or overcharged. And I think I would either use NiMh or a lead-acid battery. Certainly not a LiPo which might catch fiire.

You will probably get useful info from the Battery University website

...R

bexx123:
With solution 3 I just need to connect a 10v mains supply (so its volatge is higher than the batteries) and a 9V battery in parallel, with a diode on the 9V battery, as long as the voltage from the mains is higher (there is power), the current will be drawn from the mains. Is this correct?

The big problem here is when the battery is discharged and you're supplying it with the full charging voltage: the battery will attempt to absorb too much current too quickly.

Page 17 of TI's LM1117 datasheet shows one simple method of creating a battery backup. It's quite similar to what you're describing but the arrangement allows you to limit the current "into" the battery.