We have a small but growing collection of Raspberry Pi devices here. They’re performing a range of tasks: one’s our Pi-Hole, another runs the “Homebridge” middleware between Apple’s HomeKit and our Clipsal C-Bus, whilst a third is running the management app for the Ubuntu WiFi. And of course plenty more are strewn around the place running various test builds of the intvlm8r.
All of these are running headless, so it means if we want to shut them down for any reason we first need to SSH into them to initiate a ‘sudo shutdown now’.
There are some ‘shims’ on the market that provide on/off switch functionality and even potentially some power management, but they’re relatively expensive and generally overkill for our purposes.
A well kept secret is that the Raspberry Pi has for a while now had the ability to initiate a shutdown from an IO pin, and even to start it up again at the OS level – no scripts or fancy hardware required!
All you need is a momentary switch and one line of code in config.txt.
Open config.txt in nano (or your preferred editor):
sudo nano /boot/config.txt
Copy or re-type the below into the file. The “#” line is inert – it’s just a comment for when you return later and wonder what you did there.
#This sets up the ability for a pushbutton switch to shut it down: dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up,debounce=1000
Save and exit the file with control-X, Y <return>.
Connect your switch to GPIO 17 and ground, and you’re in business. The “debounce=1000” means you need to hold the button for a second before it will shutdown, protecting against accidental knocks and bumps. To power it up again, just remove and reapply power. (See the image for the pins.) This works on the big Pi’s as well as the Zero.
Finding a shallow enough through-hole pushbutton might be your next challenge. We’ve settled on an Apem “9633NCD” from Farnell/Element14/Newark, which conveniently fits in one of the holes already in the MCM enclosure we use. Pololu jumper wires are used for the connections to the board.
|Switch||$7.68 (Element14)||$4.53 (Newark)|
|Box||$9.49 (Element14)||$4.99 (Newark)|
|Wires (12″)||$28.95 /50 (RobotGear)||$19.95 /50 (Pololu)|
|Wires (6″)||$19.95 /50 (RobotGear)||$12.49 /50 (Pololu)|
Check out the documentation for more you can do with “dtoverlay”.
I sometimes use “gpio-poweroff” to drive a LED that lights when the Pi has stopped and it’s safe to remove power.
7th March 2020: This is the initial publication.
21st March 2020: Edited to step you through the process in a little more detail.