Raspberry Pi ON/OFF Power Controller
Power up your Raspberry Pi with a latching push-button controlled MOSFET switch
It would be convenient to use a momentary contact push button switch to turn ON and turn OFF your Raspberry Pi (RPi). A press of a button should apply power to the micro USB header, and keep it ON while the Raspberry Pi initializes and starts its application programs. There should be several options for powering OFF the Raspberry Pi:
- The RPi should be able to turn itself OFF fully autonomously, under program control, automatically on an internal timer, or in response to a network signal.
- You should be able to initiate a turn OFF sequence by momentarily pressing an OFF button. The Raspberry Pi should then shut-down its application program in an orderly way, and turn itself OFF.
- If the RPi fails to turn itself OFF or crashes, you should be able to press the button longer to force it OFF.
A soft power switch with this behavior can be implemented using a high-side latching MOSFET power switch. The latching and toggle ON/OFF power switch circuit for controlling microcontrollers shown here is easily adapted for controlling a Raspberry Pi in this manner. Here's the electronic circuit to use:
The schematic shows you two options1) for applying the switched 5V power to your Raspberry Pi. You can apply it through the micro USB connector, in which case it passes through a PolySwitch™ resettable fuse (rated at 1.1 A)2) before being applied to the rest of the RPi circuitry, or, you can apply it to header
P1, in which case it bypasses the fuse. The advantage of applying power to P1 and bypassing the fuse is that the 5V is then passed on to the USB connectors unimpeded, where it can drive high current USB devices.3)
Whichever pins you choose for applying the 5V to the board, you should use the GPIO 4 signal on P1 pin 7 for feedback to the circuit. The Raspberry Pi is then able to shut itself OFF by driving that pin low.
Using the Raspberry Pi GPIO 4 signal for both input and output
- The circuit uses a single Raspberry Pi input/output pin – the
GPIO 4signal. Because the GPIO signal numbering is different from and inconsistent with the underlying BCM2835 chip pin numbering and the RPi connector numbering, let me be explicit: We use the
GPIO 4signal which appears on pin 7 of the 26-pin header P1. Some signals differ between RPi Rev 1 and Rev 2 – but the
GPIO 4signal appears on pin 7 of P1 for both versions.
- By default
GPIO 4is initialized as an input, but idles high because it is pulled up by an internal resistor. While we don't know the precise value of the pull-up resistor, it's probably in the range of 40 KΩ - 100 KΩ as discussed here. Being pulled high doesn't affect the circuit much during startup, as the internal pull-up resistor is effectively across the 100K resistor of the circuit. As soon as the circuit powers up, in response to a button press, the
GPIO 4input is pulled up internally. It actually acts as a very weak output pin, helping to pull up the gate of the N-MOSFET, and keeping the circuit ON.
- The circuit's 100 KΩ does pull the input towards 5V, and in general you shouldn't connect an input to anything greater than 3.3V as that will forward bias the input substrate diode and inject current into the RPi's 3.3V internal supply rail. However, in this case, the current injected is very small, only about (5-3.3-0.5)/100 KΩ = 17 μA, and poses no harm of burn-out or distortion of internal signals. The RPi's input pin limits the voltage applied to the N-MOSFET gate to about 3.9 V, but that is still more than sufficient to keep the MOSFET fully turned ON.
- The 1 KΩ protects the Raspberry Pi pin from excessive current in the event that it is inadvertently set as an output high while the button is held down.
- While the circuit is in the OFF state, it draws essentially no quiescent current. The only current drawn is the inconsequential reverse leakage current through the diode and MOSFET transistors.
Power latching circuit operation and software control
- The circuit uses two MOSFET transistors in a latching feedback arrangement. The high-side PMOS power switch provides power and is controlled by the complementary NMOS transistor.
- When input power is first applied, the MOSFET power switch comes up in its OFF state if the jumper is in the Auto-OFF position, or ON in the Auto-ON position. You may replace the jumper with fixed wiring. While OFF the circuit consumes no power, and when ON only the P-MOSFET's small ON resistance dissipates power, and that is extremely low.
- When your Raspberry Pi is OFF, a momentary button press turns it ON.
- When your Raspberry Pi is ON, a momentary button press doesn't affect the power switch, but it can be detected by the RPi if
GPIO 4is configured to produce an interrupt on a falling edge, or if polled frequently enough. Your RPi can then implement an orderly shut-down.
- You can also use
GPIO 4to turn OFF the power. After the RPi detects a button press, or at any other time determined by its application program,
GPIO 4can be reconfigured as an output and set low. That forces the MOSFET latch to switch OFF after several seconds.
- Ready or not, your RPi can be forced to power down by pressing and holding the button for more than three seconds.
When the Raspberry Pi is first powered up,
GPIO 4 is automatically configured as an input with passive pull-up. That is the behavior we want, so you don't need to do anything to guarantee it. Subsequently, the application program should do the following:
- It should configure
GPIO 4to call an interrupt service routine when it sees a falling edge or an input low.
- The interrupt service routine should initiate a shut-down of the any applications running and whatever I/O they use.
- Once the applications have been shut-down safely, your program should reconfigure
GPIO 4as an output and set it low.
- Setting the output low resets the MOSFET switches to their OFF state after several seconds, turning OFF your Raspberry Pi.
Creative Commons Attribution-ShareAlike 3.0 Unported License.