Built-in project · Raspberry Pi Pico W

Pico W button on GP14 with a pull-down

MicroPython on a Pico W: a push button from 3.3 V to GP14, held low by a 10k pull-down, prints a line to the Serial Monitor at each press. The whole build, a Raspberry Pi Pico W and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $6.15. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Wiring a button with a pull-down resistor
  • Reading a pin with Pin.IN and value
  • Detecting a change instead of a level
  • Why an input pin must never be left floating
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 4

How it works

The button joins GP14 to 3.3 V when pressed. A 10k resistor from GP14 to ground, called a pull-down, holds the pin at 0 V the rest of the time. So the pin reads 0 when idle and 1 when pressed.

The loop checks the button every 10 milliseconds. It prints You pressed the button! only at the moment the reading goes from 0 to 1, not for as long as you hold it.

Parts list

4 parts, plus the jumper wires. They retail for about $6.15; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Pushbutton pin 2aRaspberry Pi Pico W pin GP14
Resistor, 10k Ω pin 1Raspberry Pi Pico W pin GP14
Pushbutton pin 1a3.3 V
Resistor, 10k Ω pin 2Ground

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Reading the button: the button joins GP14 to 3.3 V and a 10k holds the
# pin at 0 V otherwise, so pressed reads 1.
from machine import Pin
import time

button = Pin(14, Pin.IN)
was = 0

while True:
    now = button.value()
    if now == 1 and was == 0:
        print("You pressed the button!")
    was = now
    time.sleep_ms(10)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The message prints randomly or never This project
    Check that the 10k resistor connects GP14 to ground. Without it the pin floats and reads noise.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.