Pico Explorer registering false button presses

Hi, I’m writing my first MicroPython script on my Pico 2 using Pico Explorer. I want a function that advances the code when any button is pressed. Here is what I am using

from pimoroni import Button
tl = Button(12) # Top Left / Button A
tr = Button(14) # Top Right / Button X
bl = Button(13) # Bottom Left / Button B
br = Button(15) # Bottom Right / Button Y

if tl.read() or tr.read() or bl.read() or br.read():
    # my code

However, even if all of these are separate if/elif statements, without any being pressed, the code moves on and runs # my code anyways

Why are the ABXY buttons registering without being pressed? Issue also occurs with .is_pressed()

I’ve tried

print(str(tl.read()))

and it returns True, however if I do

if tl.read() == False:

it still doesn’t work when pressed

what happens if you do this:

from pimoroni import Button
import time
tl = Button(12) # Top Left / Button A
tr = Button(14) # Top Right / Button X
bl = Button(13) # Bottom Left / Button B
br = Button(15) # Bottom Right / Button Y

while True:
  time.sleep(0.5)
  print(tl.read(), tr.read(), bl.read(), br.read())

Wait a few seconds, then hold a button, and tell me what it prints.

Give this a go.

from machine import Pin

button_a = Pin(12, Pin.IN, Pin.PULL_UP)
button_b = Pin(13, Pin.IN, Pin.PULL_UP)
button_x = Pin(14, Pin.IN, Pin.PULL_UP)
button_y = Pin(15, Pin.IN, Pin.PULL_UP)

while True:
    if button_a.value() == 0:
        do this    
    elif button_b.value() == 0:
        do that
    elif button_x.value() == 0:
        do the other thing
    elif button_y.value() == 0:
        etc

You can set up the buttons and colours within the import explorer statement.

# Button Test 2 - Tony Goodhew 9 Dec 2024

from explorer import button_a, button_b, button_c, button_x, button_y, button_z, display, WHITE, CYAN, MAGENTA, YELLOW, GREEN, BLACK, BLUE, RED
display.set_font("bitmap8")
GREY = display.create_pen(50,50,50)
while True:
    display.set_pen(GREY)
    display.clear()
    if button_a.value() == 0:
        display.set_pen(RED)
        display.text("A",100,100,200,6)
    elif button_b.value() == 0:
        display.set_pen(GREEN)
        display.text("B",100,100,200,6)
    elif button_c.value() == 0:
        display.set_pen(BLUE)
        display.text("C",100,100,200,6)
    elif button_x.value() == 0:
        display.set_pen(YELLOW)
        display.text("X",100,100,200,6)
    elif button_y.value() == 0:
        display.set_pen(CYAN)
        display.text("Y",100,100,200,6)
    elif button_z.value() == 0:
        display.set_pen(MAGENTA)
        display.text("Z",100,100,200,6)
    display.update()

Unfortunately, you cannot use the more logical pull-down method which goes HIGH/True when the button is pressed with a RP2350.

1 Like