Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 12 of 47 · Games and logic · Beginner

Reaction time game

Wait for the LED to light, then hit the button as fast as you can. The Pi measures your reaction time in milliseconds.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Reaction time game: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Go LEDLED
ButtonPush button

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredGo LED - anode +Pin 11 (GPIO17)
2BlackGo LED - cathode -Pin 6 (GND)
3ColouredButton - SignalPin 13 (GPIO27)
4BlackButton - GNDPin 9 (GND)

Python code (gpiozero)

from gpiozero import LED, Button
from time import sleep, time
from random import uniform

led = LED(17)
button = Button(27)

print("Reaction game: press the button as soon as the LED lights.")

while True:
    led.off()
    print("Get ready...")
    sleep(uniform(2, 5))              # random wait of 2 to 5 seconds
    led.on()
    start = time()                    # note the time
    button.wait_for_press()
    reaction = (time() - start) * 1000
    led.off()
    print("Your reaction time:", round(reaction), "ms")
    button.wait_for_release()
    sleep(1.5)

How the code works

You will learn

  • Random numbers
  • Measuring time with time()
  • Combining waiting, timing and printing

Try this

  • Keep a best time and print "New record!" when you beat it.
  • Add a false start check: if the button is pressed before the LED, print "Too early!".
  • Use two buttons and make it a two-player game.

On a real Pi

  • A typical human reaction is 200 to 300 ms. Python on a Pi adds about a millisecond, so this is accurate enough for a game.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
7Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
8Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 11. Automatic night lampAll projects13. Wall switch with a relay →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.