Explorer on-board switch mystery

I’m taking my first few steps into the unknown with a Pi Pico H on a Pico Explorer base. Using a simple script to turn the LED on and off (that works correctly), I have a script that includes this line:

if button.value() == 1: # Check if the button is pressed

The button is specified earlier in the script as GP14, which I see corresponds to Switch X on the Explorer base. And I assume the button is already connected between GND and that pin. But when I click switch X, it’s not interrupting the script. What am I doing wrong?

We would really need to see the rest of your code to figure that out (tip: put three backticks [`] before and after your code when pasting it on the forum, it formats it to be more readable).

It’s worth noting that the button code you have won’t interrupt the program: the board will only react to the button press if you’re pressing it at the exact moment it runs that specific line of code to check the state of the button. MicroPython does have interrupts which will trigger a response any time the button is pressed, but you need to be careful with how you use those as it can cause weird things to happen.

Thanks. Since I posted earlier, I found a reference guide on the site covering various Explorer base features, including the buttons. By sharing this information with my coding assistant (Bard AI), I was able to get a script that would blink an LED and then break the loop if button A was pressed during an LED state change. But I wanted to see if there was a way to interrupt the loop at any time, even if the LED on and off cycles were 2.5 seconds each. The AI suggested creating an “inner loop” to watch for button pushes, but none of its proposed scripts have worked. Here’s where we’re at right now, and all it does is print an endless repeat of the phrase “Button state: false” with a momentary single line of “button state: true” when I click it, but the LED isn’t blinking and the break isn’t working. Of course, I have no practical need to do this–just trying to learn. I would appreciate any help to make it work:

from pimoroni import Button
from machine import Pin
import time

led = Pin(25, Pin.OUT)
button_a = Button(12)  # Replace with your desired button

while True:
    led.value(1)  # Turn LED on
    print("LED is on")

    while True:
        print("Button state:", button_a.read())
        if button_a.read():
            break  # Break out of inner loop
        time.sleep(0.01)  # Check for button press every 0.01 seconds

    time.sleep(2.5)  # LED on for 2.5 seconds
    led.value(0)  # Turn LED off

    print("LED is off")

    while True:
        print("Button state:", button_a.read())
        if button_a.read():
            break  # Break out of inner loop
        time.sleep(0.01)  # Check for button press every 0.01 seconds

    time.sleep(2.5)  # LED off for 2.5 seconds```
print("Button state:", button_a.read())
if button_a.read():

I’m not entirely sure what is going on here, but the print line seems to be causing problems. You’re using the Pimoroni Button class rather than the bog-standard Micropython stuff, and I’m not sure what that does in the background, it seems to be interfering with the next line somehow? I’d suggest changing it to look like this:

    while True:
        button_state = button.read()
        print("Button state:", button_state)
        if button_state:
            break  # Break out of inner loop
        sleep(0.01)  # Check for button press every 0.01 seconds

Wow–If you’re not sure “what is going on here,” I’m sunk! I got the impression I had to use the Pimoroni Button class, though I don’t understand why, since I would think the Explorer base would just connect the designated GP pins (12-15) the base’s buttons on the board. Anyway, I fooled around with it some more and am currently here. It generates the error
“Traceback (most recent call last):
File “”, line 5, in
NameError: name ‘Button’ isn’t defined”

import time

led = Pin(25, Pin.OUT)
button = Button(12)  # Replace with your desired button

while True:
    led.value(1)  # Turn LED on
    print("LED is on")

    while True:
        button_state = button.read()
        print("Button state:", button_state)
        if button_state:
            break  # Break out of inner loop
        sleep(0.01)  # Check for button press every 0.01 seconds

    time.sleep(1)  # LED on for 1 second
    led.value(0)  # Turn LED off

    print("LED is off")

    while True:
        button_state = button.read()
        print("Button state:", button_state)
        if button_state:
            break  # Break out of inner loop
        sleep(0.01)  # Check for button press every 0.01 seconds  
  
    time.sleep(1)  # LED off for 1 seconds

That’s because you’ve lost the

from pimoroni import Button

line from the top of your code.

Well I’m just some person on the internet, I don’t work at Pimoroni and I don’t have a great grasp of how that software works. I started using a Yukon not that long ago and noticed they have some functios which replicate things in basic MicroPython, I’m not quite sure why, and I was also having a problem with some statements which I thought looked perfectly fine. As @ahnlak said, if you haven’t imported Button you’ll need to.