Pi-Roject: Using a UPS with Your Pi
One of the risks of running a Pi 24/7 is a sudden loss of power, a tripped socket, a power cut, or someone accidentally kicking the cable. Without warning, this can corrupt your SD card, which as you know by now is catastrophic. The solution is a UPS HAT (Uninterruptible Power Supply Hardware Attached on Top).
This guide covers the Waveshare UPS HAT (B), which piggybacks onto your Pi via its GPIO pins and provides backup power from two 18650 lithium cells. Beyond just keeping the lights on briefly, we will set up a background service that monitors battery level and shuts the Pi down cleanly before the battery dies.
Once everything is in place, the service runs silently in the background. It checks battery level every 60 seconds, warns you when it drops below 15%, gives you 60 seconds to plug in the charger, and if you don't, shuts the Pi down safely on its own.
What You Will Need
Waveshare UPS HAT (B). The HAT itself. The 18650 batteries aren't included and must be sourced separately. Use official Waveshare batteries if possible, and don't mix old and new batteries or cells from different manufacturers.
2× 18650 Lithium batteries. These slot into the HAT and provide the backup power. Handle with care, as lithium batteries can be dangerous if mishandled. Mind the polarity markings (+ and −).
Barrel jack power adapter (5V recommended). The HAT is powered through a barrel jack rather than USB-C. This usually comes with the HAT.
Small Phillips screwdriver. For the mounting screws that keep the HAT attached to the Pi.
The monitor script. ups_monitor.py. You'll install this in Step 4.