I needed a way to cleanly power on and off my Raspberry Pi powered arcade machine, as my kids were just pulling the plug on the unit. Even though a Raspberry Pi is pretty durable, I know it was just a matter of time before my SD card got corrupted from this unclean shutdown method.
I searched and found some python code options which requires a script to run all the time looking to see if the power button is pressed. It also required adding this script at boot time. Not terribly difficult, and the methods I tried worked just fine. However, I like things clean and simple.
I found a post by Raspberry Pi/Raspbian expert Matthijs Kooijman explaining how he created a device tree snippet that was added into the core Raspbian OS starting with the 2017.08.16 release. Lots of great details here on his blog page:
So, taking his method, I created a short YouTube video showing how to make this method work.
Momentary Power Button (https://www.amazon.com/gp/product/B0094GP7SQ)
Connector Wires (https://www.amazon.com/HiLetgo-5x40pcs-Breadboard-Assortment-Arduino/dp/B077X99KX1)
We will use PINs 5 for signal input and a ground pin. I used pin 6 for ground, but you can use any ground pin on the board.
Either by SSH or direct from the Raspberry Pi console, enter this:
sudo nano /boot/config.txt
Scroll to the bottom of the file, enter a remark and this line of code:
# Enables PIN 5 as power button, other PIN to ground
Press Control-X and ‘Y’ to save. In order for this to take effect, reboot your Pi.
Connect your power button to PIN 5 and PIN 6 (or another ground).
Thanks for viewing!