Need GPIO pin to briefly stay high after switch is released

Thread Starter

mmcginty

Joined Feb 4, 2010
57
MCU: ESP-32

My device has an input to place it in configuration mode, a momentary switch connects VCC to an IO pin, which triggers an interrupt when high. The ISR queues a task that sets a flag in NV mem and does a software reset. When it boots it checks for the flag in NV mem, if set it clears it and enters "air kiss" mode. All good.

But I can't include that IO pin as one of the pins that can wake it from a deep sleep, because the only pin I'm using like that is connected to a hall effect flow sensor, the sensor can come to rest in either logic level high or low, and I have to read it before sleeping to program the wake-up interrupt for the opposite level. As such if the device is sleeping when the user wants to configure, she must first press the reset button, wait half a second, then press the config button.

The momentary switch I'm using is DPDT, so I have an extra set of contacts to trigger a reset with the same press, but the ISR doesn't get hooked up until a split second after reset is released. I need the config pin to stay high just a tiny bit longer than the button is held.

Years ago, in a 12V analog circuit I kept a relay energized almost a second after its power was cut, by putting a 1000 uf cap and a 1K resistor in parallel with the relay coil. Is there something similar I can do for this 3.3V digital GPIO?

TIA
 

Irving

Joined Jan 30, 2016
3,885
Yes, a capacitor /resistor combo, or something more active could do this. Can you show the part of the circuit you're referring to so we can understand the physicality better before offering a definitive solution.

However, if your 2nd contact was to connect a capacitor to vcc via a resistor when pressed and that capacitor connected to your input pin via a resistor, it would hold up for a while.

You'd probably want to reconfigure that pin as an output and take it low to discharge the C before reverting it to input and reenabling the ISR .
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
You might want to use a small mosfet (e.g., 2N7000 or 2N7002) as a switch between the pin and device. Put the RC delay on the gate. That way, you can keep the capacitor size quite a bit smaller than 1000 uF (e.g., 10 uF and 100k = 1 second R*C).
 

peterdeco

Joined Oct 8, 2019
484
Can't you do this in software? In basic, the command would be something like this:
if gpio.x = 1 then high gpio.x 'if button pushed convert to output high
pause 100 'stay high for 100ms
input gpio.x 'return to input
 

Irving

Joined Jan 30, 2016
3,885
You might want to use a small mosfet (e.g., 2N7000 or 2N7002) as a switch between the pin and device. Put the RC delay on the gate. That way, you can keep the capacitor size quite a bit smaller than 1000 uF (e.g., 10 uF and 100k = 1 second R*C).
1000uf was OPs solenoid example.

Here he needs maybe 10mS, so 10k to vcc to charge then i/p current is 1uA so
C = iT/V =1 x .01/3uF = 3.3nF.

Alternately

t ~ .7CR,
R input = 3M so C = .01/(.7 x 3)uF = 4.7nF

So use 10nF will give 30mS approx..
100nF, 300mS..
 

Thread Starter

mmcginty

Joined Feb 4, 2010
57
1000uf was OPs solenoid example.

Here he needs maybe 10mS, so 10k to vcc to charge then i/p current is 1uA so
C = iT/V =1 x .01/3uF = 3.3nF.

Alternately

t ~ .7CR,
R input = 3M so C = .01/(.7 x 3)uF = 4.7nF

So use 10nF will give 30mS approx..
100nF, 300mS..
Awesome, this is exactly what I needed, thank you very much...

So, like this?
SCH2.png

Or this (seems to make more sense)?
SCH1.png


Thanks again!
 

Thread Starter

mmcginty

Joined Feb 4, 2010
57
Can't you do this in software? In basic, the command would be something like this:
if gpio.x = 1 then high gpio.x 'if button pushed convert to output high
pause 100 'stay high for 100ms
input gpio.x 'return to input
The pin state has to survive an MCU reset, so no I don't think so.
 

Thread Starter

mmcginty

