I have an arduino that is powered by a 9v block battery and enabled by a switch. I want to change the curcuit, so that when the switch is turned off the arduino remains powered until certain actions are finished, and then turn off.
However I dont just want the arduino to sleep, the power should be disconnected. This is important, because I have additional components draining power from the 5v pin (around 300mA) and I want those to shut down too.
My first idea was to use a relay in parallel to the switch. When the switch is turned on, the arduino turns the relay on too. When the switch is turned off, the arduino is still powered via the relay. The arduino detects when the switch is turned off (the switch is double pole, so i can detect its state with a digital input pin) and powers off the relay when the remaining actions are finished.
Is there a better way than using a relay? Can I use a mosfet or something else instead of the relay?
And maybe, in the future, I will need the 9V for additional components, which should turn off too when the arduino decides to shut down. But when the relay/mosfet/..? is placed between battery and arduino VIN I can just connect those between relay/mosfet/..? and the arduino. So this should be simple.
The motor is just a placeholder for my additional components that drain power from the battery and should be shut down too.