irq firing multiple times, why?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
thewifimaster
Posts: 9
Joined: Fri Oct 08, 2021 5:37 pm

irq firing multiple times, why?

Post by thewifimaster » Sat Oct 30, 2021 5:11 pm

I have two light barriers connected to two GPIO inputs and the following program code to handle the status change. What I do not understand is why the irq typically fires 8x times on a status change. I now just filter it in my code, but want to understand what is happening.

Any hints?

Code: Select all

import machine
import micropython
    
micropython.alloc_emergency_exception_buf(100) # enable debugging in IRQ handler as per https://docs.micropython.org/en/latest/reference/isr_rules.html#the-emergency-exception-buffer

LICHTSCHRANKE_1_PIN = const(9)
LICHTSCHRANKE_2_PIN = const(11)


class lichtschranke:
    
    def __init__(self, gpio):
        self.pin = machine.Pin(gpio, machine.Pin.IN, machine.Pin.PULL_DOWN)
        self.pin.irq(trigger = machine.Pin.IRQ_FALLING, handler = self.irq_handler)
        self.status = self.pin.value()
        
    def irq_handler(self, pin): # pin argument has to be set as it is sent by the IRQ
        if self.status != self.pin.value(): # for whatever reason up to 8x IRQs fire with one change, debouncing does not seem to be the isse and disabling the IRQ does not make sense, hence filter
            try:
                micropython.schedule(self.process, self.pin.value()) # detach any follow-on data processing task to the main program routine to keep the IRQ routine short
                self.status = self.pin.value()
            except RuntimeError: # schedule queue fills up very quickly, ignore - not sure if this is the right thing to do though (..)
                pass
                
    def process(self, data):
        print('%s: %s' % (self.pin, data))


l1 = lichtschranke(LICHTSCHRANKE_1_PIN)
l2 = lichtschranke(LICHTSCHRANKE_2_PIN)

while True:
    pass
I am on v1.17 on a Lolin S2 Mini (ESP32-S2).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: irq firing multiple times, why?

Post by pythoncoder » Sun Oct 31, 2021 9:27 am

I would first investigate whether the hardware is generating multiple pulses. Can you get access to an oscilloscope or logic analyser?
Peter Hinch
Index to my micropython libraries.

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

Re: irq firing multiple times, why?

Post by Roberthh » Sun Oct 31, 2021 1:13 pm

Most likely the interrupt is firing multiple times. That happens if the input signal has a "slow" slope. The input logic of the GPIO port can create several high/low transitions, when the signal is about Vdd/2. That can be caused by noise, internal or external. That can be covered by software or hardware. Software would ignore the following interrupts for a while. Hardware would be to set up a Schmitt-Trigger input characteristic.

thewifimaster
Posts: 9
Joined: Fri Oct 08, 2021 5:37 pm

Re: irq firing multiple times, why?

Post by thewifimaster » Thu Nov 04, 2021 9:47 pm

Thanks Peter and Robert, guess I'll dig out that Oscilloscope at work :)

thewifimaster
Posts: 9
Joined: Fri Oct 08, 2021 5:37 pm

Re: irq firing multiple times, why?

Post by thewifimaster » Mon Nov 08, 2021 8:44 pm

Ok, as it seems it requires some debouncing.

Post Reply