Powering Your Raspberry Pi with UPS and GPIO Monitoring

Hi everyone, I wanted to share a practical solution I’ve implemented to ensure my Raspberry Pi remains stable, even during power outages. This setup not only provides backup power but also allows the Pi to detect and react to power failures, which can be incredibly useful for automation and monitoring tasks.

The Setup

I use a Raspberry Pi 3 as my openHAB server, and I’ve integrated it with a USB power supply that includes a built-in UPS (Uninterruptible Power Supply) and a slot for a 18650 LiPo battery. This combination provides about 15 minutes of backup power, which is more than enough for graceful shutdowns or to keep the system running until mains power is restored.

The DIY Part

To make the Pi aware of power outages, I’ve added a simple DIY modification. I tapped into the power supply’s circuit and connected a wire to one of the Pi’s GPIO pins. Specifically, I used GPIO4 to detect when the mains power is lost. This setup allows me to trigger rules or notifications when the system switches to battery power.

Configuration

Here’s how I set it up in openHAB:

plaintext
Contact Mains “Mains” { gpio=“pin:4 activelow:no” }

And the corresponding rule:

plaintext
rule “Mains”
when Item Mains changed
then
if (Mains.state == ON) {
sendLogNotification(“Mains active”)
} else {
sendNotification(“…@…”, “BATTERY BACKUP ACTIVE”)
}
end

Benefits

This setup has been incredibly reliable for me. It not only provides peace of mind but also allows for automated responses to power issues. For example, I’ve configured it to send notifications to my phone when the mains power goes out, and to log the event for future reference.

Why This Matters

For anyone running critical home automation systems, having a reliable power solution is essential. This DIY approach is cost-effective and doesn’t require complex modifications. Plus, it’s a great way to learn more about GPIO programming and system monitoring.

Final Thoughts

I’d love to hear if others have implemented similar solutions or have tips for improving this setup. It’s always inspiring to see the creative ways people ensure their systems remain robust and reliable!

Cheers,
Sascha