Single interrupt trigger performing callback function several times.

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
HexVitor
Posts: 6
Joined: Sat Dec 28, 2019 2:11 am
Location: Brazil

Single interrupt trigger performing callback function several times.

Post by HexVitor » Sun Jul 05, 2020 3:06 pm

Hi, I'm having problems with an ESP32-based project due to the strange behavior of the interruption on some pins.
When configuring the interrupt according to the code below, a single actuation on pin 23 has called the callback function more than 1 time, even without other activations on the pin.
This has happened with several pins. How do I execute the interruption only once per trigger?

MCU: ESP-WROOM-32 (ESP32D0WDQ6)
Firmware: MicroPython v1.12 (for ESP32 with IDF4, 2019.12.20) https://micropython.org/resources/firmw ... -v1.12.bin
IDE: Thonny Python IDE
OS: Windows 10 Home Single Language 2004

Code: Select all

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)
button = Pin(23, Pin.IN, Pin.PULL_DOWN)

def callback(pin):
    led(1)
    sleep(1)
    led(0)
    sleep(1)
    
button.irq(trigger=Pin.IRQ_RISING, handler=callback)

User avatar
Roberthh
Posts: 2135
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Single interrupt trigger performing callback function several times.

Post by Roberthh » Sun Jul 05, 2020 3:55 pm

Two possible reasons for that:
a) bouncing of the input signal, if that comes from a switch. There are lots of discussions in this forum about de-bouncing.
b) false triggers due to slow slope of the input signal. That happens with the ESP32. Either increase the slope of the signal, or use the de-bouncing method.

User avatar
HexVitor
Posts: 6
Joined: Sat Dec 28, 2019 2:11 am
Location: Brazil

Re: Single interrupt trigger performing callback function several times.

Post by HexVitor » Tue Jul 07, 2020 12:49 pm

Thanks for the feedback. I did exhaustive tests with internal and external pull up and pull down resistors and none of the pins were 100% satisfied (they start well but sooner or later they perform the callback function 2 or 3 times with a single trigger). Based on this I believe that the hypothesis of the slow slope of the input signal is the most likely cause, however I do not know how to correct this. Will I need capacitors? If so, how will I use them? Thanks for helping me.

User avatar
Roberthh
Posts: 2135
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Single interrupt trigger performing callback function several times.

Post by Roberthh » Tue Jul 07, 2020 2:07 pm

You need an external active component like the schmitt-trigger gate 74hc14.

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: Single interrupt trigger performing callback function several times.

Post by jcw » Wed Jul 08, 2020 10:47 am

Still sounds like electrical contact bounce to me - resistors won't solve that. Google for "button debounce", e.g.

https://www.allaboutcircuits.com/techn ... l-with-it/

Post Reply