Pico Display Pack 2.0 Buttons

Is there a way to trigger a different function with each button that is pressed on runtime without having to use a while loop and constantly check to see if a button is being pressed?

Use an interrupt on each button.

See chapter 6 in Official MicroPython Pico Guide.

Here is a simple example with button and an LED.

# Interrupt example
import machine
import time

pressed = False
led = machine.Pin(8, machine.Pin.OUT)
button = machine.Pin(14,machine.Pin.IN,machine.Pin.PULL_DOWN)

def button_handler(pin):
    time.sleep(0.05) # debounce
    global pressed
    if not pressed:
        pressed = True
        led.value(0)
    else:
        pressed = False
        led.value(1)

button.irq(trigger = machine.Pin.IRQ_RISING, handler = button_handler)

# Main loop
c = 0
while True:
    print(c)
    c = c + 1

Hints:
1 keep the interrupt routine very short
2 Do not use time.sleep() in the main loop

I hope this helps.

Hi Tonygo2,

I really appreciate your response. I understand how to do this using a regular button. The problem that I’m having is that the Pico Display Pack 2 has its own set of buttons that function through one of the pimoroni libraries and I’m not able to use the regular methods that I would use with a button.
Your code shows exactly what I want to do but I need to find a way using the pimoroni Display pack 2 library.

You don’t need the pimoroni libraries to use the buttons, They are wired to usable pins. Check the pinout to see what button uses what pin. Pressing the button grounds that pin.

I know it seems straight forward but this has not worked for me.

It can be frustrating, when things don’t work as planned.
The Buttons on the Display Pack are just “normal buttons”. The Pimoroni Button Library just makes it easier to use them in Micro Python, thats all.
Just be careful you don’t mix up GP number versus the physical pin number.
Button A, SW_A is wired to physical pin 16, which is GP12.

This may help.
It sets up a counter running flat out without any button polling.
You can change the colour of the display and the direction of the count with the buttons. Give it a go.

# Interrupt example using the Pimoroni Display 2
# Tony Goodhew 23 Sept 2023
from machine import Pin
import time
import gc

from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB332

from pimoroni import RGBLED

display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_RGB332, rotate=0)
display.set_backlight(1.0)
display.set_font("bitmap8") 

led = RGBLED(6, 7, 8)
led.set_rgb(0,0,0)   #Turn off the RGBLED

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(200, 0, 0)
BLUE = display.create_pen(0, 0, 200)
GREEN = display.create_pen(0, 200, 0)

COL = WHITE
delta = 1

led = Pin(25, Pin.OUT)
swA = Pin(12, Pin.IN, Pin.PULL_UP)
swB = Pin(13, Pin.IN, Pin.PULL_UP)
swX = Pin(14, Pin.IN, Pin.PULL_UP)
swY = Pin(15, Pin.IN, Pin.PULL_UP)


def sw_handlerA(pin):
    time.sleep(0.05) # debounce
    global COL
    COL = display.create_pen(200, 0, 0) # RED
    
def sw_handlerB(pin):
    time.sleep(0.05) # debounce
    global COL
    COL = display.create_pen(0, 200, 0) # GREEN
    
def sw_handlerX(pin):
    time.sleep(0.05) # debounce
    global COL
    COL = display.create_pen(0, 0, 200) # BLUE
    
def sw_handlerY(pin):
    time.sleep(0.05) # debounce
    global COL
    global delta
    COL = display.create_pen(200, 200, 0) # YELLOW
    delta = delta * -1 # Reverse count direction

swA.irq(trigger = machine.Pin.IRQ_FALLING, handler = sw_handlerA)
swB.irq(trigger = machine.Pin.IRQ_FALLING, handler = sw_handlerB)
swX.irq(trigger = machine.Pin.IRQ_FALLING, handler = sw_handlerX)
swY.irq(trigger = machine.Pin.IRQ_FALLING, handler = sw_handlerY)

count = 0

# The following loop runs flat out - NO time.sleep()
while True:
    display.set_pen(BLACK)
    display.clear()
    count = count + delta
    display.set_pen(COL)
    display.text(str(count), 10,10,300,6)
    display.update()

1 Like

Hey Tonygo2, this works perfectly! Thank you for taking the time to help with this. Now that I see your code I can see how I was implementing the interrupt in the wrong way. Now I can implement it into my project. Awesome job. Extra brownie points for you. Thanks again!

Glad to help. I hope the project works out.

1 Like