A push button looks simple but trips up a lot of beginners because it has four pins instead of two, and because the way you wire it changes whether a press reads as HIGH or LOW in code. This walks through connecting a button to a Raspberry Pi Pico the easy way, using the Pico's internal pull-up resistor, and reading its state with MicroPython.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi Pico - Button
What you need
- Raspberry Pi Pico W
- Raspberry Pi Pico
- Micro USB Cable
- Breadboard-mount Button with Cap
- Breadboard-mount Button Kit
- Panel-mount Button
- Push Button Module
- Breadboard
- Jumper Wires
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A breadboard button actually has four legs, but they're internally paired, so only two of them are electrically distinct. Pick one pin from each pair: connect one to a Pico GPIO pin and the other to GND. With the internal pull-up enabled in code you don't need an external resistor at all.
If you're using a button module instead, it exposes three labeled pins: GND goes to ground, VCC goes to the Pico's 3.3V supply, and OUT goes to a digital input pin. That module's output idles LOW and goes HIGH when pressed.
How it works
Inside a push button are two contacts, call them pin A and pin B. While the button is untouched they're disconnected, so no current flows between them. Pressing the button physically bridges A and B together, completing the circuit.
What that means for the Pico's GPIO pin depends on how you wired it. With the internal pull-up setup used here, the pin reads HIGH when the button is untouched and drops to LOW the moment it's pressed, because pressing connects the pin to GND.
Code
The pin is configured with Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP), which tells the Pico to hold that pin HIGH internally until something pulls it LOW. From there the code just reads the pin's state to know whether the button is currently pressed.
You can extend this same reading logic to detect press and release as distinct events, rather than just polling the current state, which is useful if you want to react once per press instead of continuously while held.
raspberry-pi-pico-button.py
from machine import Pin
import time
BUTTON_PIN = 0 # The Raspberry Pi Pico pin connected to the button (GP0)
# Set up the button pin. The Pin.PULL_UP enables the internal pull-up resistor.
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
while True:
# Read the state of the button
button_state = button.value()
# Print the button's state
print(button_state)
# Wait for half a second before reading the button again
time.sleep(0.5)
Things that go wrong
Mechanical contacts don't settle cleanly the instant they touch, they tend to bounce and register as several rapid presses before stabilizing. This is called chattering, and it mostly matters if your code is counting button presses, where a single press might get counted multiple times. For simple on/off state checks it's usually not worth worrying about.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-button

Top comments (0)