GUIDE | Pi Safe Shutdown Using Adafruit's Push Button Power

Show off your completed Game Boy Zero, or post your build logs here!
Post Reply
User avatar
infinitLoop
Posts: 521
Joined: Mon Dec 24, 2018 11:46 am
Location: Portland, OR
Has thanked: 214 times
Been thanked: 190 times
Contact:

GUIDE | Pi Safe Shutdown Using Adafruit's Push Button Power

Post by infinitLoop » Sat Jun 27, 2020 12:19 pm

:arrow:

For those struggling with a way to achieve a Safe (software) Shutdown, here is how you can wire the Push Button Power Switch breakout board with any power supply / PSU, using a DPDT sliding switch.

Here is the main product page for the board, but you can buy it many places around the web: https://www.adafruit.com/product/1400

There are also custom-built solutions from Camble designed for AdaFruit's Powerboost model, and Pocket Adventures and Tinkerboy who both have it integrated into their own power supply boards.

... but if you have some of your own power supply boards (the popular generic or "banggood" ones for instance), or just want to do it yourself with a different PSU or whatever... read on...
..

Wiring:
s75-0577p03wl.jpg
s75-0577p03wl.jpg (202.02 KiB) Viewed 114 times


Software:

If you are using RetroPie 4.4 or later, you should only need to add this to the /boot/config.txt file, replacing the ALL-CAPS text with the appropriate GPIO pin number:

Code: Select all

## Safe Shutdown ##
dtoverlay=gpio-poweroff, gpiopin="GPIO_POWEROFF_PIN"
dtoverlay=gpio-shutdown, gpio_pin=GPIO_SHUTDOWN_PIN
If using RetroPie 4.3 or earlier, the "shutdown" overlay isn't installed by default, so you need to do something else to trigger that (or install the overlay, which I have in my GBCZ build, linked in my signature). This approach should also work on 4.4 or later.

You still need the "power off" which I believe exists on all the RetroPie builds, at least back to 4.0:

Code: Select all

## Safe Shutdown ##
dtoverlay=gpio-poweroff, gpiopin="GPIO_POWEROFF_PIN"
And for the "shut down", we can just create a quick python script to handle it..

At a command prompt, create the file with sudo nano ~/shutdown.py and paste this in, updating for your GPIO pin:

Code: Select all

from gpiozero import Button
from signal import pause
from subprocess import check_call

def doShutdown():
    check_call(['sudo', 'poweroff'])

# Button interupts
shutdownButton = Button(GPIO_SHUTDOWN_PIN)
shutdownButton.when_pressed = doShutdown

pause()
it's possible you might need to install gpiozero for it to work, if so just run at a command prompt: sudo apt install -y gpiozero

and finally add it to startup by using sudo nano /etc/rc.local to edit the script, and add the following before the "exit 0" line:

Code: Select all

python /home/pi/shutdown.py &
and that's it. It should power on when you switch to the one side, then trigger the shut down command when you switch to the other side, which will send a signal to the board to power off at the "off" pin when it is done. If the pi is locked up, or otherwise not responding to the shutdown/poweroff sequence, all that you need to do is switch the switch back-and-forth again and it will cut the power - no need to open up the battery compartment to disconnect it!

Good luck! :)


.
Last edited by infinitLoop on Sun Jun 28, 2020 5:35 pm, edited 1 time in total.

User avatar
infinitLoop
Posts: 521
Joined: Mon Dec 24, 2018 11:46 am
Location: Portland, OR
Has thanked: 214 times
Been thanked: 190 times
Contact:

Re: Pi Safe Shutdown Using Adafruit's Push Button Power

Post by infinitLoop » Sat Jun 27, 2020 2:48 pm

I haven't tried it, but I think if you have a PowerBoost or other PSU with an "engage" (en) pin that turns it on, you would just need to wire one more wire from that "en" pin to the unused leg of the switch that is on the same row as the "shut down" ones, and I think it would work the same

.

DaRk_dOg
Posts: 16
Joined: Mon Oct 28, 2019 6:58 am
Has thanked: 16 times
Been thanked: 9 times

Re: GUIDE | Pi Safe Shutdown Using Adafruit's Push Button Power

Post by DaRk_dOg » Sun Jun 28, 2020 11:44 pm

Awesome info as always! Thanks for this guide, it will come really handy at some point.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest