A server UPS for Raspberry Pi ensures clean shutdown during power cuts, preventing SD card corruption and data loss. In India, where power fluctuations and brief outages are common, running a Pi as a home server, NAS, or IoT gateway without UPS protection virtually guarantees eventual filesystem corruption. This guide covers UPS HAT selection, GPIO-triggered shutdown scripts, and a DIY 18650-based solution.
Raspberry Pi UPS Requirements
The Raspberry Pi requires:
- Voltage: 5.0V +/-0.25V via USB-C (Pi 4/5) or micro-USB (Pi 3)
- Current: 2.5-3A for Pi 4 with peripherals; 5A for Pi 5
- Clean power: Less than 100mV ripple; brownouts below 4.63V cause instability
- Graceful shutdown: 10-30 seconds of power after outage detection to complete shutdown
The key challenge: the Pi has no built-in power management. It does not know when power is lost and cannot initiate shutdown automatically. A UPS must both provide backup power AND signal the Pi to shut down.
UPS HAT Options for Pi
| UPS HAT | Battery | Runtime | Shutdown Signal | Cost (India) |
|---|---|---|---|---|
| Waveshare UPS HAT (B) | 2x 18650 | 2-4 hours | I2C (INA219) | ₹1,200-1,800 |
| PiJuice HAT | LiPo 1820mAh | 1-2 hours | I2C + GPIO | ₹4,000-6,000 |
| Zbotic 18650 Shield | 2-4x 18650 | 3-8 hours | GPIO (DIY) | ₹300-500 |
GPIO-Triggered Safe Shutdown
The UPS signals power loss to the Pi via a GPIO pin. A Python script monitors this pin and initiates shutdown:
Hardware:
UPS "power lost" output → GPIO 17 (pulled high normally)
When mains power lost: GPIO 17 goes LOW
Pi detects LOW → runs shutdown -h now
Alternative: Monitor battery voltage via ADC or INA219
When voltage drops below threshold → shutdown
DIY UPS with 18650 Shield
Using the Zbotic 2×18650 or 4×18650 battery shield:
- Insert charged 18650 cells into the battery shield
- Connect shield 5V output to Pi USB-C via cable
- Connect shield USB input to 5V 3A charger (mains power)
- Wire a voltage divider from charger input to GPIO 17 for power detection
- Install shutdown script on Pi
Voltage divider for power detection:
5V charger input → 10K resistor → GPIO 17 → 10K resistor → GND
GPIO 17 sees 2.5V when charger present (HIGH)
GPIO 17 sees 0V when charger absent (LOW via pull-down)
Python Shutdown Script
#!/usr/bin/env python3
# UPS Shutdown Monitor for Raspberry Pi
import RPi.GPIO as GPIO
import subprocess, time, logging
POWER_PIN = 17 # GPIO pin connected to charger detect
SHUTDOWN_DELAY = 30 # Seconds to wait before shutdown
LOG_FILE = "/var/log/ups-monitor.log"
logging.basicConfig(filename=LOG_FILE, level=logging.INFO,
format='%(asctime)s %(message)s')
GPIO.setmode(GPIO.BCM)
GPIO.setup(POWER_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
logging.info("UPS monitor started")
try:
while True:
if GPIO.input(POWER_PIN) == GPIO.LOW:
logging.warning("Power loss detected!")
time.sleep(5) # Debounce
if GPIO.input(POWER_PIN) == GPIO.LOW:
logging.critical(f"Confirmed power loss. Shutdown in {SHUTDOWN_DELAY}s")
time.sleep(SHUTDOWN_DELAY)
if GPIO.input(POWER_PIN) == GPIO.LOW:
logging.critical("Initiating shutdown")
subprocess.run(["sudo", "shutdown", "-h", "now"])
else:
logging.info("Power restored, cancelling shutdown")
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
Set up as a systemd service to start at boot:
sudo nano /etc/systemd/system/ups-monitor.service
[Unit]
Description=UPS Shutdown Monitor
After=multi-user.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/ups-monitor.py
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl enable ups-monitor
sudo systemctl start ups-monitor
FAQ
Will frequent power cycling damage my Pi’s SD card?
Yes. Improper shutdowns during write operations corrupt the ext4 filesystem. This is the number one reliability issue for Pi-based servers in India. A UPS with proper shutdown scripting is essential, not optional. Additionally, consider read-only root filesystem (overlayfs) for maximum SD card protection.
Can I use a power bank as a Pi UPS?
Most power banks do not support simultaneous charge and discharge (pass-through). Those that do often have a brief output interruption when switching between modes, which resets the Pi. Purpose-built UPS HATs or the 18650 battery shield provide seamless switchover without interruption.
Add comment