One of the things I want for my IoT devices is a smart power button. When off or in low power I would like to be able to press the button and have the device power on. If the device is on I want to press the power button and have the device shutdown safely and either power off or indicate that power can be shut off.
The PI has built in support for the seed of my solution in the form of device tree overlays (dto) that can activate kernel support for a shut down signal and a powered down signal. I will be using these two features to implement a simple shutdown button and running indicator led. Not quite the feature set I am looking for, but this may be useful to pull out for some circumstances. Besides, if I can control a led at shut down I could send a signal to put the PI in low power or have it’s UPS shutdown.
There are many sources on the web that detail these two dto’s . The two I found most helpful were:
shutdown signal: https://www.raspberrypi.org/forums/viewtopic.php?t=217442 toggling gpio at shutdown: https://www.raspberrypi.org/forums/viewtopic.php?t=223157
It should be noted that specifying the gpio pin to use is different in these two overlays. My implementation looks like this (in /boot/config.txt):
dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up
dtoverlay=gpio-poweroff,gpiopin=19,active_low=1
With this configuration I am monitoring pin 17 for a low signal to initiate system shut down. When the system has halted, pin 19 will go low. Pin 19 will go from low to high on startup.
When pin 19 signals shut down there are two low to high transitions that look like this:

So — using this line to control a running led would have the led on while running. When the system shuts down the led will blink once and then go out indicating it is safe to power off. Keeping the led lit adds 10 ma to the measured current draw of the PI. If this is a concern the logic could be reversed and the led could be made to illuminate at system shutdown. I’m going to be using the led as a running indicator, on while running.
These simple circuits are all that we need to implement our running indicator and shutdown switch.

In case it is not clear — those upside diamonds are grounds at the bottom of the schematic. I like the other style ground symbols better.
D1 is a generic led I had on my desk. R1 is 500 ohms because I have so many of them. R3 and C1 are for switch debounce. You can leave these out if you want. I added them because my switch was glitchy when activated:
Without r3/c1:

With r3/c1

And there you have it. With a simple file edit and circuit we now have a shutdown switch and a running indicator for our PI. We no longer have to log in and issue a shutdown command and have to assume it is safe to power off our PI. This is a good first step, but it would be handy if we could get this to be a smart power switch. We will be looking at this in an up coming post.
These links are relevant for what was done here:
These links are relevant for what was done here: https://www.onsemi.com/pub/Collateral/2N3903-D.PDF https://www.thegeekpub.com/246471/debouncing-a-switch-in-hardware-or-software/ https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/ https://www.electronics-tutorials.ws/transistor/tran_4.html Logic captures by the awesome Saleae logic analyzer
If you find this work helpful perhaps you would consider supporting m2ag.labs open source efforts by buying us coffee. Any amount would be appreciated. Please use this link to do so:

Thanks for stopping by.