Raspberry Pi Soft On/Off Circuit

Posted by

This is a build tutorial for a simple soft power on/off circuit for the Raspberry Pi. For the more advanced version with emergency shutdown, visit this page.

Required Components:
IRF7319 Dual Mosfet on eBay or Amazon
300K Ohm Resistor on eBay or Amazon

Optional Components:
BAT54C Dual Diode on eBay or Amazon

At the center of this circuit is a dual mosfet, the IRF7319. This is not the only mosfet capable of performing the duty, but it has characteristics that make it well-suited. 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.

MOSFET-DIAGRAM-COLORED

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.

The TXD pin is set to ground when the Pi is off and 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.

Advanced Feature:

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. There is a major software component to this, and a guide is forthcoming.

MOSFET-DIAGRAM-WITH-DIODE

Questions? Comments? Leave a reply or visit the forum.

13 comments

  1. Hey,

    I think this is a brilliant solution! I have googled around alot for a proper powerdown solution, but all I have found seem complicated. This is simple and so elegant with self powered pullup/downs.

    Could you add the dual diode to this you think? I dont quite get from the main build page how this is hooked up and how it works. Is the momentary switch fpc slice soldered directly to the cathode of the diode, pulling down both the P-gate and the gpio?

    Cheers!

    1. That’s right. The anodes of the dual diode are connected to the p-mosfet gate and the gpio (and the gpio has to detect the voltage drop and issue the shutdown). The cathode connects to the momentary switch. I should probably add some more details and pictures showing that the diode should be included if youre doing a gpio shutdown. Also, I’m going to put a software guide together for the shutdown portion.

  2. Thanks for a very nice tutorial!! I was just wondering regarding the software part you wrote about (“The GPIO pin will detect a press of the switch when the OS is running, and it will issue a shutdown command. There is a major software component to this, and a guide is forthcoming.”)

    Do you have any news on this guide? 🙂

    best regards
    André

    1. All my focus has been on getting these boards made. I’ll get that guide up soon. The bulk of the guide is just the installation of RPI.gpio and then running a script at startup that looks for a voltage drop on a pin.

    2. So scratch what I said earlier. Either RPi.GPIO wasn’t included in the earlier builds or it just wasn’t functioning on my install and had to be reinstalled. It’s included and functioning in the newest RetroPie. Anyway, that means it’s very easy to get the shutdown button working. I’ll have an updated guide showing the whole process soon.

  3. Amazing guide, thanks for this.
    One question tough: where exactly do the resistors habe to be placed?
    For the P Channel, is it +5V or on the Switch?
    And on the N channel on the GND or TXD?

    If they have to placed on all the mentioned, how can you do it with just resistors?

    (Sorry for the newb question ;))

Leave a Reply