# Implementing power on/off through gpio pins for mcu while asserting for few seconds

I'd like to know how can a power-on/power-off system for a battery operated device with GPIO inputs can be designed that has the following features:

• When MCU is off, pressing and holding down a switch for 4 seconds should only turn it on
• This switch must be usable as a GPIO input pin to the mcu after it turns on
• Pressing and holding again the same switch for 4 seconds should turn the system off by either cutting off the power supply or controlling a Shutdown signal to the regulator.

The last part can be easily done by having a timer run in the mcu and check the duration of press everytime there is an interrupt generated, and then asserting the shutdown signal of the voltage regulator when ready. But I'm not getting ideas for 4 second turn on, and at the same time be usable as a GPIO after power on. Subsequent short press on this switch should not turn off the system. Any thoughts ?

• Wouldn't it be smarter to put the MCU in deep sleep mode instead? – Lundin Jan 17 '18 at 12:55
• @Lundin Depending on the MCU sleep current, the battery size you can afford to use and the operating life you need, I can easily understand it may not be feasible. Sometimes, 10µA is too much. – dim Jan 20 '18 at 9:36
• @dim A 10k pull-down on 5V draws 500uA... – Lundin Jan 22 '18 at 7:39

You could do it with a few passives, a flip-flop and a schmitt trigger, something like that:

When the switch is closed, the capacitor slowly charges. When the high-level transition input voltage of the schmitt trigger is reached (here at ~4s), the schmitt trigger buffer (you can use SN74xx1G17) output goes high, triggering the clock of the flip-flop (you can use SN74xx1G74), which then toggle its output. Note the D flip flop is configured with the inverting output connected to its D input, so it changes state at each clock rising edge.

When the button is released, the capacitor discharges (it takes a bit more time than the charge time due to R2) and you can then press again to toggle the output off.

To sum up, here, each time you press the button for longer than 4s, the "PWREN OUT" output will toggle (this can directly drive the enable input of a regulator), and you'll still be able to detect shorter button presses from the MCU with the "SW OUT" output.

The advantage of this solution is that it doesn't involve the software at all. So if there is a bug somewhere and the MCU hangs, you can still shut it down.

Be aware, however, that if the MCU is powered off, applying a voltage on the GPIO ("SW OUT" being high because the button is pressed) may damage it. Check the MCU ratings in the datasheet. To solve this, you can put a high-valued (100k or so) resistor in series with the "SW OUT" line (and eventually a diode that will clamp any voltage higher than the MCU supply voltage, but the MCU most likely has such diodes builtin).

• Thank you very much for the answer. But PWREN OUT is not guaranteed to be high (or Low) since the initial state of flip flop is unknown ? So isnt there a 50% possibility that as soon as the battery is connected, the regulator shall be turned on, if PWREN OUT is connected directly to the regulator's EN input ? – Jay Aurabind Jan 20 '18 at 9:20
• @Jay Potentially, yes. Fortunately, the SN74xx1G74 also has a reset input (not shown on the schematic), on which you can put a capacitor-resistor network to ensure a reset on power-up. But the values will depend on the time your supply takes to reach inital voltage. See electronics.stackexchange.com/questions/143968/… – dim Jan 20 '18 at 9:26