Joined Feb 4, 2010
57
You might want to use a small mosfet (e.g., 2N7000 or 2N7002) as a switch between the pin and device. Put the RC delay on the gate. That way, you can keep the capacitor size quite a bit smaller than 1000 uF (e.g., 10 uF and 100k = 1 second R*C).
The 1000 uF cap would take 1/4 of my board! That was just a reference to something similar I did many years ago.
 

Thread Starter

mmcginty

Joined Feb 4, 2010
57
Yes, a capacitor /resistor combo, or something more active could do this. Can you show the part of the circuit you're referring to so we can understand the physicality better before offering a definitive solution.

However, if your 2nd contact was to connect a capacitor to vcc via a resistor when pressed and that capacitor connected to your input pin via a resistor, it would hold up for a while.

You'd probably want to reconfigure that pin as an output and take it low to discharge the C before reverting it to input and reenabling the ISR .
Good point about discharging it, the pin gets set low during reset so that should take care of itself, but setting it low in software before couldn't hurt anything. I will also dump the level of the pin to the console before resetting, just to be sure.

Thanks!
 

jpanhalt

Joined Jan 18, 2008
11,087
The input impedance of the pin itself is probably high enough that you can toss that resistor. And if that is not the case, the resistor does no good.
 

Irving

Joined Jan 30, 2016
3,885
Awesome, this is exactly what I needed, thank you very much...

So, like this?
View attachment 212084

Or this (seems to make more sense)?
View attachment 212085


Thanks again!
Close, but what I had in mind was...

1594674113268.png

When the reset button is pressed the capacitor is charged through the 10k to 3.3v. After reset the voltage is held on the capacitor (needs to be a reasonable quality, but experiment to see how long it stays up), and can be read by the GPIO. Once the interrupt is recognised the GPIO is turned to an output and pulled low to discharge the capacitor. The 1k is there to protect the output transistor when discharging. Without it the current into the GPIO port could be around 1A.
 

Thread Starter

mmcginty

Joined Feb 4, 2010
57
Close, but what I had in mind was...

View attachment 212095

When the reset button is pressed the capacitor is charged through the 10k to 3.3v. After reset the voltage is held on the capacitor (needs to be a reasonable quality, but experiment to see how long it stays up), and can be read by the GPIO. Once the interrupt is recognised the GPIO is turned to an output and pulled low to discharge the capacitor. The 1k is there to protect the output transistor when discharging. Without it the current into the GPIO port could be around 1A.
Great, thanks yet again! Had to order the cap from Digikey, I'll throw it on the breadboard and try it out when the mail comes.
 

Thread Starter

mmcginty

Joined Feb 4, 2010
57
Great, thanks yet again! Had to order the cap from Digikey, I'll throw it on the breadboard and try it out when the mail comes.
I probably did something wrong, maybe stray capacitance, maybe a marginal switch, hard to say. Another factor is, the reset circuit (that I copied from an Expressif reference design) actually pulls the power rail down, not just the enable pin, so there is no 3V3 while the reset button is held. (but I tried it without resetting, and no ISR, just dumping the value of the pin in a loop, and still got inconsistent results.)

So after some frustration I ended up solving the problem in firmware after all. After a hardware reset I made it go into config mode for one minute, then if no airkiss activity it boots into its usual code. So the user can always get into config instantly with one button press. It'll take a little longer to reset if need be, but not that much (hopefully the user won't ever have to do that anyway.)

Thanks to all for your time and assistance, I learned a few things, so not a waste at all. :)
 

Irving

Joined Jan 30, 2016
3,885
the reset circuit (that I copied from an Expressif reference design) actually pulls the power rail down, not just the enable pin, so there is no 3V3 while the reset button is held
I find that very surprising as my reset circuits, also based on Expressif reference info put a 10k to +3.3v and 1uF to ground on EN for an ~20mS reset pulse. I've never heard of/seen grounding a supply rail for reset, that makes no sense!?

Can't you change that?
 
Top