Detect power off switch with MCU then shut down - Page 1
Forum section: electronics / beginners

author: ratatax
author_rank: Regular Contributor
timestamp: February 01, 2024, 01:02:19 am
content: Hey all,For a project I want the MCU to detect when the user wants to shut down the system, perform some actions, then power off. With a simple SPDT switchI can't figure how to do it. The mosfet is by default OFF with the resistor, pushing the switch turns the mosfet ON, then the MCU drives KEEPALIVE to LOW to prevent the user from shutting it down. Problem : I cannot know the switch position anymore as soon as I use KEEPALIVE to override it. I have to know its physical position to decide what to do, while still maintaining the system active before shutting it down.I once designed something similar successfully with a DPDT switch, it was easy because one pole was used for position detection, the other pole for maintaining/disabling power. But I can't figure it out with an SPDT switch. Any ideas?

author: ataradov
author_rank: Super Contributor
timestamp: February 01, 2024, 01:20:19 am
content: Here is a circuit I've used with a momentary push buttonhttps://github.com/ataradov/siggen/blob/master/hw/siggen.pdf

author: ratatax
author_rank: Regular Contributor
timestamp: February 01, 2024, 01:46:22 am
content: Thanks ! This should work? Using your double schottky diode arrangement, I think that was the critical block I was missing !

author: ataradov
author_rank: Super Contributor
timestamp: February 01, 2024, 01:50:39 am
content: The idea is correct.I don't remember exactly why I needed the second transistor, but I think it was necessary. I think the keepalive signal from the MCU pin was constantly pulling the signal low when MCU was not powered or something like that.Definitely prototype before fully committing. Or add an option for the second transistor.

author: ratatax
author_rank: Regular Contributor
timestamp: February 01, 2024, 02:03:08 am
content: Yes, also I'm afraid that the diode in series with the gate prevent it from fully reaching GND, I will try this circuit tomorrow

author: pqass
author_rank: Super Contributor
timestamp: February 01, 2024, 03:12:41 am
content: I think the attached circuit would work with just one GPIO pin.The momentary switch can provide the initial power-on. Then the GPIO pin would output a HIGH to keep it alive. Periodically, the the pin can switch to input mode to read the switch position while the cap keeps power on long enough to switch back to output mode and maintain the keep alive signal. To power off, just output a LOW (or set the pin to input mode) to let the cap run down. If the switch is enabled while the GPIO outputs a HIGH, no damage will result (due to the 1K). Also, GPIO wins if it outputs a LOW (well, at least until it reboots if the user lays on the switch).Play with thesimulation here.

author: Ian.M
author_rank: Super Contributor
timestamp: February 01, 2024, 03:26:52 am
content: Also see:https://www.eevblog.com/forum/beginners/push-button-power-circuit-for-arduino-trying-to-modify-it-for-12v-input/for some sample circuits and ideas. If you need the circuits found there to work down to single LiPO or 3.3V logic levels, you'll need to substitute some components e.g. pick N and P MOSFETs with low enough threshold voltage.I presented a single GPIO soft power switch in reply#6of that topic. It relies on the GPIO pin having a switchable weak pullup to stay on once active, which means you don't have to swap pin modes to poll the switch.There's also a video testing and comparing the circuits from that topic:

author: Peabody
author_rank: Super Contributor
timestamp: February 01, 2024, 04:07:24 am
content: The circuit in reply #2 probably won't work. When the power is off, the keepAlive pin will go to ground through its protection diode to Vcc (which will drop to near ground). So the power will remain on.Ian.M's single pin circuit is the one you want. It will work if the input voltage is above Vcc, and the button can be used for other input options in addition to turning off the power.

author: Ian.M
author_rank: Super Contributor
timestamp: February 01, 2024, 04:19:36 am
content: Actually, with appropriate low gate threshold voltage MOSFETs, it will also work with Vbatt lower than Vcc, switching power to a boost converter to feed the MCU.

author: Peabody
author_rank: Super Contributor
timestamp: February 01, 2024, 04:37:03 am
content: Yes, and isn't there a popular dual mosfet chip with one P-channel and one N-channel? I think Ralph Bacon has one of those that he likes. Or for the N-channel you can just use a lowly 2N7000. All you need is a low enough threshold. RDSon and current don't matter.

author: Ian.M
author_rank: Super Contributor
timestamp: February 01, 2024, 05:43:33 am
content: A 2N7000 will usually work, so OK for a prototype, but as its Vgs_th (max.) is 3.0V, if you are unlucky it will be marginal with 3.3V logic, and fail if the 3.3V rail droops or the weak pullup is weaker than expected. A 2N7002 is better with 2.5V Vgs_th (max.), but ideally you should pick a N-MOSFET with a threshold voltage of no more than half the logic level, and similarly below half the supply voltage for the P-MOSFET.The problem with single package dual complimentary MOSFETs is ideally, you need a small signal N-MOSFET, with low enough gate capacitance to switch on quickly due to the pullup current when the button is released, but also a power P-MOSFET to handle the load current. No doubt there is at least one out there as use as a level shifter and high side switch is common, but good luck finding one with decent availability at a price that makes it worth it vs two discrete 'jellybean' parts. When you do find it, do a lifetime buy as the chances of it going obsolete are high. For lower load currents, and with the P-MOSFET 'slugged' with enough extra gate capacitance to slow its turnoff, single package near-matched complimentary pairs can be made to work, but as the load current increases, you'll probably run into transient SOA violations during P-MOSFET turnoff.

author: Peabody
author_rank: Super Contributor
timestamp: February 01, 2024, 03:36:44 pm
content: Well, I think you'd really have to be unlucky for the 2N7000 not to work at 3.3V. The maximum current it has to pass is less than a milliamp. I don't see why the threshold voltage would need to be one-half of Vcc. It's only going to be grounding the gate of the N-channel, not passing any real current. And the 2N7000 comes in TO92. :-)Edit: The dual mosfet I was trying to remember is the SI4599.

author: Ian.M
author_rank: Super Contributor
timestamp: February 01, 2024, 05:35:55 pm
content: Yes, very very unlucky, and certainly below the hobbyist failure noise floor of ESD damage and fake parts. However for production, if there is a significant chance of a reel of parts skirting their tolerance limit, you have to consider the long odds to avoid the expensive risk of assembling a batch that just scrapes through factory test but fails in the field, e.g. consider the case of Vcc 5% low + a weak pullup equivalent of 50K, which forms a potential divider with the 1Meg N-MOSFET gate pulldown. The result is less than 3.0V gate drive, and as the gate threshold voltage rises in cold conditions, (approx 3% from nominal at 0° C) it would be risky to use a 2N7000 here for a mass-produced product for use outdoors.