Raspberry Pi Simple Soft On/Off Circuit

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.

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. 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. 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.

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.

MOSFET-DIAGRAM-WITH-DIODE

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

Share this with others

33 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!

    • 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é

  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 ;))

  4. Can post the python script for the shutdown om raspberry pi? Do we need a supercapacitor on the +5V input to PI connection? The reason is the raspberry pi needs some time to shutdown. Your advice? Thks

  5. I’d love to use this in an NES — can this be modified to use a non-momentary switch such that when the switch is closed it stays on, and when the switch is open it safely shuts down?

          • Yes, it should. I just hadn’t had a chance to respond. It works because your switch is acting much like the momentary switch, and this circuit doesn’t care if you keep the switch engaged. If you want the OS to automatically shut down when you slide the switch to the off position, you’ll need software to pull a pin high look for it to go from low to high. Having the switch in the on position will keep the pin low.

  6. Have been looking on the net for a way to build a on/off switch for raspberry and found that, seems simple to implement, thanks!!

    Sure its a newbie question but…. I have a doubt about how to put the momentary switch , its connected to the diode cathode and to the ground power supply?

  7. I just tried to measure the TXT voltage and can confirm, that the TXT Pin will go to 3.3V right after the pi has been powered on. Unfortunately in my case the pin will go back to 1.2V just about a second later when the kernel is loaded.

    Serial port has been enabled here but is now explicitely disabled using enable_uart=0.

    However it shows up in dmesg:
    root@raspberrypi:~# dmesg|grep -i ttyAMA0
    [ 2.111682] 3f201000.uart: ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2

    What am I doing wrong here?

    • I’m updating the guide for the improved power schematic, and I’ll be updating this one too once I’m done. Previous versions of the OS pulled the TX pin high and kept it there, but the new ones don’t appear to do that. I used that pin because it made the process easier for everyone.

      Anyway, your solution is to add a line to config.txt
      dtoverlay=gpio-poweroff,gpiopin=14,active_low=”y”

      That will pull the TX pin high at bootup and low at shutdown. You can change the gpiopin=14 to use any other GPIO pin if the TX pin continues to be difficult.

  8. Hm using this setup I will end up having Pin 14 at 3.3V and again Powered of for a short while then switching back to 3.3V when the Kernel starts to boot.

    Thus I will end up having to hold the button until I bridge this off gap.

    Any workaround?

  9. It takes about 2 seconds till the first positive edge appears. High level is gone again after one second (or even less) and will re appear after another 2 seconds.

    I think about using a monoflop to generate a power-on pulse of ~6 seconds.

Leave a Reply