I am building a pi project that is battery powered and is powering something through the 5V GPIO. What I want the pi to do is to start up on a button press, do its thing and turn itself off again without the intervention of the user. The thing is that since it is battery powered, it is not very efficient to keep powering the pi and the 5V peripheral. Therefore, I am looking to cut the circuit between the pi and the powerbank after shutdown. To make it fit in the encapsulation, I am looking for something relatively thin, but it can be spread out (max 1cm thick).

I have been looking online for a circuit but couldn't figure it out, so I am posting my question here. (I have little experience in eletronics)

How can I start the power supply to the pi after a button press and cut the power supply again when it did its task to save energy between tasks?

Thank you in advance for any solutions, circuit designs, or products that do this!

  • Apart from thinking of a mechanical flip-flop'ish element, I guess there must be a way to do this purely electrically. For example, I was looking at this tutorial electronics-lab.com/project/… - 4 minutes into the video, it seems there are two circuit points, one for ON one for OFF (before it goes on to put them to the same switch), so you might adapt that if you manage to "short" the OFF circuit part from the computer.
    – 0__
    Commented Dec 15, 2021 at 22:34
  • I think a "5V Flip-Flop Self-locking Latch Relay Module" might be a way to do this if you wire it up so you can toggle the relay from the Pi to turn it off again. Perhaps this helps: projects-raspberry.com/pi-on-off-psu-relay-switch
    – 0__
    Commented Dec 15, 2021 at 23:00

2 Answers 2


This can be done without a lot of additional circuitry. There was a question similar to yours earlier this year, and this answer was proposed. The updated schematic shown below was subsequently prepared, and this circuitry was built and tested. (Yes - it worked.)

You've said you want the RPi to do its thing and turn itself off again without the intervention of the user. The circuit below will support that requirement, but just to be clear, your code will need to recognize when the task is completed, and issue the sudo halt command. When you are ready to re-start the RPi, you will simply press the momentary pushbutton labeled START in the schematic.

A description of this circuit's operation follows:

  • The schematic assumes that the RPi initially has no power applied; i.e. the relay contacts between pins 5 & 8 of K1 are OPEN.
  • Pressing the START push-button switch momentarily causes Q1 to conduct, which energizes the K1 relay coil between pins 1 & 16, in turn causing the relay contacts between pins 5 & 8 of K1 to CLOSE. The Vcc terminal of the Power Source is now connected to the RPi's Power Input.
  • Due to the action of the latching relay K1, the 5-8 relay contacts will remain closed until the opposing coil (pins 2-16) is energized.
  • With power applied, the RPi will boot, and begin its operations.
  • With the GPIO-POWEROFF overlay for pin 26 added to the device tree (/boot/config.txt), issuing a halt, poweroff or shutdown command to the RPi will cause pin 26 to toggle as shown in the oscilloscope trace below.
  • The logic high (3.3V) toggle at Pin 26 will cause Q2 to conduct, which energizes the K1 relay coil between pins 2 & 15, in turn causing the relay contacts between pins 5 & 8 of K1 to OPEN. The Vcc terminal of the Power Source is now disconnected from the RPi's Power Input. Since the halt command was issued, the RPi is powered down safely. 

enter image description here

enter image description here

There are other related answers and information that may be of interest - this one for example. Also, the one-button ON-OFF switch is the basis for an improved design that you may find useful. Finally, there has been some discussion of this improved design implementation on my GitHub site - if you find that interesting, feel free to participate.

I'd suggest you review these answers to get a bit of background, and let us know if you have questions.

  • Hi Seamus, thank you for your answer. I was just wondering if my understanding of this circuit is correct. When the button is pressed the latching relay will connect the battery with the pi and when the pi is turned off the device tree overlay will send a signal as the last thing it will do (so safe turn off first finished). The signal sent by the GPIO will switch the relay to a position where the circuit is interrupted. Is that what is happening?
    – Qorzyking
    Commented Dec 17, 2021 at 9:51
  • 1
    @Qorzyking: Yes - That's very close, and thanks for asking. I've made an edit that explains the circuit operation in some detail. Also, I've realized that the schematic is confusing in that the latching relay has 2 sets of pin numbers on it :/ You can find a spec sheet for this latching relay at DigiKey & other distributors.
    – Seamus
    Commented Dec 18, 2021 at 8:56
  • 1
    Thank you for your help! got around to soldering the components and works like a charm!
    – Qorzyking
    Commented Jan 3, 2022 at 10:26

I only know the German Term for this: Selbsthaltefunktion The Switch S1 has to be a second relay, which is switched from the Pi. Then The PI will start-up with a press on S2, stay active untill the Pi activates the negotiated relay for S1.

Best regards, Bastian

  • Yeah, I was having a similar thought ; something involving a relay, there is also - another German word - a Stromstossschalter, which translates to [en.wikipedia.org/wiki/Latching_switch]. (I haven't tried that, so not sure it helps)
    – 0__
    Commented Dec 15, 2021 at 22:32
  • You can possibly solve this with pivoyager hat by Omzlo. I use it with a Pizero as a UPS, but you can have the pi start up by pressing the button on the ups board (the pivoyager) after which the pi can do its thing and shut down. The MCU of the pivoyager will then cut the power to the pi completely after a short delay. Pressing the button another time will start the pi up again.
    – ArjenR
    Commented Dec 16, 2021 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.