Home / Projects

Project: build a reaction timer with a Raspberry Pi Pico

Reaction timer wiring: green LED on GP14 and red LED on GP15, each through 330 Ω to ground, and a button from GP16 to ground
Beginner

Wait for the green light, then hit the button as fast as you can. The Pico measures your reaction time to the millisecond, keeps your best score, and catches anyone who jumps the gun. It's a good first project: it uses outputs, an input, timing and a little randomness, and needs no soldering.

Difficulty: beginner. Time: about 30 minutes. Works on: Raspberry Pi Pico, Pico H, Pico W, Pico 2 and Pico 2 W, running MicroPython.

What you need

  • A Raspberry Pi Pico of any kind, with headers so it plugs into a breadboard
  • A micro USB cable that carries data, not just power (the Pico 2 uses the same connector)
  • A breadboard and a few male-to-male jumper wires
  • One green LED and one red LED (standard 5 mm or 3 mm)
  • Two 330 Ω resistors (orange, orange, brown bands)
  • One tactile push button, the small four-legged kind
  • Thonny, a free Python editor, on your computer

Why these parts and values

The Pico's pins run at 3.3 V. A red LED drops about 2.0 V, leaving 1.3 V across the resistor, so 330 Ω gives (3.3 − 2.0) ÷ 330 ≈ 4 mA. A standard green LED drops about 2.1 V, giving about 3.6 mA. That's clearly visible, and well within what a Pico pin can supply. You can check other values with the LED resistor calculator.

Some green LEDs are "true green" types that drop about 3 V. On 3.3 V they'll barely glow through 330 Ω. If yours is very dim, swap its resistor for 100 Ω, which gives about 3 mA.

The button uses the Pico's built-in pull-up resistor, so it needs no resistor of its own. The pin reads 1 normally and 0 while the button is pressed. Wiring buttons to ground with a pull-up is the most reliable choice on every Pico. On the Pico 2, it also avoids a known issue with the RP2350's internal pull-downs, listed as erratum RP2350-E9 in its datasheet.

The circuit

Pico GP14 (19) GP15 (20) GP16 (21) GND (23) 330 Ω green 330 Ω red button GND
Each LED has its own 330 Ω resistor. The button connects GP16 to ground when pressed. Numbers in brackets are the Pico's physical pin numbers.

Step 1: wire it up

Unplug the Pico from USB while you wire. Push the Pico into the breadboard so it straddles the centre gap, with the USB socket at one end. Our Pico pinout guide shows where every pin is. GP14, GP15 and GP16 are physical pins 19, 20 and 21, at the end away from the USB socket, and pin 23 next to them is a GND.

  1. Run a jumper from pin 23 (GND) to the breadboard's blue (−) rail. This is your ground rail.
  2. Green LED: connect a 330 Ω resistor from GP14 (pin 19) to an empty row. Put the green LED's long leg in that row and its short leg in another row, then jumper the short leg's row to the ground rail.
  3. Red LED: do the same from GP15 (pin 20) with the other 330 Ω resistor and the red LED.
  4. Button: place it across the centre gap. Connect one leg to GP16 (pin 21) and the leg diagonally opposite to the ground rail. Using diagonal legs makes sure you go through the switch, because pairs of legs on the same side are often joined inside.

New to breadboards? Our breadboard guide explains which holes are connected.

Step 2: set up MicroPython

If your Pico doesn't have MicroPython yet, hold down its BOOTSEL button while plugging it in, then follow the prompts in Thonny (Tools, Options, Interpreter, then choose MicroPython for the Raspberry Pi Pico and install it). Our guide to Python for electronics covers this in more detail.

Step 3: test each part

Before running the game, check the wiring. Run this in Thonny. Each LED should flash once, then the shell prints "pressed" while you hold the button.

from machine import Pin
import time

green = Pin(14, Pin.OUT)
red = Pin(15, Pin.OUT)
button = Pin(16, Pin.IN, Pin.PULL_UP)

