PiZ SuperCap safe shutdown for Raspberry Pi Zero

Last year, I learned of a new Kickstarter that solved a significant problem with using a Raspberry Pi for embedded “turn key” projects. The Raspberry Pi is a disk-based Linux system, using a microSD card in place of a hard drive. If you kill power to a Raspberry Pi without safely shutting down Linux first, file system corrupt can occur. I have seen this dozens of times over the years on my devices. When it happens, I just reformat the memory card and re-image it and continue.

But now we don’t have to — at least not if we are using the Pi Zero.

Abhinav Shukla created the PiZ SuperCap and launched it as a Kickstarter in 2021. The device is a small circuit board with a capacitor. It connects to a Pi Zero via the I/O header. Instead of plugging the USB power cable directly in to the Pi, you plug it in to the PiZ SuperCap board. This charges the capacitor then begins powering the Pi.

PiZ SuperCap by Abhinav Shukla

If power is disconnected, the capacitor has enough power to run the Pi Zero for a short amount of time (about 15 seconds). It also toggles a GPIO pin to indicate that power has been lost. By running a simple Python script on the Pi, you can now detect when power has been lost and, if it is not resumed in a set amount of seconds, safely shut down the Pi Zero by issuing a “shutdown” command.

I backed ten of these units, and I am glad I did. They work great! Now I can use a Pi Zero for any type of embedded project I want and just kill power when I want to shut down.

How it works

Here are some things to be aware of:

  1. When you first apply power, the Pi Zero will not immediately power up like you are used to. It must first charge the capacitor. The Pi Zero won’t actually start up until about 8 seconds after you apply power.
  2. When you turn off power, if no shutdown script is installed, the Pi won’t turn off until the capacity runs out. On my Pi Zero W (first version), I timed it at 75 seconds when just sitting at a text console login prompt. If you are running a graphical desktop or any programs using the CPU, it won’t last that long.
  3. The PiZ SuperCap will toggle GPIO pin 4 to indicate a power loss. To enable safe shutdown, you need some form of program or script that will monitor GPIO pin 4 and shut the system down when power goes away. The sample code provided uses a 9 second delay before deciding to power off, making the unit act like a mini UPS rather than shutting down on any temporary power blip. Clever.

Shutdown script

The designer provides some sample Python code on the project page, but here is a shorter one I came up with. I am not a Python programmer, so I have no idea if my technique is good. I just wanted it to block until it sees a power loss, and either shut down (after a delay) or continue:

supercap_shutdown.py

#!/usr/bin/python3

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

while True:
        # Wait for power to drop.
        print ("Waiting for power loss...");
        GPIO.wait_for_edge(4, GPIO.FALLING)
        print ("Power loss")

        # Give user 9 seconds to restore power.
        print ("Waiting to see if power is restored...")
        time.sleep(9)

        if GPIO.input(4) == 0:
                print ("Power not restored.")
                os.system("sudo shutdown -h now")
                break;
        else:
                print ("Power restored.")

The above script has print statements in it so you can run it from the console to verify it is working. Those can be removed (or commented out) once you are sure it works.

You will need to do a “chmod +x supercap_shutdown.py” to make it executable.

If you want to make it run on startup, edit the /etc/rc.local file:

sudo pico /etc/rc.local

…and add this line at the end before “exit 0”:

# Pi Z SuperCap monitor script:
python /home/pi/supercap shutdown.py

You can then restart the system (“sudo restart now”) and when it reboots, the script should be running. Disconnect power and after 9 seconds (or whatever time you modify the script to use) it will issue a safe “shutdown” command.

3-D printed enclosure

I have created a very simple 3-D printer enclosure that holds a Raspberry Pi Zero and the PiZ SuperCap. Let me know if this is something you might want.

PiZ SuperCap by Abhinav Shukla in a custom 3-D printed enclosure by Sub-Etha Software.

Support the designer

If one of these might be of interest to you, consider backing the project at:

https://moonshotjr.com/moonfunded/project/piz-supercap/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.