Battery Backup For RasPi Keeps Your Data Safe

We’ve all raised a clench fist in anger over lost data, and it’s usually the result of unjustified optimism and lack of planning. [George] shared his solution that prepares for the worst: a circuit that provides backup power to a RasPi and its hard drives. [George’s] Pi setup runs as both an Apple Time Machine server and a website backup server, and a power outage could corrupt the data stored on the Pi’s attached hard drives.

Rather than turn to commercial solutions, however, [George] wanted to take advantage of the Pi’s low power consumption and create an inexpensive custom circuit that would safely and automatically power down the devices upon loss of power. To detect a power failure, the build connects one of the Pi’s GPIOs to an opto-isolator, which—through a zener diode—connects to the 12V wall adapter: though [George] welcomes suggestions for alternative methods of safely identifying a mains power loss. The rest of the circuit serves as a trickle charger for the two attached 9V batteries and as a regulator to supply the correct voltage to the RasPi. Power MOSFETs connected to a GPIO handle the delayed power off.

You can view (and edit!) the circuit online here and find the relevant source code on [George’s] website. If you want to build your own RasPi file server, try cramming all the parts into an old optical drive enclosure.

17 thoughts on “Battery Backup For RasPi Keeps Your Data Safe

  1. Maybe you could use a relatively large capacitor. Enough to cope with the time it takes to switch over from mains to battery power. Then you could measure the voltage across it. When it begins to dip you know you have a certain amount of time to switch over the power supplies, halt and processes you need to close and issue a shutdown command.

  2. A simple circuit, which can be a bit improved:

    1. Replace the whole optocoupler thing with a resistor divider. (5.6K + 15K).
    2. Replace the whole 2 MOS switch with a single NMOS, logic level, disconnecting the ground of the battery.
    3. Replace the 9V batteries with 6 NiMH AA 1.2V cells, or better a 6V lead acid stand-by battery, or 2 3.7V lithium cells. .
    4. Make a proper charging circuit, even the low current charging will destroy the cells within 1-2 years.
    5. Add a low battery detector and shut down only when there is no more battery left. I suspect most surges will be short enough to enable no shut down.

    1. even simpler: 2 LiIon rechargeable batteries with protection circuits and attach them and the 12V source through schottky diodes to the raspi, this eliminates the necessity for switching the batteries at all.

      Charging could be set up either with a proper charger, or if the protection circuits are trustworthy enough through a couple of resistors and diodes charging the cells until the battery overvoltage trips. The protection circuit will then stay in overvoltage mode until power is cycled.

  3. The batteries are switched off because the raspberry pi still draws the whole amount of current even after it has “soft shut down”, it is not like a computer.

    Also, relying on the protection circuits inside the battery is a bad idea.

  4. The two common battery chemistry that are suitable for UPS are NiCd and
    Lead Acid as they allow for handling continuous float/trickle charge.
    Your average NiMH or Li batteries would have limited life as they can be
    damaged by that.

    Would be much better off to use a 6V lead acid battery with a float
    charger. Use OR’ing diode to switch the DC rail and regulate the voltage
    down to 5V for the RPi. Add undervoltage shutdown circuit if you want
    to protect the battery. You can get regulator chip with shutdown pin.

    Use a switcher that can do 100% duty cycle if you want good efficiency.

  5. How does this work in regards to a RasPi as a backup server? I can’t think that he is really backing up to a SD card. If not, and he is using a USB HD or something wouldnt this need a UPS (or much larger battery) anyways?

    Once again it seems as if the RasPi is being used as a solution looking for a problem, and a legacy PC would be better suited for this task at the same cost.

    1. He is using the 9 volts to have enough power to keep the hardrives running. He is probably using portable usb powered drives.
      A legacy PC? No that would suck too much power. A NAS like the PogoPlug or any number of other NASs that support USB connections to a UPS? Yep that would work just fine. Still in interesting hack

  6. I did this with a PIC. Much reading didn’t give me a way to remove power safely because I couldn’t know when the Pi had finished the shutdown procedure. Eventually I did it by monitoring the video out signal of the Pi and used that to tell the PIC that is was safe to remove power. Its all in the Project logs forum ^^ up there under FORUM.

  7. The author rightly says you should use diodes rated at 2A but the schematic shows some 1N4148s. Be aware that the 1N4148 is a very low power device not even remotely close to 2A. The ones in series with power supply or battery will melt in milliseconds.
    On the breadboard however it uses the more robust 1N400x series ones.

    On Ebay some chinese vendors sell cheap charging and protection boards for lithium cells and step up regulators. A much more efficient version could use these boards plus one or more 18650 cells.

      1. I do see your point, but this would allow you to run from just an ignition-switched supply (e.g. cigarette lighter socket) and not have to pull your car apart for a non-switched supply.

  8. Wikipedia says: “Trickle charging means charging a fully charged battery under no-load at a rate equal to its self-discharge rate, thus enabling the battery to remain at its fully charged level.”

    Will that work for discharged batteries too? According to this i am afraid, that you will have to charge the batteries in proper charger outside this circuit every time they will get flat after pover failure… Because trickle charging will not charge them as they will be discharging at the same speed… or is that wrong?

Leave a Reply to Harvie.CZCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.