3

Is there a non-polling way to detect when an input goes from OFF to ON with the Raspberry Pi Pico MicroController?

I see the Raspberry Pi allows one to do something like below to detect rising edges on a GPIO port (reference: https://forums.raspberrypi.com/viewtopic.php?t=184361):

import RPi.GPIO as GPIO
GPIO.add_event_detect(<pin number>, GPIO.RISING, callback=<callback>)

Is there a similar API available for the Pico? When I try the above on Thonny/Pico, it fails because it doesn't recognize the GPIO module.

Vivek
  • 131
  • 3
  • Which Pico GPIO pin interrupt tutorial are you following? I once tried Tom's Hardware Thonny MicroPython and found everything easier than Rpi3/4.. – tlfong01 Oct 24 '22 at 08:07
  • I searched my old code and I found I used the following things: *import _thread* import utime

    global variables , increments with each interrupt

    counter_1 = 0 counter_2 = 0

    declare pins for leds

    red_led = Pin(20, mode = Pin.OUT, value = 0) yellow_led = Pin(21, mode = Pin.OUT, value = 0)

    declare pins for monitoring interrupts using push buttons

    *core_1_interrupt_pin = Pin(16,Pin.IN,Pin.PULL_DOWN)* *core_2_interrupt_pin = Pin(15,Pin.IN,Pin.PULL_DOWN)*

    – tlfong01 Oct 24 '22 at 08:22
  • 1
    I followed a couple of tutorials and this is one: https://electrocredible.com/raspberry-pi-pico-external-interrupts-button-micropython/. You also need to define call back functions. – tlfong01 Oct 24 '22 at 08:25
  • 1
    Thanks @tlfong01 -- the tutorial link was helpful. The key statement here is the irq call, thanks! – Vivek Oct 26 '22 at 13:30
  • You are welcome. Cheers. – tlfong01 Oct 27 '22 at 02:42

1 Answers1

3

The Raspberry Pi and the Raspberry Pico are two very different machines. Do not assume a tutorial for one has any relevance to the other.

You need to read the Raspberry Pico API documentation for your chosen programming language.

The Raspberry Pico supports GPIO interrupts so no GPIO polling is needed.

joan
  • 71,014
  • 5
  • 73
  • 106
  • 1
    Thanks... turns out it's as simple as: from machine import Pin... inputPin.irq(trigger=Pin.IRQ_RISING, handler=<callback>). References:Raspberry Pi Pico Python SDK, https://github.com/raspberrypi/pico-micropython-examples/blob/master/irq/irq.py. The latter also shows an inline lambda example!

    A MicroPython Tutorial but for a different platform: https://docs.micropython.org/en/v1.9.2/esp8266/esp8266/tutorial/pins.html#external-interrupts

    – Vivek Oct 24 '22 at 15:56