for led in (green, red):
    led.on()
    time.sleep(0.5)
    led.off()

while True:
    print("pressed" if button.value() == 0 else "released")
    time.sleep(0.2)

Press Stop in Thonny when you're done. If something doesn't work, see the troubleshooting section below before going on.

Step 4: the game

from machine import Pin
import time
import random

green = Pin(14, Pin.OUT)                 # "Go" LED
red = Pin(15, Pin.OUT)                   # "Too soon" LED
button = Pin(16, Pin.IN, Pin.PULL_UP)    # reads 0 when pressed

def pressed():
    return button.value() == 0

def wait_for_release():
    while pressed():
        time.sleep_ms(10)
    time.sleep_ms(50)                    # let the contacts stop bouncing

best = None
print("Reaction timer. Press the button to start.")

while True:
    green.off()
    red.off()
    while not pressed():                 # wait for the player to start a round
        time.sleep_ms(10)
    wait_for_release()
    print("Get ready...")

    delay = random.randint(1500, 4000)   # random wait, 1.5 to 4 seconds
    start = time.ticks_ms()
    too_soon = False
    while time.ticks_diff(time.ticks_ms(), start) < delay:
        if pressed():
            too_soon = True
            break
        time.sleep_ms(1)

    if too_soon:
        red.on()
        print("Too soon! Wait for the green light.")
        wait_for_release()
        time.sleep_ms(1000)
        continue

    green.on()
    t0 = time.ticks_us()
    while not pressed():                 # tight loop for an accurate time
        pass
    reaction_ms = time.ticks_diff(time.ticks_us(), t0) // 1000
    green.off()

    if best is None or reaction_ms < best:
        best = reaction_ms
    print("Reaction: {} ms   Best: {} ms".format(reaction_ms, best))
    wait_for_release()
    time.sleep_ms(500)

Run it, press the button to start a round, and wait for green. A typical session looks like this:

Reaction timer. Press the button to start.
Get ready...
Reaction: 237 ms   Best: 237 ms
Get ready...
Too soon! Wait for the green light.

Most people score somewhere around 200 to 300 ms. To make the game run on its own without a computer, save the file to the Pico as main.py and power it from any USB charger. The scores are printed to Thonny's shell, so without a computer you'll just see the lights. Adding a display is one of the ideas below.

How it works

  • The random wait stops you learning the rhythm. While waiting, the code checks the button every millisecond, so an early press is caught straight away.
  • ticks_ms() and ticks_diff() are MicroPython's timers. Always use ticks_diff() to subtract them, because the counters wrap around after a while and plain subtraction would then give nonsense.
  • The tight loop after the green LED comes on checks the button as fast as the Pico can, many thousands of times a second, so the timing error is tiny compared with a human's reaction.
  • Debouncing. A button's metal contacts bounce for a few milliseconds when pressed or released, which the Pico can read as several presses. Waiting 50 ms after each release hides this. Without it, the bounce from releasing the start press could count as a false start.

Troubleshooting

  • An LED doesn't light: it's probably backwards. Swap its legs; the long leg goes towards the resistor. Also check its short leg reaches the ground rail.
  • The test always prints "pressed": the button is wired across two legs that are always joined. Rotate it 90° or use diagonally opposite legs.
  • It always prints "released": check one button leg goes to GP16 (pin 21) and the other to ground.
  • "Too soon" when you didn't press early: make sure you're using the code above with the 50 ms wait, and that the button isn't loose in the breadboard.
  • Thonny can't find the Pico: try another USB cable. Many cheap cables only carry power.

Take it further

  • Add a second button and a second green LED for a two-player race.
  • Add a small I²C display to show the time and best score. Our I²C, SPI and UART guide explains the wiring.
  • Add a passive buzzer with PWM for a beep on "go" and a buzz on a false start.
  • Average your last five times, and ignore any single round over a second.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.