RasPi is a great platform -- but doesn't have any power management as bought. And, running a Linux kernel means that files are open almost all the time. Turning off power is risky.
Per Jeremy Cook it's pretty easy to do this. I bought switches like he suggested, rated at 12v but the leds light up with 5v just fine. These switches have a built-in resistor on the LED, so the wiring to the switch is just 3 wires: ground, gpio3, and led-signal.
- A momentary switch between header pins 5 and ground
- An LED between (arbitrarily) pin 10 (gpio 15) and ground (with resistor, as needed)
- This in
/boot/config.txtwhere the "gpio15" line matches your chosen wiring
[all] # shutdown via button dtoverlay=gpio-shutdown # led on gpio15 gpio=15=op,dh
and wonder-of-wonders -- IT WORKS!
Right up until it doesn't. The latest version (as of June 2023) of the Raspi image installed by the RaspberryPi imager for Moonraker doesn't work with any combination of using GPIO 3 as the shutdown pin. Using another pin works. config.txt gets modified as
[all] # shutdown via button # dtoverlay=gpio-shutdown #default gpio 3 dtoverlay=gpio-shutdown,gpio_pin=4 # first alt # dtoverlay=gpio-shutdown,gpio_pin=22 # second alt # led on gpio15 gpio=15=op,dh
instead of the default above, and the wiring is modified to use this pin (or your choice) accordingly.
The downside (not for me, really) is that the "turn it off" button does not "turn it on" again. Oh well...
Other details, maybe the root cause...