Category Archives: Raspberry Pi

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:


import RPi.GPIO as GPIO
import time
import os

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...")

        if GPIO.input(4) == 0:
                print ("Power not restored.")
                os.system("sudo shutdown -h now")
                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” 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

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:

Raspberry Pi Pico?

For those who find the unwieldy huge size of the Raspberry Pi Zero off-putting, the Raspberry Pi Foundation has now released a Raspberry Pi Pico.

It uses a custom chip, the RP2040, and seems to be more of an Arduino than a Pi. It has 264KB of memory and can be programmed in C/C++ or MicroPython. Thus, it is not a Linux system.

The unfortunate name of calling it a Pi may cause some confusion.

But it’s still neat. With the Pi group entering this market, it finally gives them something to compete with Arduino. The Pi is great, but having a slow-booting disk base OS that can corrupt the file system if you do not shut down properly each time was not a good fit for embedded designs.

The Pi Zero is $5, and the new Pi Pico is $4. This is a great price point compared to things like the mini Arduinos, but it’s for folks who can solder if you want to hook anything to it. Since the Pi Zeros are sold with versions that have header pins soldered on (for $5 more), it seems likely someone will do that for the Pico allowing folks who don’t solder well (such as myself) to make use of this item.

My current favorite supplier of Pi items is Vilros:

More to come…

Pi Problems.

I have six Raspberry Pis (three Pi Zero Ws, two Pi 3s, and one Pi 4). Tonight I tried to upgrade some of them.

  • My CoCo-Pi 3 unit booted and I was able to update it and install the latest CoCo-Pi emulator package.
  • My second Pi 3 booted to a kernel panic, and I am having to reinstall.
  • My first Pi Zero W got stuck on the Rainbow screen so I’ll have to reinstall it.
  • My second Pi Zero W has a different operating system running on it that I did not touch.
  • I also have a Pi 4 that is a retro game system, but I did not check it.

What makes these systems, when they just sit here idle (not powered on, haven’t been touched in months) not work? Anyone know?

I’ve had many Pis since the very first one came out, and I’ve had many issues with them over the years.

But I still buy more. Dunno why.

Raspberry Pi – what a difference a year (or more) makes!

Like many things that I post, this is a long and rambling story that could be summarized like this: Wow, it sure is easier to set up a Raspberry Pi image today than it used to be!

…and now, the long version:

I began my current embedded C programming job in April 2012. This is what (indirectly) got me exposed to Arduino and Raspberry Pi. While I was certainly aware of both of them (and had even considered buying an Arduino UNO to play with), I had never actually used either. I have previously mentioned how getting to use an Arduino at work led me to using them for a local Halloween haunted house input controller project. I also ended up ordering a Raspberry Pi to have a cheap Linux-style system to learn on.

In July 2012, I ordered  Raspberry Pi from Newark/Element14 and it arrived a month later. (I actually got mine sooner than a coworker who had ordered his as soon as started taking pre-orders.) Getting this Pi going was cumbersome, and involved downloading special utilities that could format an SD card on a Mac in the Linux file format so it could then be booted on the Pi.

As I have mentioned in other posts, I have lately had an interest in retro gaming (a very affordable alternative when you can’t justify buying a $400 PlayStation 4). A friend of mine, that I will call Mike (because that is his name), informed me that a cheap $35 Raspberry Pi could be made in to a nice retro emulation system running MAME and other open source emulators (see the PiMAME project). If you are interested in this, all you need is a Raspberry Pi, an enclosure, USB power supply and an SD card. The whole thing could be bought for around $60.

Unfortunately, I couldn’t get all the emulators to recognize the controller I wanted to use, and Mike had to loan me a USB keyboard so I could navigate the menus since most of them didn’t accept joystick input. Plus, getting game ROMs on to the Pi was difficult since you couldn’t just take the Linux-formatted SD card out and copy games to it directly. Most folks just put their Pi on the home network and used FTP. That’s fine, but I had no ethernet available in the living room where my Pi was hooked to a TV. The Pi has two USB ports, but all the sites say you shouldn’t try to run a wireless adapter without using a powered hub. The end result would have ben a Pi with power supply, a USB hub with power supply, a USB keyboard, USB mouse, a WiFi dongle, plus a cable running to the joystick (if I could get it to work). (Mike used a hybrid keyboard/trackpad thing that cut down on cables a bit.)

Even without getting this far, and just playing games using the USB keyboard, I found that PiMAME wasn’t fast enough to some of the later games I tried. It was, like many open source projects, a clunky mess to get things all going and working together. I am told the PiMAME team is working on making a much nicer front end that would at least take care of the user interface challenges.

Thus, I gave up on PiMAME and switched over to a commercially available Ouya Anrdoid device (thank you, Amazon, for the chance to review this interesting product). The Ouya plays games that the Pi had trouble with, plus has WiFi built in, can read a PC-formatted drive, and also comes with a wireless controller. It still doesn’t seem to support the USB joystick I want to use, but it did recognize my Atari 2600 USB joystick just fine — great for the 2600 emulator!

So tonight I traded my original Pi to Mike, and I decided I would download a fresh Pi image and reset my PiMAME box back to stock Linus. Boy have things changed!

Today, you just go get an SD formatting utility (for Mac or Windows), use it to format the SD card, and then extract a zip file to the card. The Pi can boot off that, and then the rest of the install can be done via a keyboard/TV. There was even a smaller version of the zip that would download the rest over the internet.

I just tried it and it worked fine. One downside: If you choose the NOOBS image, a keyboard is required (but not a mouse; you can use arrow keys and hot keys to do the install). If you choose Raspberian directly, it starts up with SSH enabled so you can hook the Pi to a computer and ssh in to it and complete the install/setup without ever hooking up a monitor or keyboard. Thank goodness I forgot to return the USB keyboard to Mike tonight, else I would have been starting all over with that image. (I haven’t had a wired keyboard/mouse in ages.)

Good job, Pi team. This is so much easier than the first time around.

UPDATE: I have now learned that the “easy” part of this is the NOOBS distribution. If you want to directly install Rasperian, that still requires writing out a raw Linux disk image using special software. The Rasperian image has SSH enabled by default (I think) so you can just insert the SD card, and then plug the Pi up to your router and boot it. You can log in to your router to find the IP address the Pi is using, then ssh in to it to finish configuration. The NOOBS install is a front end to install an OS, so it has no remote capability enabled. Thus, you have to have an HDMI tv/monitor (or composite monitor) and USB keyboard to get it to install Rasperian (or other OS).

UPDATE 2: And, I have found out that my system no longer likes my Transcend 8GB SD card, which used to work fine. Previously, my system had locked up after a system update and I assumed it had just messed up and bricked the image. I never got around to restoring the SD card until the other night. While it boots NOOBS and installs, it hangs during the Raspberrian reboot with some disk error I find in numerous reports online… My card model appears on the compatible list, so perhaps it’s just going bad? Dunno. My Pi is dead for now, until I get the SD card issue figured out.

2014/12/31 UPDATE: The December 2014 release of NOOBS has fixed the issues and now that SD card boots just fine. Win!