As everyone is aware the Pi has no on / off power supply switch the only way to turn it off is to physically remove the micro USB cable or to turn off the main adapter / pull plug from the wall.
Turning off from the wall isn’t an option for me since I share the same socket with my Sony infrared headphones transmitter.
The lack of a power supply switch can also be an issue for battery operation since the Pi would still be providing power to the USB ports even after shutdown mode (init 0) and draining more energy unnecessarily.
After some time messing around on breadboard I have come up with a relay circuit design which I had finally committed to a circuit board and I’m happy to say works really well.
There’s no need for any fancy background software or microcontroller, just a single link wire from the GPIO 14 (TXD) / UART TX pin to indicate the running state of the Pi.
To turn on the Pi you push the “on” button, when the Pi has shutdown the relay circuit automatically kills the power.
There is a built-in delay for detecting the Pi shutdown, this is because a system reboot could be mistaken momentarily as a full shutdown when it’s not.
If for whatever reason you cannot shutdown the Pi maybe a system hang or kernel panic there is a “off” button to instantly kill the power.
The circuit also provides a level of protection against accidental short circuit of the Pi’s 5v power rail so your battery pack / mains PSU won’t fry or pop.
This works because the relay requires power to keep the switch active, a short circuit would cause a voltage drop which in turn causes the relay to lose power and it kills the switch.
This circuit has been released under the Creative Commons Licence for non-commercial usage.
You are free to copy and reproduce but for personal or educational purposes only – http://creativecommons.org/licenses/by-nc/2.0/uk/
The transistor values are not critical any general purpose ones will do i.e. BC548, BC108, BC109, I happen to be using S9014C because they’re dirt cheap from eBay and I still have plenty to use from my previous purchase.
The component value for diode D1 isn’t critical either as long it’s silicon and not a zener diode but it’s required to protect the GPIO pin from being supplied +5v.
The R1 resistor and C1 capacitor are used to set the power off delay, if you want a different duration then change the component values.
The 47k resistor in the circuit is used to charge C1 this is to ensure the transistors will turn on the relay the instant you press the “on” button.
When the circuit is switched on the 47k is disconnected and the GPIO 14 (TXD) is now responsible for keeping C1 charged and thus the transistors stay turned on.
When the Pi is in shutdown mode the GPIO 14 (TXD) pin goes low and C1 discharges until the voltage drops too low for the transistors to maintain power to the relay.
If a system reboot occurs the GPIO 14 (TXD) pin also goes low, but the delay effect provide enough time for the GPIO 14 (TXD) pin to return back to high again.
Since the 47k resistor is required to charge C1 there is a very tiny standby current of approximately 75uA, a small price to pay for an instant startup from the button.
You could remove the 47k altogether but then you would have to press and hold the “on” button for at least 2 seconds which frankly isn’t practical.
If you want to operate the relay as a basic on / off latching circuit with no shutdown detection then disconnect the 47k at the relay end and connect this directly to the PSU +5v rail.
For more detail: Pi on / off PSU relay switch