Powering off and monitoring shutdown status

This is my first post, so apologies if it’s in the wrong place…!

OFF OFF, not standby off!

Because I wanted to build a Pi-based system that completely switches off from the mains, I needed to monitor the GPIO pins to establish when the Pi was truly OFF…! (There are many clever devices already on the market that will manage the Pi’s power, but none I could find that would completely and utterly switch the Pi and its power supply off.)

Using a hand-held oscilloscope I discovered that, uniquely; GPIO 14 (pin 8) drops from a pulsed high of 3.4 V down to just a few millivolts when the Pi shuts down:
Annotated GPIO pin-out diagram, showing pins that hint at the Pi’s power status.


Using this fact, a simple indicator can be built (using a PNP transistor, resistors and LED) which gives the ‘OK’ to pull the power:
Simple circuit diagram for indicating when a pin has gone low, in this case GPIO 14 on the RaspberryPi. This is handy, when combined with a shutdown button (see the links at the end!), for headless Pi units.

Shutting the power off

Now, there are many examples of code which instruct the Pi to shutdown cleanly, but now I need something that monitors the shutdown, and then ‘pulls the plug’ automatically: All that is needed is a simple microcontroller to monitor GPIO 14 (with a slight delay to cater for the pulsed output), wait for a second of so to ensure complete shutdown, then turn off the relay. I used a PICAXE for this, but any µC can be used. (I can supply the PICAXE code to anyone interested)

I won’t provide any details regarding mains electricity: This is not something to be meddled / dabbled / fiddled with!

There are ways to do this safely with wireless remote mains sockets, for example.

Links to two great shutdown button projects that I used as a starting point:

A RaspberryPi shutdown button project

How to use interrupts with Python on the Raspberry Pi

The Pi has a kernel module that’s explicitly for asserting a pin when it goes into “Halt” at the end of a shutdown cycle. There shouldn’t be any need for trickery or pin monitoring. @RogueM has got it up and running, I think.

It can also be done with systemd scripts: https://github.com/pimoroni/picade-hat/tree/master/daemon/lib/systemd/system-shutdown

@gadgetoid I made this “trickery” before I had any clue what I was doing with the Pi!

Also, making up the little indicator circuit on a board like the ‘Slice of Pi’ from CISECO means it can be plonked onto a Pi without anything else needing to be done.

What would be ideal, is a mains switch with a logic-level input in place of a wireless remote. I can’t find one in the UK though…

yes the gpio-poweroff dt overlay would do the trick.

The Picade HAT provides that functionality out of the box (on Raspbian) via a dt blob that implements just that, so while not the primary purpose of the board, it can be used as a smart switch to cut off power to a Pi by calling a shutdown.

And with nothing more than a very basic soft shutdown script and a button you can initiate a halt/wake.

There’s a big downside that I forgot to mention: the mains power needs to be manually switched back on, as power to EVERYTHING is cut. I did this in my test-rig with a momentary action mains-rated switch.