Back

Activity 2.7: Reaction Timer

Digital Input & Timing — Day 1 of 2

divider

Activity 2.7

Reaction Timer

Key Concepts

Digital Input

Measuring Time

Fast or Slow

Everything So Far Has Been Output

Five activities of telling pins what to do. Today the board starts listening — and once it can listen, it can react, and once it can react, you can time it.

One Method, Two Jobs

led.value(1) # setting: turn this pin on
state = button.value() # reading: what is this pin right now?

.value() with a number sets the pin. .value() with empty parentheses reads it, and hands you back 0 or 1.

An Input Pin Needs a Default

A pin with nothing connected does not read 0— it reads whatever electrical noise is nearby. So an input pin needs something holding it at a known value when the button is not pressed.

button = Pin(33, Pin.IN)

A 10K pull-down resistor from the pin to GND quietly holds it at 0. Press the button and 3V3 overrules it.

The board also has one built in, switched on with Pin(33, Pin.IN, Pin.PULL_DOWN). Today you build your own, so you can see it.

So: Pressed Means 1

Not pressed → 0. Pressed → 1. The same as an LED: 1 is the active state.

You will meet PULL_UP elsewhere, which does the opposite — pressed reads 0. Same idea, inverted. Worth knowing when you read other people's code.

Measuring Milliseconds

start = time.ticks_ms()
# ... something happens ...
stop = time.ticks_ms()
print(time.ticks_diff(stop, start), "ms")

time.ticks_ms() is a running count of milliseconds. Read it twice and compare.

Why Not Just Subtract?

That millisecond counter eventually runs out of digits and rolls back to zero, the way an odometer would. If it rolls over between your two readings, plain subtraction gives a nonsense answer.

time.ticks_diff(stop, start) handles the rollover for you. Use it instead of - whenever you subtract two tick readings.

Today's Objectives

  • Wiring a button and reading it as a digital input
  • Wiring a 10K pull-down resistor so an unpressed pin reads 0
  • Measuring elapsed time with ticks_ms() and ticks_diff()
  • Comparing a measured time with if/else to light green or red

Key Terms

Digital Input
A pin the board reads instead of drives, giving 0 or 1.
Pull-Down Resistor
A resistor from an input pin to GND, holding it at 0 when nothing else is driving it. Today's is a 10K on the breadboard; the board also has one built in, switched on with Pin.PULL_DOWN.
Reaction Time
The milliseconds between the GO signal and the press.

'F' → Fullscreen

divider

Build

Today's game needs three LEDs, not five. A blue LED is the GO signal, green means your reaction was fast, and red means it was slow. Create a new MicroPython script named 2-7-reaction-timer.


Task 1: Wire the Button and Read It

  • Wire one push button between the 3V3 pin and GPIO 33.
  • Add a 10K pull-down resistor from GPIO 33's side of the button to GND.
  • Create the button as an input pin.
  • Light the blue LED while the button is held, and turn it off when released.

GPIO 33 is the last pin in the block you have been using — immediately to the left of 32:

The SparkFun IoT RedBoard RP2350 with the pin labeled 33 outlined on the top header

The button connects GPIO 33 to 3V3 when it is pressed. The 10K resistor connects the pin to GND the rest of the time:

Vertical circuit diagram: 3V3 at the top, then the push button, then a junction that runs off to GPIO 33, then a 10K resistor (brown-black-orange), then GND at the bottom

With the USB cable unplugged, change Activity 2.6's array into the game's three lights, then press the button into the empty left side of the board so it straddles the gap down the middle. The 3V3 wire goes to the top + rail rather than straight to the button, so the parts you add in later activities can share it:

PartWhere it goes
GPIO 31 and 32's wires, LEDs, and resistorsTake them off
LED on GPIO 28 (e16, e17)Swap in a blue LED, long leg in e16
LED on GPIO 29 (e19, e20)Swap in a green LED, long leg in e19
LED on GPIO 30 (e22, e23)Swap in a red LED, long leg in e22
Button legse9, e11, f9, f11
Wire from GPIO 33a9 (above the left leg)
Wire from the 3V3 pinTop + rail
JumperTop + rail to a11 (above the right leg)
10K resistor (brown, black, orange)c9 to c6
Jumpera6 to the top − rail
A breadboardThree LEDs in a row: blue on GPIO 28 in e16 and e17, green on GPIO 29 in e19 and e20, and red on GPIO 30 in e22 and e23, each with its resistor up into the top minus rail. A push button straddles the center gap with its legs in e9, e11, f9, and f11. A pink wire from GPIO 33 lands in a9. A red wire from 3V3 lands on the top plus rail, and a red jumper runs from the rail down to a11. A 10K resistor runs left from c9 to c6, and a black jumper takes a6 up to the top minus rail.

