# Help powering an MCU when a switch changes state (with auto turnoff)

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
Hi!

Been trying to practice electronics after a long time, and found this tutorial https://randomnerdtutorials.com/lat...auto-power-off-circuit-esp32-esp8266-arduino/ on how to do a power latch which lets you turn off an MCU when its job is done.

So to practice a bit, I decided to make a similar circuit, with the difference that instead of activating the MCU when a switch closes, it should activate it when a switch changes state. The requirements I set for it are:
1. When the switch changes state, the circuit should transmit ~170mA and 3.3V to the MCU during around 1 second (so it has time to initialize, and set a GPIO to HIGH, which will hold the MCU on until the task is done).
2. When the MCU's task is done, it will set GPIO to LOW and it should turn off the MOSFET and the MCU should turn off (no power on its VCC pin). Please see the randomnerdtutorials link if this is not clear.
3. The MCU should receive ~170mA during startup (after startup it's okay to receive around 100mA).

This is the current circuit I have: https://tinyurl.com/ywvbywpn which has three problems:
a- Main problem: the circuit always transmits voltage to the MCU Vcc PIN (represented with a LED). I'm not sure what's the way to make the circuit stop transmitting current after ~1 second.
b- Despite the 5V input, it transmits only 2V to the MCU, how could I make it so there's less voltage drop? I guess the resistor on top of the switch should be changed?
c- When the switch changes to closed it takes a while to turn the circuit on. I guess this could be fixed by changing the capacitor next to the XOR?

Note that the 0V input on the left represents the GPIO of the MCU to reset it.

Any pointers will be appreciated, even if they are small easier circuits I can look at that would help me understand some basic concepts for this circuit. Specially when it comes to how to stop transmitting electricity to the MCU after X seconds, so it doesnt stay ON all the time.

I'm really newbie when it comes to this kind of circuits, and most of the tutorials I find online don't really explain the process of thought behind placing each component. Either they give you a full circuit or they are generic tutorials on how components work. That's why I decided to challenge myself to modify that circuit, with not much luck so far.

Thank you!

#### Ian0

Joined Aug 7, 2020
8,940
Where's the switch?
By the way, your circuit diagrams should come with a message that says "Warning: Contains flashing images"

#### BobTPH

Joined Jun 5, 2013
8,075
The MCU should receive ~170mA during startup (after startup it's okay to receive around 100mA).
You cannot control the current used by the MCU. You give it a constant voltage and it takes what current it needs when it needs it. And it is unlikely that an MCU alone ever takes 170mA. Are you talking about a chip, or a board that is powering additional chips?

Also, it is unnecessary to use an external timing circuit; the MCU can handle any timing itself.

And finally, what you are trying to do would normally be done by sleeping, rather than powering on and off. Is there a reason that would not work for you?

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
Where's the switch?
By the way, your circuit diagrams should come with a message that says "Warning: Contains flashing images"
I have updated the circuit so it's not running by default. https://tinyurl.com/ywpljj43 and you can see the switch on the bottom left.

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
You cannot control the current used by the MCU. You give it a constant voltage and it takes what current it needs when it needs it. And it is unlikely that an MCU alone ever takes 170mA. Are you talking about a chip, or a board that is powering additional chips?

Also, it is unnecessary to use an external timing circuit; the MCU can handle any timing itself.

And finally, what you are trying to do would normally be done by sleeping, rather than powering on and off. Is there a reason that would not work for you?
Right, that makes sense. I'm trying to power an ESP-01, which needs at least 170mA during initialization. Just want to make sure the circuit will be able to deal that much to it, sinc in the simulation with the LED I just see 120mA (can't simulate with that board because it's not in the simulation software). But I'd like to have a general circuit that I can modify to power other boards/MCUs too with the same mechanism, if that makes any sense.

About the timing, what I meant is, that in my current circuit the MOSFET is letting the current pass all the time. What I'd like is the MOSFET to let the current pass only when there's been a state change on the switch, and until the MCU has finished it's task (the GPIO would be LOW, set to HIGH on initialization and to LOW again).

And I could do sleeping, but this is more of a learning project, not for any specific project. And as far as I know powering an ESP8266 during several months, even using deep sleep is not effective, since it consumes quite a lot. Anyways, the idea is to see how other people would approach this issue rather than making any specific product, so I can learn a bit about it.

#### geekoftheweek

Joined Oct 6, 2013
1,093
I did something similar to your circuit at one time with a few exceptions. On the gate of the PFET I had the switch pull it low to initially "turn it on" then used a pin off the micro to drive another NFET to pull the PFET gate low. As long as you hold the switch for say at least a half a second the system will stay running. After five minutes of inactivity it will turn itself off.

Edit:

Nevermind... I realized you are not doing the same thing at all I did.

Last edited:

#### Ian0

Joined Aug 7, 2020
8,940
When you say "switch changes state" do you want it to activate when the switch opens AND when it closes?

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
I did something similar to your circuit at one time with a few exceptions. On the gate of the PFET I had the switch pull it low to initially "turn it on" then used a pin off the micro to drive another NFET to pull the PFET gate low. As long as you hold the switch for say at least a half a second the system will stay running. After five minutes of inactivity it will turn itself off.

Edit:

Nevermind... I realized you are not doing the same thing at all I did.
I'm not sure I follow, do you have a diagram of that? If the PFET is always pulled to low, it will always be on, no? How did you manage that? And how did you set up the 2 XOR so they don't always set the NPN transistor base to high?

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
When you say "switch changes state" do you want it to activate when the switch opens AND when it closes?
Yes, I'd like to get a few seconds of current to initialize the MCU, set the GPIO pin to HIGH (as a safeguard to maintain the current flowing), and when the MCU finishes its task, it would set the GPIO to low and the MOSFET should stop transmitting current.

#### Ian0

Joined Aug 7, 2020
8,940
When the switch changes state, the 4077 outputs a negative going pulse that lasts about 7ms (0.7 R3 C1) which takes the regulator out of shutdown. When the processor wakes up it should first take the right hand end of R4 to 3.3V which will keep the regulator enabled.
The regulator outputs 3.3V. (it is the 3.3V version of the LP2951)
When the task is finished, R4 should go back to 0V which will put the regulator back into shutdown.
(Similar to @geekoftheweek 's suggestion)

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
View attachment 302935When the switch changes state, the 4077 outputs a negative going pulse that lasts about 7ms (0.7 R3 C1) which takes the regulator out of shutdown. When the processor wakes up it should first take the right hand end of R4 to 3.3V which will keep the regulator enabled.
The regulator outputs 3.3V. (it is the 3.3V version of the LP2951)
When the task is finished, R4 should go back to 0V which will put the regulator back into shutdown.
(Similar to @geekoftheweek 's suggestion)
Thank you for the explanation!

Just a few questions. The OUT of the LP2951 would go to the MCU, correct?

So the flow would be, switch closes, the C1 capacitor would charge and when done the XNOR would send a small pulse to SH pin in the voltage regulator. The voltage regulator would get active and forward to the MCU (connected to OUT pin).

The MCU would turn on, set the pin connected to R4 to high and maintain it until its task is done. When done, it would set the R4 pin to LOW, but not sure how this would make the MCU turn off?

Then when the switch opens, the C1 capacitor discharges and sends a pulse to SH pin, which would trigger the MCU to turn on, set the R4 connected-pin to HIGH.

What is the purpose of the C2 capacitor? How would it affect the circuit?

How do you calculate the length of the pulse?

Isn't having the IN always connected to V1 gonna be draining the battery non-stop, or this is really not important since it's IQ is 0.075mA? How could I calculate the battery life, for example if I have two AA batteries giving me 2500mAh. My guess is to add up all the IQ of the components and calculate with something like: https://www.digikey.es/en/resources/conversion-calculators/conversion-calculator-battery-life (+ predict how many times the MCU will be running and so on).

Is this circuit possible without any premade components (meaning, basic transistors, diodes,...) inestead of using a voltage regulator?

And sorry for so many questions, just really interested to understand how these circuits work, without taking assumptions!

#### Ian0

Joined Aug 7, 2020
8,940
Thank you for the explanation!

Just a few questions. The OUT of the LP2951 would go to the MCU, correct?
Yes - it provides the 3.3V supply (other voltages of LP2951 are available)
So the flow would be, switch closes, the C1 capacitor would charge and when done the XNOR would send a small pulse to SH pin in the voltage regulator. The voltage regulator would get active and forward to the MCU (connected to OUT pin).

The MCU would turn on, set the pin connected to R4 to high and maintain it until its task is done. When done, it would set the R4 pin to LOW, but not sure how this would make the MCU turn off?

Then when the switch opens, the C1 capacitor discharges and sends a pulse to SH pin, which would trigger the MCU to turn on, set the R4 connected-pin to HIGH.
Almost. When the switch is pressed, the two inputs to the ex-nor gate are different, so its output goes low. The second input is delayed by the RC network, and catches up with the first input after about 0.7RC seconds.≈
What is the purpose of the C2 capacitor? How would it affect the circuit?
It is the output capacitor for the IC (keeps it stable). I think the minimum may be as low as 100nF but I always use 10uF.
How do you calculate the length of the pulse?
≈0.7RC, but can vary by 20%
Isn't having the IN always connected to V1 gonna be draining the battery non-stop, or this is really not important since it's IQ is 0.075mA? How could I calculate the battery life, for example if I have two AA batteries giving me 2500mAh. My guess is to add up all the IQ of the components and calculate with something like: https://www.digikey.es/en/resources/conversion-calculators/conversion-calculator-battery-life (+ predict how many times the MCU will be running and so on).
Typically 30uA, but if you need better than that, other regulators with shutdown inputs are available.
Is this circuit possible without any premade components (meaning, basic transistors, diodes,...) inestead of using a voltage regulator?

And sorry for so many questions, just really interested to understand how these circuits work, without taking assumptions!
Yes, but it would probably be tricky to beat the quiescent current of a CMOS gate and a micropower regulator.
You can make a precision regulator out of half a dozen transistors and a low power voltage reference, but there aren't many references that work with <30uA.

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
Yes - it provides the 3.3V supply (other voltages of LP2951 are available)

Almost. When the switch is pressed, the two inputs to the ex-nor gate are different, so its output goes low. The second input is delayed by the RC network, and catches up with the first input after about 0.7RC seconds.≈

It is the output capacitor for the IC (keeps it stable). I think the minimum may be as low as 100nF but I always use 10uF.

≈0.7RC, but can vary by 20%

Typically 30uA, but if you need better than that, other regulators with shutdown inputs are available.

Yes, but it would probably be tricky to beat the quiescent current of a CMOS gate and a micropower regulator.
You can make a precision regulator out of half a dozen transistors and a low power voltage reference, but there aren't many references that work with <30uA.
This is great help! I might buy the components and experiment myself then!

Just two more questions and I let you go on with your live

1- How does setting the R4 connected PIN to low gonna turn off the MCU? Wouldn't that trigger a pulse to SH when the switch has been closed for a while? Meaning that the MCU would not turn off?

2- For the 0.7RC formula, is it always 0.7? How did you get this value?

#### Ian0

Joined Aug 7, 2020
8,940
This is great help! I might buy the components and experiment myself then!

Just two more questions and I let you go on with your live

1- How does setting the R4 connected PIN to low gonna turn off the MCU? Wouldn't that trigger a pulse to SH when the switch has been closed for a while? Meaning that the MCU would not turn off?
turns off the transistor, which allows the 4077 to pull the SHUTDOWN pin high.

When the switch has been closed for a while, both 4077 inputs will be at 0V so the output will be high.
2- For the 0.7RC formula, is it always 0.7? How did you get this value?
V=V0.(1-e^(-t/RC))
Assume that the threshold is about 0.5Vcc so
V/V0=0.5
0.5=(1-e^(-t/RC))
e^(-t/RC)=0.5
-t/RC=log0.5
t/RC=-log0.5
log0.5=-0.698

#### geekoftheweek

Joined Oct 6, 2013
1,093
I'm not sure I follow, do you have a diagram of that? If the PFET is always pulled to low, it will always be on, no? How did you manage that? And how did you set up the 2 XOR so they don't always set the NPN transistor base to high?
My apologies. I was stretching the "similarity" between my project and yours way beyond belief. I simply used a momentary switch to "start" the board. After that the switch was no longer readable as the micro pulled the gate low which also was the same as holding the switch. Instead of reading the switch to turn it off my project either turned off due to inactivity or a message through a WiFi socket.

I missed the detail of actually being able to use the switch after the initial power on which makes my comment totally useless for your project.

#### Ian0

Joined Aug 7, 2020
8,940
My apologies. I was stretching the "similarity" between my project and yours way beyond belief. I simply used a momentary switch to "start" the board. After that the switch was no longer readable as the micro pulled the gate low which also was the same as holding the switch. Instead of reading the switch to turn it off my project either turned off due to inactivity or a message through a WiFi socket.

I missed the detail of actually being able to use the switch after the initial power on which makes my comment totally useless for your project.
Actually, it's closer than you think. The output from his 4077 is the same as the output from your momentary switch.

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
turns off the transistor, which allows the 4077 to pull the SHUTDOWN pin high.

When the switch has been closed for a while, both 4077 inputs will be at 0V so the output will be high.

V=V0.(1-e^(-t/RC))
Assume that the threshold is about 0.5Vcc so
V/V0=0.5
0.5=(1-e^(-t/RC))
e^(-t/RC)=0.5
-t/RC=log0.5
t/RC=-log0.5
log0.5=-0.698
Thank you very much! These kind of info helps a lot to understand these circuits when starting out.

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
View attachment 302935When the switch changes state, the 4077 outputs a negative going pulse that lasts about 7ms (0.7 R3 C1) which takes the regulator out of shutdown. When the processor wakes up it should first take the right hand end of R4 to 3.3V which will keep the regulator enabled.
The regulator outputs 3.3V. (it is the 3.3V version of the LP2951)
When the task is finished, R4 should go back to 0V which will put the regulator back into shutdown.
(Similar to @geekoftheweek 's suggestion)
About the LP2951, I've been looking which one to buy, but in the specs I always see it says: Curent Output 100mA, does this mean it never goes over 100mA, or that the min output will be 100mA?
For example in this one https://www.digikey.es/en/products/detail/texas-instruments/LP2951DR/1739779. Should I look for another one, or this one would do? (Note the ESP-01 needs ~170mA for initialization)

#### Ian0

Joined Aug 7, 2020
8,940
About the LP2951, I've been looking which one to buy, but in the specs I always see it says: Curent Output 100mA, does this mean it never goes over 100mA, or that the min output will be 100mA?
For example in this one https://www.digikey.es/en/products/detail/texas-instruments/LP2951DR/1739779. Should I look for another one, or this one would do? (Note the ESP-01 needs ~170mA for initialization)
You probably need something with more output current. LP2951 was an example. There are lots of 3.3V regulators with enable inputs out there. The higher the rating, the larger the quiescent current is likely to be, so you might have to read rather a lot of datasheets! Also, what the ESP-01 datasheet doesn't tell you is how long it needs 170mA for initialisation.

Thread Starter

#### lpares12

Joined Sep 6, 2023
15
You probably need something with more output current. LP2951 was an example. There are lots of 3.3V regulators with enable inputs out there. The higher the rating, the larger the quiescent current is likely to be, so you might have to read rather a lot of datasheets! Also, what the ESP-01 datasheet doesn't tell you is how long it needs 170mA for initialisation.
Great, I'll look for this then.

One more question, imagine I were to get this XNOR https://www.lcsc.com/product-detail/Gates_Texas-Instruments-CD4077BM96_C86563.html which has a quiscent current of 1uA, so on first sight it looks really good. But then I realized one thing, the source current is 3.4mA. When the switch is closed, both inputs of the logical gate will be receiving a HIGH, so output will be HIGH too. Does this mean that when the switch is closed the gate is gonna be using 3.4mA instead of the Quiscent current?