How to detect that a voltage source is not powered?
Replace SW1 with a two-pole switch. Use one pole to switch your circuit on and off, and connect the second pole to the GPIO and ground of your microcontroller. You may need to add a pull-up resistor to the GPIO pin if your microcontroller doesn't have one built-in.







simulate this circuit &ndash; Schematic created using CircuitLab