How to tell a plasma2040 a button has been released?


My only programming experience is with Defold in Lua, in which I can use functions such as button.pressed and button.released. With the plasma2040, you use a while loop as per the examples.

while True:
        a = True
        print("A pressed")
        led_strip.set_rgb(0, 100, 0, 0)
    else: led_strip.set_rgb(0, 0, 100, 0)

The problem is that is only true for one frame, the instance the button is pressed.

So my question is, how do I detect that a button has been released?

Please forgive my Python ignorance.


Best guess is look for a
a = False

Thanks for your reply but that doesn’t work! A is always false except for the one frame when the button is pressed. I assumed, like you, it would be either connected or disconnected, but it’s as if it were momentary.

Not sure this actually helps?

Import the Button class from the pimoroni module and the pin constants for the buttons:

from pimoroni import Button
from plasma import plasma2040
Set up an instance of Button for each button:

button_a = Button(plasma2040.BUTTON_A)
button_b = Button(plasma2040.BUTTON_B)
To get the button state, call .read(). If the button is held down, then this will return True at the interval specified by repeat_time until hold_time is reached, at which point it will return True every hold_time / 3 milliseconds. This is useful for rapidly increasing/decreasing values such as hue:

state =

pimoroni-pico/ at main · pimoroni/pimoroni-pico (

It’s useful in the sense that it seems I can rule out the thing that I wanted to do. “If the button is held down, then this will return True at the interval specified by repeat_time” is exactly what I was afraid of.

Having said that, at some point along the line the software knows if the button is down, so I just need to find out where. And see how to make that fit into my plan.

Perhaps it is possible to set repeat_time to 0? Where would I set that? I can already see the “divide by zero” error…

If you can’t get our shared Button class to do what you want it to, there’s a ton of functionality in raw MicroPython for reading button inputs, including setting up interrupts so you don’t have to wait for the button to be read in your main loop. The Get Started with MicroPython on Raspberry Pi Pico book covers a few different methods of reading buttons (IRQ interrupts are explained on page 90, but there are simpler methods described elsewhere).

Many thanks Hel, I will look into this soon. As you say I am pretty sure this is not possible within the current system. However, from what I can see, that is not the case with the IOexpander which is basically plug and play with the plasma2040. I need more buttons anyway, so I will use this
Phew, i almost had to learn something then!!

1 Like

Just in case anyone is looking at this in the future, an IO expander from Pimoroni and the following code gets you an additional 14 buttons to play with. It is easy to know when a button has been pressed/released.

This code triggers and event when a button is pressed for the first time. It doesn’t work well if you are pressing two buttons at the same time, but that problem could easily be solved. Good luck!

import time
import math
import plasma
from plasma import plasma2040
from pimoroni_i2c import PimoroniI2C
from pimoroni import RGBLED
from breakout_ioexpander import BreakoutIOExpander

# I2C config
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
ioe = BreakoutIOExpander(i2c, address=0x18)
for i in range(14):
    ioe.set_mode(i+1, BreakoutIOExpander.PIN_IN_PU)
buttonpress = 0

def pincheck():
    global buttonpress
    for i in range(1,15):
        if (ioe.input(i)) == 0: #button i is down 
            if (buttonpress != i): #this is a new button being pressed. 
                buttonpress = i
                if buttonpress == 1:
                                print("button 1 is being pressed")
                elif buttonpress == 2: 
                 print("button 2 is being pressed")
                elif buttonpress == 3: 
                 print("button 3 is being pressed")
                elif buttonpress == 4: 

while True:
1 Like