How to Build a DIY Raspberry Pi UPS Battery Backup System

Raspberry Pi's are vulnerable to power cuts which corrupt the SD card. Build a DIY Uninterruptible Power Supply (UPS) to protect your Pi.

By Tim TrottRaspberry Pi Projects • December 18, 2017
How to Build a DIY Raspberry Pi UPS Battery Backup System

Raspberry Pis are low-cost computers, great for education and projects, and ideal for home servers. They are, however, vulnerable to power cuts, which corrupt the SD card. This simple DIY project allows you to build a DIY Uninterruptible Power Supply (UPS) from a mobile phone power bank and a few lines of code.

An Uninterruptible Power Supply (UPS) is a peripheral which provides emergency battery power to servers when the input power source or mains power fails. While most UPS devices are quite large and heavy, smaller devices are used to protect individual PCs. These smaller devices are more affordable, with prices around £100. However, they are overkill for a Raspberry Pi. In this DIY project, we will use a mobile phone power block to provide a battery backup for the Raspberry Pi, which is both low-cost and small. If you have a spare power bank lying around not being used, this project may not even cost a penny. This affordability makes this project accessible to all DIY enthusiasts, regardless of their budget.

Raspberry Pi DIY UPS
Raspberry Pi DIY UPS

A mobile phone power bank is ideal as it already has a USB charger and typically a micro USB out. In the most basic form, all that is required is to plug the main adaptor into the power bank and the power bank into the Raspberry Pi. Depending on the capacity of the power bank, you may get a few minutes to several hours of battery use. Some newer ultra-high batteries may even last days, but I'd recommend anything over 4,000 mA. This straightforward setup makes the DIY UPS project a breeze for anyone.

Some power banks cannot be plugged in continuously. Ensure the power bank has suitable overcharge protection and can regulate internal temperature. This will help prevent damage to the power bank batteries and prolong their life.

Raspberry Pi DIY UPS
Raspberry Pi DIY UPS

To be fully protected, you need to have the Raspberry Pi automatically shut down when the power is cut. We need to have the Raspberry Pi connected to an ethernet hub, which is not on the protected circuit. The plan is for the hub to lose power when the power is cut. We can then detect this from the Raspberry Pi and issue a shutdown command. Of course, this will also cause the Raspberry Pi to shut down should the cable be disconnected. Still, for most home server applications, this isn't an issue.

We will be using a small bash script that will allow us to shut down the Raspberry Pi during a power outage and log the events in the system log. This script is crucial in ensuring the safe shutdown of the Raspberry Pi, protecting it from potential damage during power outages.

sudo mkdir /opt/check_lan
sudo pico /opt/check_lan.sh

Copy in these lines for the script:

#!/bin/sh

# cron script for checking LAN connectivity
# Change 192.168.1.1 to whatever IP you want to check.
IP_FOR_TEST="192.168.1.1"
PING_COUNT=1

PING="/bin/ping"
IFUP="/sbin/ifup"
IFDOWN="/sbin/ifdown --force"

INTERFACE="eth0"

FFLAG="/opt/check_lan/stuck.fflg"

# ping test
$PING -c $PING_COUNT $IP_FOR_TEST > /dev/null 2> /dev/null
if [ $? -ge 1 ]
then
    logger "$INTERFACE seems to be down, trying to bring it up..."
        if [ -e $FFLAG ]
        then
                logger "$INTERFACE is still down, shutting down..."
                rm -f $FFLAG 2>/dev/null
                sudo shutdown -h -t 0
        else
                touch $FFLAG
                logger $(sudo $IFDOWN $INTERFACE)
                sleep 10
                logger $(sudo $IFUP $INTERFACE)
        fi
else
#    logger "$INTERFACE is up"
    rm -f $FFLAG 2>/dev/null
fi

Next, we need to make this script executable:

sudo chmod 700 /opt/check_lan.sh

Now we need to edit the crontab:

sudo pico /etc/crontab

And add this line so that our script runs every two minutes.

*/2 * * * * root /opt/check_lan.sh >/dev/null

That should be all good to go.

What this is going to do is create a scheduled task which runs every two minutes. This task will call a bash script to check the LAN connection. It will first ping the IP address defined at the start of the script, which, in this case, is my router. If the ping fails, it will write a temp file stuck.fflg, drop the ethernet connection and restart it. The script then ends. Two minutes later, it checks the ping. If it still doesn't work, the temp file is removed, and the Raspberry Pi is shut down. If the ping does work, it removes the temp file and carries on as normal. This detailed explanation of the script's functionality ensures you understand how the Raspberry Pi shutdown process works.

The total time between first checking the connection and the shutdown is 4 minutes. You can change this value to anything you want; ensure your battery lasts at least this long. If the battery runs out before the script runs, you risk damaging the Raspberry Pi.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 3 comments. Why not join the discussion!

New comments for this post are currently closed.