External Interrupts not working.

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
pynewt
Posts: 2
Joined: Sun Dec 22, 2019 3:15 am

External Interrupts not working.

Post by pynewt » Sun Dec 22, 2019 3:32 am

Hello all,

I'm very new to micropython and jumped in by purchasing a pyboard d-series (SF2W) to use as a learning tool for python in general. Currently I'm experiencing an issue with setting up an pushbutton to trigger an external interrupt. So far, I'm not having any luck following the official documentation. the code I'm using is very simple as this was just to test the functionality.

Code: Select all

from pyb import ExtInt, Pin, LED

led = LED(1)

#Wired the button between pin 'X6' and the X GND
ex1 = ExtInt('X6', ExtInt.IRQ_FALLING, Pin.PULL_UP, led.toggle())

Is there something I'm missing?

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

Re: External Interrupts not working.

Post by pythoncoder » Sun Dec 22, 2019 7:11 am

There are two problems The first is that you're specifying the callback with parentheses: this will cause it to run once, returning None (Python functions return None by default). None is then being assigned to the ISR callback. The second is that the callback expects a single arg, and led.toggle() does not. Try adding your own callback:

Code: Select all

from pyb import ExtInt, Pin, LED
import micropython
micropython.alloc_emergency_exception_buf(100)

led = LED(1)
def callback(arg):
    led.toggle()
#Wired the button between pin 'X6' and the X GND
ex1 = ExtInt('X6', ExtInt.IRQ_FALLING, Pin.PULL_UP, callback)  # No parens
See the docs to understand the thaumaturgy in line 3. tl;dr it helps with error messages if you make a mistake in a callback.
Peter Hinch

pynewt
Posts: 2
Joined: Sun Dec 22, 2019 3:15 am

Re: External Interrupts not working.

Post by pynewt » Sun Dec 22, 2019 8:32 am

Thank you so much! I was misreading the line from the documentation and not realizing the importance of the presence of an argument.
callback is the function to call when the interrupt triggers. The callback function must accept exactly 1 argument, which is the line that triggered the interrupt.
It works exactly as intended now.

Post Reply