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