A long time ago, I covered a UPS extension board for RaspberryPi from 52pi. It was a great option at the time to keep your favourite board from shutting down abruptly on power loss. Unfortunately, the unexpected shutdown was still on the cards as the board had no way of letting the Pi know it is about to run out of power. That’s all about to change thanks to Waveshare’s UPS HAT(Amazon, AliExpress, ThePiHut).
Power loss protection with Waveshare UPS HAT
A recent power cut at work, which lasted almost an hour, reminded me that despite living in a country where power cuts rarely happen, it’s good to have your buttocks covered. After all, pulling the plug on a Raspberry Pi board can result in data loss and what’s worse, data corruption.
I think ThePiHut find out about that, as a day later I received a tempting email listing various Raspberry Pi items in stock, including the Waveshare UPS HAT. Who could resist the price of £21 for a 18650 powered board with interesting features?
The board screws up to the bottom of Raspberry Pi 4, 3B+/3 and supplies 5V with up to 5A peak current to keep the board operational even at the most demanding loads, with USB peripherals at work. Unlike the old 52pi counterpart, apart from 2 pogo pins delivering the power, Waveshare UPS HAT uses 2 extra pogo pins to communicate the UPS data via I2C. This design doesn’t obstruct the 40-pin header so you can slap extra HATs on your Raspberry Pi board.
The board requires 2 x 18650 batteries which output 8.4V to keep everything powered. To keep your board protected Waveshare UPS HAT is equipped with overcharge/discharge protection, over current protection, short circuit protection, and reverse protection – in case you decide to change the batteries without paying attention to the orientation. As a bonus, you also have an extra USB-A power to charge or power up another device. Waveshare UPS HAT comes with a dedicated 8.4V/2A charger and uses a DC jack to deliver the power. Once installed, you can ditch the Raspberry Pi power supply.
UPS but how long?
It’s near impossible to answer this question upfront, as it depends on a couple of conditions. First, you have to take the batteries capacity. 18650 comes in various flavours and Pi’s runtime will depend on the mAh stored.
Secondly, the power use of the board will change based on the number of peripherals connected and the amount of power Raspberry Pi needs to perform the computational operations. The board can draw as little as 300mAh for Raspberry Pi 3B in idle state to 3 times as much for the latest board during power-intensive benchmarks.
You should expect around 2-4h based on your load and the cells included. The good news is that your Raspberry Pi will be aware of how much juice Waveshare UPS HAT has, and can perform a shutdown safely when the power levels get critical. I fitted mine with 1200mAh cells and run a CPU intensive benchmark to see how long would the board last. With the current draw oscillating between 0.6-0.8mAh, the board shut the system down (I have it set at 25%) after exactly 2h.
The batteries were charged up again 2:20min later.
Attach the Waveshare UPS HAT according to instructions, and power everything up. To receive the power usage data from the HAT, you have to enable the I2C interface in
sudo raspi-config. Then try out the default Waveshare’s script to read the data from the board:
sudo apt-get install p7zip wget https://www.waveshare.com/w/upload/4/4a/UPS_HAT_B.7z 7zr x UPS_HAT_B.7z -r -o./ cd UPS_HAT_B python3 INA219.py
The script works on the latest version of RaspberryPi OS (Bullseye) but if you come across any issues like:
ModuleNotFoundError: No module named 'smbus'
Install supporting libraries:
sudo apt-get update sudo apt-get install python3-smbus python3-dev i2c-tools
The installed code will give you access to the following metrics:
- Load Voltage
- Current (charge and discharge rate)
- Power Usage
- Battery level in %
- PSU Voltage (optional)
- Shunt Voltage (optional)
The sample code doesn’t include the safe shutdown routine, so you have to implement it yourself. I added a quick and dirty IF statement that shuts down my Raspberry Pi board at 30% power. Why 30%? It’s not healthy to discharge the cells, and I’m more concerned about safe shut-down than keeping the board running without an external source of power. After all, if the power supply is down, chances are, my entire automation has no power as well, leaving me only with the battery-powered ZigBee devices.
When triggered, it will schedule the shutdown, exit out the script and display a message:
Shutdown scheduled for Sat 2022-01-22 16:23:54 GMT, use 'shutdown -c' to cancel.
The system will terminate a minute later in a safe and planned manner.
It would be nice to see a simple battery indicator LED included on the PCB, but knowing that you can get the data from the board, you can always design your own indicator.
Waveshare UPS HAT (Amazon, AliExpress, ThePiHut) is a nice improvement over the other UPS board I had. It keeps the server alive long enough to either survive a power cut or me accidentally blow up the main fuse or shut down safely when the power isn't restored in time. With dozen or so battery-operated ZigBee sensors, I have the incentive to log all that data even during the shutdown, and do my best to keep the system and collected data intact. After all, better safe than sorry, and £21 is a fair price to pay for that peace of mind. All I need now is a much better case! Let me know if you have any questions in this Reddit thread.
💳📈 - See the transparency note for details.