Introduction: Raspberry Pi Power Button
If you're like me, you may have been scouring the internet looking for the best solution to safely turn your Raspberry Pi on and off with the push of a button. While many solutions exist, most require you to download a Python script and make it executable at boot—and there's more than one way to do that. However, a far simpler solution is already baked into the Raspberry Pi. By shorting specific pins and adding one line to a boot configuration file, you can have a fully functional power button in as little as 10 minutes!
Step 1: Assemble the Circuit
Gather the following materials:
- Raspberry Pi (I use the 3A+ with the latest Raspbian Stretch installed)
- 2 female-to-male jumper wires ORAdafruit T-Cobbler Plus for Raspberry Pi (with 40-pin connector)
- 2 male jumper wires (ONLY if using T-Cobbler)
- 1 momentary pushbutton
- 1 half-size (or larger) breadboard
Connect each wire to a contact on the pushbutton. Then connect one wire to pin 5 (GPIO3/SCL) and one wire to pin 6 (GND). Now, half the work is already done! Momentarily shorting pins 5 and 6 by pressing the pushbutton will wake the Pi from a halt state. (The Raspberry Pi remains in a halt state when it is "shutdown" but still connected to power.)
Step 2: Edit System Boot File
Now, you can wake up your Pi after shutting it down. While this is a really great step, you can add a little more functionality to your new power button: you can use the same button to safely shutdown your Pi!
Log in to your Pi (or SSH if using a remote/headless setup), and enter the following into the command line:
sudo nano /boot/config.txt
This will open up a configuration file that your Pi uses when booting. At the end of the file, add the following:
The gpio-shutdown overlay enables the Raspberry Pi to be shutdown when pins 5 and 6 (already connected to the pushbutton) are temporarily shorted. Press CTRL X to exit, then press Y and ENTER to save your changes to the "config.txt" file.
If Using I2C:
You may have noticed that GPIO3 (pin 5) is also the SCL pin for connecting I2C devices. While you MUST use pins 5 and 6 to wake the Raspberry Pi from its halt state, you can specify a different GPIO pin to use for shutting down your Pi, thus freeing up GPIO3 to use with your I2C devices.
To specify a different shutdown pin, open the "config.txt" file, and add the "gpio-pin" parameter to the overlay. For example, your overlay setting would look like this if you were to use GPIO21 (pin 40) as a shutdown pin:
For Further Information:
If you want to learn more about the functionality of this overlay, enter the following:
dtoverlay -h gpio-shutdown
To learn about additional overlays, navigate to the overlay guide in the following directory:
To learn more about the amazing things you can do with "config.txt", visit the Raspberry Pi website here for official documentation.
Step 3: Reboot and Enjoy!
For these changes to take effect, reboot your Pi. Once rebooted, whenever you press the pushbutton, your Pi will safely shutdown. Once your Pi is shutdown, you can press the button again to wake it again from its halt state.
Congratulations! You now have a fully functional power button for your Raspberry Pi!