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 Ubiquiti UniFi. 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.
I assume it is the ping that is labeled GPIO17 (pin 11) and not pin 17. Am I correct?
Also… The gpio-shutdown overlay does not appear to be on my Pi 3 Model B. There is no gpio-shutdown.dtbo file in the /boot/overlays directory. Do I need to install it? If so, how?
root@grybo-rpi3:/home/pi# dtoverlay -h gpio-shutdown
* No help found for overlay ‘gpio-shutdown’
root@grybo-rpi3:/home/pi# ls -l /boot/overlays/gpio*
-rwxr-xr-x 1 root root 1036 Jun 22 2016 /boot/overlays/gpio-ir.dtbo
-rwxr-xr-x 1 root root 901 May 4 2016 /boot/overlays/gpio-poweroff.dtbo
root@grybo-rpi3:/home/pi# cat /etc/*release
root@grybo-rpi3:/proc# cat /proc/device-tree/model
Raspberry Pi 3 Model B Rev 1.2
PRETTY_NAME=”Raspbian GNU/Linux 8 (jessie)”
Sorry if I was a bit ambiguous there. You only need to edit boot.txt [sudo nano /boot/config.txt] and add the “dtoverlay=” line into the file.
You’re running Jessie, which I know to be OK (and I’ve done this on the PiZero as well as its bigger siblings like the Pi3 shown in the picture).
To your pin question, you are correct. I’m using GPIO17, and as you can see in the image it’s the 6th pin down on the “inside” row of pins, aka pin 11.
Actually Jim now I’m not so sure about Jessie – but do you realise that’s 2 major releases behind now? “Buster” is the current flavour:
I just upgraded fro Jessie to Stretch and that fixed the problem. Thanks!