Best approach for button functionality
Best approach for button functionality
It is a very simple question and maybe very novice however I would like to know the simplest and nicest method of implementing a simple on/off button functionality on ESP32.
I am guessing with internal pulldown(?) but what about filtering just like the pulse counter has?
Thanks for advices
I am guessing with internal pulldown(?) but what about filtering just like the pulse counter has?
Thanks for advices
Re: Best approach for button functionality
There's an example here . . . .
https://www.arduino.cc/en/Tutorial/Debounce
. . . . . but use internal resistor.
If you want to switch on/off the power to your ESP32 consider purchasing a push button switch with 'push on push off' action (sometimes called 'maintained or 'latching' action).
https://www.arduino.cc/en/Tutorial/Debounce
. . . . . but use internal resistor.
If you want to switch on/off the power to your ESP32 consider purchasing a push button switch with 'push on push off' action (sometimes called 'maintained or 'latching' action).
Re: Best approach for button functionality
That example is strictly for Arduino and it doesn't filter the spikes.
I found interesting info here
https://forum.arduino.cc/index.php?topic=62065.0
I wonder if anyone used a schottky diode to eliminate any unpleasant effects which are basically related to boosting voltage.
3.3V switch shouldn't pose serious risks but I am not really sure. The button I plan to use is one on rotary encoder that bounces contact off after releasing. I will have to check on the scope what exactly is going on during button scenarios
I found interesting info here
https://forum.arduino.cc/index.php?topic=62065.0
I wonder if anyone used a schottky diode to eliminate any unpleasant effects which are basically related to boosting voltage.
3.3V switch shouldn't pose serious risks but I am not really sure. The button I plan to use is one on rotary encoder that bounces contact off after releasing. I will have to check on the scope what exactly is going on during button scenarios
Re: Best approach for button functionality
Yes, it's Arduino code but you shouldn't have any trouble reading it to understand what it does and why it does it. It does "filter" contact bounces. As it stands, it determines when there have been no contact bounces for at least 50ms.
Re: Best approach for button functionality
It is good code however it does not filter bounces but just delays the response until bouncing ends.
It is not the best way to handle button press. I need to get rid of physical voltage spikes and noise instead of skipping them.
Also I'd rather use interrupt than wait loop. Just was wondering if esp had built in filtering particularly for buttons.
It is not the best way to handle button press. I need to get rid of physical voltage spikes and noise instead of skipping them.
Also I'd rather use interrupt than wait loop. Just was wondering if esp had built in filtering particularly for buttons.
Re: Best approach for button functionality
There is nothing wrong with using delays, there really is no need to add hardware to filter the bounce, when you can do it all in software just the same. That snipped of Arduino code is far more than it needs to be.
Also, one needs to be careful when using interrupts on buttons with no filtering. You can get many IRQ events very rapidly.
Just keep it simple.
Also, one needs to be careful when using interrupts on buttons with no filtering. You can get many IRQ events very rapidly.
Just keep it simple.
Code: Select all
loop()
{
if(button is low)
{
// typical debounce time
delay_ms(20);
// Lets check again if still low
if(button is low)
{
Lets do something about it.
// Stay here if the thing we need to do is would be done before the user can let go of the button.
// So we dont re-trigger thinking its another press.
while(button is low);
}
}
}
Re: Best approach for button functionality
What if I do following (from gpio interrupt) :
Pseudocode on interrupt triggered
-disable interrupt
-wait until gpio low
-delay some time
-set button flags,variables,state
-enable interrupt
Pseudocode on interrupt triggered
-disable interrupt
-wait until gpio low
-delay some time
-set button flags,variables,state
-enable interrupt
Re: Best approach for button functionality
There are many ways to skin a cat, but the way I skin mine when using a interrupt for this situation is this.
Pin changes state and jumps to ISR. The first thing is disable interrupt. Set a variable to true, then clear IRQ bit to be ready for next IRQ, and exit ISR.
Depending on how fast I need to check this button I will either create a new task for it or just just check in my main loop.
If the variable is true, I will do what I need to, then clear the variable, read the pin and wait until it goes back to the idle state and then re-enable the interrupt.
Pin changes state and jumps to ISR. The first thing is disable interrupt. Set a variable to true, then clear IRQ bit to be ready for next IRQ, and exit ISR.
Depending on how fast I need to check this button I will either create a new task for it or just just check in my main loop.
If the variable is true, I will do what I need to, then clear the variable, read the pin and wait until it goes back to the idle state and then re-enable the interrupt.
Re: Best approach for button functionality
How do I clear IRQ bit? Isn't disabling the interrupt the same?
Re: Best approach for button functionality
I have not dug into the docs on the ESP32 to answer that. Typically there is a register and bit in that register enable / disable and configure things like interrupt on going high, going low, or both. Gotta look into the docs to find out.
Who is online
Users browsing this forum: No registered users and 95 guests