Both wires go on the same side of the gap. A button's four legs are two pairs that are always joined, and pressing it joins the pairs to each other. The button only fits across the gap one way, and then each pair straddles the gap: e9 is joined to f9, and e11 to f11. So a9 and a11 are on different pairs, and only a press connects them. If the blue LED stays lit with nothing pressed, one of your wires is in the bottom half, on the same pair as the other.

The 10K resistor goes on GPIO 33's side of the button, in column 9 with the pin's wire, and runs left, away from 3V3. With the button open, it is the only thing connected to the pin, and it leads to GND, so the pin reads 0. Pressing connects the pin straight to 3V3, and the pin reads 1. Only a little current flows through 10K, so 3V3 wins easily.

Reaction Timer
from machine import Pin
blue = Pin(28, Pin.OUT)
button = Pin(33, Pin.IN)
while True:
if button.value() == 1:
blue.value(1)
else:
blue.value(0)

Task 2: The Reaction Test

  • Import time and random.
  • Each time round the loop, wait a random 2–4 seconds, then light the blue LED as the GO signal.
  • Start timing at GO, stop when the button is pressed, and print the reaction time.

The delay has to be random. A fixed pause lets you anticipate the GO, and then you are measuring your counting, not your reflexes.

The inner while loop does nothing at all on purpose — pass just means "keep checking." The program sits there until the button goes down.

Reaction Timer
from machine import Pin
import time
import random
blue = Pin(28, Pin.OUT)
button = Pin(33, Pin.IN)
while True:
print("When the blue LED turns on, push the button!")
time.sleep_ms(random.randint(2000, 4000))
blue.value(1)
start = time.ticks_ms()
while button.value() == 0:
pass
react_time = time.ticks_diff(time.ticks_ms(), start)
blue.value(0)
print("Your reaction time was", react_time, "ms")
time.sleep(3)

Task 3: Fast or Slow

  • Add the red and green LEDs.
  • After each press, light green if the time is 215 ms or less, and red if it is slower.
  • Leave the result showing for 5 seconds, then turn both off before the next round.
Reaction Timer
from machine import Pin
import time
import random
blue = Pin(28, Pin.OUT)
green = Pin(29, Pin.OUT)
red = Pin(30, Pin.OUT)
button = Pin(33, Pin.IN)
while True:
print("When the blue LED turns on, push the button!")
time.sleep_ms(random.randint(2000, 4000))
blue.value(1)
start = time.ticks_ms()
while button.value() == 0:
pass
react_time = time.ticks_diff(time.ticks_ms(), start)
blue.value(0)
if react_time <= 215:
green.value(1)
else:
red.value(1)
print("Your reaction time was", react_time, "ms")
time.sleep(5)
green.value(0)
red.value(0)

Challenge (Optional): Your Best Time

  • Keep a variable best that holds your fastest time so far.
  • After each round, replace it if the new time beats it, and print it under the reaction time.
divider

Checkpoint

Your finished program should behave like this:

Terminal window
When the blue LED turns on, push the button!
Your reaction time was 243 ms
When the blue LED turns on, push the button!
Your reaction time was 198 ms
  • The blue LED lights after a random wait of 2–4 seconds
  • Reaction times land somewhere around 150–400 ms
  • Green lights for 215 ms or less, red for anything slower, and both go off after 5 seconds
  • The next round starts on its own
divider

Reflection

Answer the following questions before submitting your work.

  1. What does the 10K resistor from GPIO 33 to GND do, and what would an unpressed input pin read without it?
  2. The wait before the blue LED is random. What would a fixed three-second wait let a player do, and how would that spoil the measurement?
  3. Hold the button down before the blue LED comes on, and keep holding it. What reaction time does the program print? Use the inner while loop to explain why.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete