This is a build tutorial for a simple soft power on/off circuit for the Raspberry Pi. For the improved version with emergency shutdown, visit this page.
At the center of this circuit is a dual mosfet, the IRF7319. Using this component, the +5v wire leading to the Pi will be switched on and off. The only other components necessary are 2 resistors with an ohm rating of at least 100k. The circuit has been tested with everything from 100k to 1M ohm and all performed fine.
There are 8 pins on the mosfet, but the outputs have shared pins so there are only 6 connection points.
It’s a dual-mosfet (positive and negative) circuit. Pushing a momentary switch for a second or two turns the power on.
The circuit works like this:
The +5v wire runs from the power supply to the input on the P-channel mosfet, and the output from the mosfet connects to the Pi’s +5v input. Power is only supplied when the gate on the mosfet is connected to ground. This happens initially using the power button, one end of which is connected to ground. When it is pushed, the ground connects to the gate of the P-channel mosfet and power is supplied to the Pi.
The problem is that this will only keep the system powered as long as the button is held. This is where the upper half of the component, the N-channel mosfet, comes in. This mosfet works opposite of the P-channel, and has a negative input and output with a positive gate. Current only flows when the gate is positive. The positive gate of this mosfet is activated using the TXD pin on the Pi. This pin is typically used for serial data communication, but it has some characteristics that we will make use of here. It’s not the only pin that can be used, it just has a feature enabled by default.
The TXD pin switches to 3.3v within about a second of the Pi powering on. This means that the initial press of the power button connects the ground to the P-mosfet gate and turns the Pi on, and after about a second of holding the button the TXD switches to 3.3v. The TXD is connected to the gate of the N-mosfet, so when the TXD switches to 3.3v it turns the N-mosfet on. The output of the N-mosfet is connected to the gate of the P-mosfet, so it now serves as the ground connection for that gate and makes holding the power switch unnecessary.
The Pi is now powered on and is stable in that condition.
Now, let’s backtrack slightly. There are also resistors in this circuit. They serve the purpose of making sure the mosfets don’t switch on by accident. The resistor on the N-mosfet keeps the gate at ground under normal conditions, and when the gate is at ground the mosfet is turned off. The resistor on the P-mosfet keeps the gate at +5v under normal conditions, and when the gate is at +5v the mosfet is turned off. These resistors only do their job of pulling voltage down and up when the circuit is powered off, and they have no effect when the circuit is on. They are absolutely necessary. If they are not in place, the system will power on randomly.
There is one more feature included in this circuit, and it’s the ability to fully power the system off when the operating system is shut down. The operation is simple. When the OS shuts down it switches the TXD pin back to ground. Since the TXD pin needs to be at 3.3v to power the gate of the N-mosfet (which in turn powers the gate of the P-mosfet), the whole circuit immediately shuts off. The resistors come into play here as well. If they were not in place, the system may not shut off immediately or may not shut off at all. They pull the gates down and up and kill power to everything. The Pi is now powered off and is draining zero power from the power supply or batteries. It can be powered back on immediately by holding the power button again.
One additional great feature to have is the ability to shut the Pi off using a pushbutton switch. Even better than that is the ability to shut the system down using the same button that turns the system on. With a small addition, this circuit can be used for both the power-on and power-off. One diode is added between the momentary switch and the P-channel mosfet gate, and the other diode is added between the momentary switch and a GPIO pin. I recommend the BAT54C Dual Diode because of its small size. The GPIO pin will detect a press of the switch when the OS is running, and it will issue a shutdown command.
Questions? Comments? Leave a reply or visit the forum.