Button to temporarily turn on Arduino

Hello, I know it has been discussed a lot but I would still like to ask how to do this:

  1. Initially the battery is disconnected from Arduino.
  2. When I press button, Arduino wakes up and runs the code.
  3. At the end of the code Arduino "powers off" the button and again battery is disconnected.

I am not sure but maybe there is somekind on electro-magnet that I could use. When I push the momentary button, the electromagnet magnetizes and switches on power using reed-switch. After Arduino turns some GPIO to LOW state, the electromagnet loses magnetism and reed-switch disconnects battery.

  1. Have the button supply power to the Arduino to turn it on.
  2. Have the sketch turn on a relay that also supplies power to the Arduino to keep it on.
  3. When the sketch has done what you need have it turn off the relay to turn off the Arduino

Thanks for the idea!

Not great for a battery project, although certainly intuitive to use.

A p-channel MOSFET is the way it's often done combined with a smaller n-channel MOSFET that helps to act as a latch.

See the 'best answer' here and ignore the MOSFET parts used. They're silly. Substitute for more sensible ones. The topology is OK though.