The Problem: On our Raspberry Pi Zeros, we’ve all experienced unexpected shutdowns. Whether it’s a faulty connection or a power outage, SD Card corruption can result in the loss of important data! Simply put, no one enjoys unexpected shutdowns and losing work-in-progress programs and projects.
The Solution: The PiZ-SupCap enters the picture at this point. Simply put in your USB Power cord to the PiZ-SupCap and connect it straight out of the box via the 40 GPIO pins, and you’re good to go!
The Raspberry Pi will be powered directly from the USB connector’s 5V ports. When the power is turned off for any reason, the Pi Supercapacitor will take over and provide power for up to 15 seconds.
For a proper shutdown, we can use the 15 seconds provided to deliver a soft shutdown instruction.
If you don’t want to issue a soft shutdown command every time the power is pulled, we can develop a program that will do so automatically when the USB cable is pulled.
When the power cord is connected, it detects a high level on GPIO Pin #4. When the power is turned off, the GPIO Pin begins to read LOW. We can use this to write a program that advises the Raspberry Pi to shut down when the status of GPIO pin #4 is LOW.
Finally, we’ll execute this program as background software that performs this operation in the background so that if the power cable is disconnected, the background software issues the shutdown command automatically.
The PiZ-simplified Sucap’s schematics are shown below. The power enters the PiZ-Sucap through the J1 Connector. Fuse F1 protects against overcurrent. The SuperCap C1 is at the heart of its design. When the connection at J1 is pulled out, the Raspberry Pi Zero is powered by the charged capacitor C1.
The software can detect if the Cable is pulled out thanks to the resistor Divider R3 and R4. When the Power Cable at J1 supplies power, the junction of R3 and R4 is at 2.5V. When the power cable is removed, the voltage drops to 0V.
We already know that the status of GPIO Pin #4 indicates whether the USB cable is giving power to the board, so we can develop the software to send a gentle shutdown when the USB wire is withdrawn.
Step 1: To write our software, open Thonny IDE or any other Python IDE of your choosing. Copy and paste the code below (to see what’s going on, read the comments).
Once you’ve finished, save the application to a file of your choice.
#Made for Pi Z SuperCap
#Code by Abhinav Shukla
Import RPI.GPIO as GPIO
#Import to read state of GPIO Pin
#Import to execute sudo shutdown command
#Import for sleep function, gives the user time to plug the cable back in
#Uses BCM pin numbering (i.e., the GPIO number, not the pin number)
#Setup to receive input from GPIO pin #4 as HIGH(1) or LOW(0)
if GPIO.input(4) == 0: #0==Low, if it detects cable is pulled
time.sleep(9) #Sleep for 9 seconds, allows user to plug the cable back in in the allotted time,
#change as needed for up to 30 seconds
If GPIO.input(4) == 1: #If after 9 seconds, user plugs cable back in and GPIO 4 reads
os.system(“sudo shutdown -h now”) #If cable not plugged back in, execute shutdown #command
if GPIO.input(4) == 1:
Step 2: Now, we want the program to execute every time the Raspberry Pi is turned on. To do so, simply open a command prompt and enter the following command: “sudo nano /etc/rc.local”
Now scroll down to the bottom of the file and add “python” and your file location immediately before the line “exit 0.” “python /home/pi/supercap shutdown.py” was the command in my instance.
Exit by pressing “CTRL + X,” then “Y,” then “ENTER” once you’ve added that line.
That’s all there is to it! We’ve completed our task. Reboot the Raspberry Pi for the modifications to take effect, and you won’t have to worry about losing crucial data or corrupting your SD Card the next time it loses power!
- Is the Pi SuperCapacitor compatible with any Raspberry Pi? No, the Pi SuperCapatiator only works on the Raspberry Pi Zero, as far as we know.
- How long does the SuperCapacitator’s battery last on the Pi Zero? When completely charged, the SuperCapacitator’s battery lasts about 15 seconds.
- How long will the boards take to arrive? Early December is the target date for US buyers, while early January 2021 is the target date for overseas buyers.
Abhinav S. is a senior year student at Foxboro High School in Massachusetts. He is involved in solving problems related to Raspberry Pi and coding when he is not doing School Homeworks or when not enjoying Basketball. He is applying for College Admission ( A computer Science from NYU or Columbia in New York and NorthEastern in MA are his favorite) Learning and creating products with Moonshot Jr is going to help in the College Admissions.