Some time ago I was searching for a circuit where the short push of a button switches on the power to the MCU, and the MCU was able to turn off the power itself.
Because I don't like waiting for the bootloader to power up the MCU to hold the power on.
It's hard to find such a circuit, so I've created this one for educational purposes.
It's similar to the "Mini Pushbutton Power Switch" produced by Pololu.
It's also possible to create such a circuit only with PNP and NPN transistors instead of MOSFETs. But this one uses 1 PMOS, 1 NMOS and 1 NPN transistor, because when I used my PNP/NPN transistors, they were only able to power an ATmega328, but not a Wemos D1.
Note that not all types of MOSFETs work. It probably only works with logic level MOSFETs. This circuit uses an IRL3803PBF as N-channel MOSFET and a NDP6020P as P-channel MOSFET. The BC337-25 is used as a NPN transistor (which shorts the NMOS gate to ground, just like the Off Button does, to switch off the power).
The code to switch off the power is very simple:
The Off Button and the power indicator LED are optional. Of course this circuit also works without an MCU connected to it.
The R8 resistor is also optional. I found that I'd have to push the button for like 500 milliseconds instead of 10 milliseconds for the circuit to keep powered, when that resistor is not in place.
|3||WEMOS D1 MINI||U1||WEMOS D1 MINI||1|