We often want to turn on a microcontroller (MCU)-based product or project with the press of a button. This is quite simple if the microprocessor is already running in the background. Most MCU families offer a range of low-power sleep modes with the ability to wake the processor on a button press. But what if we want to consume no power at all until the button is pressed?
We can resort to a circuit like that shown in Figure 1. Here the incoming power rail is switched by a P-Channel MOSFET, Q1 before it reaches the regulator. When power is first connected Q1 will be held off R1 and C1. Q2 will be held off by R2, as the GPIO1 pin on the MCU will be floating since it is as yet unpowered. Note that C1 is necessary to eliminate the risk that the circuit will latch on when power is applied, as there will be some stray capacitance from Q1s gate to the negative rail which could cause Q1 to momentarily switch on as it charges.
When the user presses the ON button, the gate of Q1 is brought negative with respect to the source and it turns on. Power is then applied to the MCU via the regulator. The software immediately configures GPIO1 as an output and sets it high. This turns the N-Channel MOSFET, Q2 on pulling Q1’s gate low latching the power on. This all happens in the few tens of milliseconds while the pushbutton is pressed so the power is latched on before the user releases the on-button.
This is great for turning the circuit on, but the only way to turn this circuit off is for the MCU to drive the GPIO1 output to zero, turning off both MOSFETs. We can make a couple of minor changes to this circuit so that we can detect if the button is pressed while the circuit is powered and initiate the turn-off that way. This is shown in Figure 2.
GPIO2 and two Schottky diodes are added. Power-on works as described above, except that the pushbutton pulls the gate of Q1 down via D1. The second GPIO2 is configured as a digital input with an internal pull-up. When the circuit is on, D1 is reverse biased and GPIO2 effectively monitors the state of the pushbutton. When it is pressed, the GPIO pin is pulled low via D2 and this state can be detected by the MCU which can then switch the circuit off via GPIO1 as before.
The advantage of switching off via software is that the user can determine how long the button must be pressed to qualify as a valid off signal and the program can complete any necessary housekeeping before powering down. You could, for example, require the button to be held down for three seconds to initiate power-down. The downside of this circuit is the need for two GPIO pins. Wouldn’t it be better if we could get away with just one?
Figure 3 shows how this can be achieved. This circuit turns on when the button pulls the gate of Q1 low, as per the previous circuits. This time however, we configure GPIO1 as an input with internal pull-up. This pull-up is sufficient latch the power on by turning Q2 on. Neat.
When we press the button while the power is on, the GPIO1 pin is pulled low via D2 and R3 which is detected by the MCU as before. The software must then switch the GPIO to an output and pull it low to turn Q2 and thus the rest of the circuit off. The inclusion of R3 is advisable to limit the current in the unlikely event that the GPIO is driven high before the user releases the button. If you are sure this will never happen Q3 could be eliminated.
There are dozens of variations on this theme out there—just be aware that not all of them are suitable for situations where the input voltage is higher than the MCU supply. In these circuits D2 ensures the GPIO2 input never sees a voltage higher than Vdd.
By. “Ditch The Switch: A Soft Latching Circuit Roundup.” Hackaday (blog), June 24, 2019. https://hackaday.com/2019/06/24/ditch-the-switch-a-soft-latching-circuit-roundup/.
Arshad, Areeba. “Soft Latch Switch Circuit.” Circuits DIY (blog), January 28, 2020. https://circuits-diy.com/soft-latch-switch-circuit/.