To get you started with how to program your Pico in CircuitPython, especially for those who may have started out with the official MicroPython setup, we've 'ported' the Getting Started with MicroPython on Pico book examples to CircuitPython. The book is awesome, please download/purchase it to support Raspberry Pi Press!

Studying human reaction time is literally a science. There are many ways to test reaction time, but turning it into a game can make it fun. It's super easy to build a simple reaction game using a microcontroller, a few components and CircuitPython.

In this section, you'll learn how to use your Raspberry Pi Pico board to build a single-player reaction game, including an LED and a button, that tells you your reaction time in milliseconds. Then, add a more competitive aspect to it with the two-player version, built on the first example and including a second button, that tells you who pressed their button first.

Parts Used

scattered pile of multi colored unlit LEDs
Need some indicators? We are big fans of these diffused LEDs. They are fairly bright, so they can be seen in daytime, and from any angle. They go easily into a breadboard and will add...
$4.95
In Stock
Angled shot of 25 Through-Hole Resistors - 1.0K ohm 5% 1/4W.
ΩMG! You're not going to be able to resist these handy resistor packs! Well, axially, they do all of the resisting for you!This is a 25 Pack of...
$0.75
In Stock
angled shot of 20 6mm mini tactile button switches.
Little clicky switches are standard input "buttons" on electronic projects. These work best in a PCB but
$2.50
In Stock

Wiring the Reaction Game

For this example you'll need your Pico board, an LED, a resistor, a button switch, and a number of male to male jumper wires. A 220Ω-1.0KΩ resistor will work; a 220Ω resistor is shown in the diagram.

The first step is to connect the LED and button to your Pico board. The diagram uses a red LED, but you can choose any color, Wire them up as shown below.

  • Board GND to breadboard ground rail (using a jumper wire)
  • Board 3V3 to breadboard power rail (using jumper wire)
  • Board GP13 to 220Ω resistor
  • LED+ to 220Ω resistor
  • LED- to breadboard ground rail (using a jumper wire)
  • Board GP14 to leg of button (using a jumper wire)
  • Opposite leg of button to breadboard power rail (using jumper wire)

Programming the Reaction Game

Now that you've wired up your reaction game, you can begin programming it.

Update your code.py to the following and save.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Reaction game example for Pico. LED turns on for between 5 and 10 seconds. Once it turns off, try
to press the button as quickly as possible to measure reaction timm.

REQUIRED HARDWARE:
* LED on pin GP13.
* Button switch on pin GP14.
"""
import time
import random
import board
import digitalio

led = digitalio.DigitalInOut(board.GP13)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.GP14)
button.switch_to_input(pull=digitalio.Pull.DOWN)

led.value = True
time.sleep(random.randint(5, 10))
led.value = False
timer_start = time.monotonic()
while True:
    if button.value:
        reaction_time = (time.monotonic() - timer_start) * 1000  # Convert to ms
        print("Your reaction time was", reaction_time, "milliseconds!")
        break

The LED will turn on for a period of time. Once it turns off, try to press the button as quickly as you can. Your reaction time will show up in the serial console! To restart the game, click in the serial console and press CTRL+D to reload the board.

Now, a more detailed look at the code. First you import the necessary modules.

import time
import random
import board
import digitalio

You've imported time, board and digitalio previously, but this example uses a new module as well: random.

Next, you set up the LED and the button.

led = digitalio.DigitalInOut(board.GP13)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.GP14)
button.switch_to_input(pull=digitalio.Pull.DOWN)

Then, you begin the game. First, you turn on the LED. Then you use a time.sleep() to keep it on for a random amount of time, between 5 and 10 seconds. The random module includes the randint function, which returns a random integer between the two provided integers, in this case 5 and 10. Provide that function call to time.sleep(), and it will use the integer as seconds. This is followed by you turning off the LED.

led.value = True
time.sleep(random.randint(5, 10))
led.value = False

Then you begin the timer by setting a variable, timer_start, equal to time.monotonic().

timer_start = time.monotonic()

Now, you create a loop to check for the button press. Once the button is pressed, the code checks the current value of time.monotonic() and subtracts from that the initial value, e.g. when the timer started. This value is multiplied by 1000 to convert the value from seconds to milliseconds. Then, the message, Your reaction time was ### milliseconds! is printed to the serial console, where ### is the number of milliseconds between the time the LED turned off and the time you pressed the button.

while True:
    if button.value:
        reaction_time = (time.monotonic() - timer_start) * 1000  # Convert to ms
        print("Your reaction time was", reaction_time, "milliseconds!")
        break

The break is included because without it, the loop would continue to repeat following the button press, and the message would be spammed to the serial console repeatedly. You want to run the calculation once, and print the message once. The break stops the loop from continuing, and the code stops running following the button press.

Two Players Makes It More Fun

Trying to best yourself in a reaction game can be fun, but trying to best someone else can be even better. You can easily involve them in the game above; invite them to play and then compare reaction times to see who is fastest. But, with a few modifications to the hardware and software, you can create a game you can play together!

Wiring the Two Player Reaction Game

For this example, you'll need your current hardware setup, another button switch and a few more male-to-male jumper wires.

Add the second button switch as shown below.

  • Board GP16 to leg of second button (using a jumper wire)
  • Opposite leg of second button to breadboard power rail (using jumper wire)

Programming the Two Player Reaction Game

This example looks quite similar to the single-player version with a few modifications.

Update your code.py to the following and save.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Two-player reaction game example for Pico. LED turns on for between 5 and 10 seconds. Once it
turns off, try to press the button faster than the other player to see who wins.

REQUIRED HARDWARE:
* LED on pin GP13.
* Button switch on pin GP14.
* Button switch on GP16.
"""
import time
import random
import board
import digitalio

led = digitalio.DigitalInOut(board.GP13)
led.direction = digitalio.Direction.OUTPUT
button_one = digitalio.DigitalInOut(board.GP14)
button_one.switch_to_input(pull=digitalio.Pull.DOWN)
button_two = digitalio.DigitalInOut(board.GP16)
button_two.switch_to_input(pull=digitalio.Pull.DOWN)

led.value = True
time.sleep(random.randint(5, 10))
led.value = False
while True:
    if button_one.value:
        print("Player one wins!")
        break
    if button_two.value:
        print("Player two wins!")
        break

The LED still turns on for a period of time. Once it turns off, both of you can press your respective buttons. The winner is mentioned in a message printed to the serial console! To restart the game, click in the serial console and press CTRL+D to reload the board.

Now, a detailed look at the code. The imports are identical to the single-player version. The first modification needed is to include the setup for the second button. As there are now two buttons, you'll need to update the variable name for the existing button as well. Setup now looks like the following.

led = digitalio.DigitalInOut(board.GP13)
led.direction = digitalio.Direction.OUTPUT
button_one = digitalio.DigitalInOut(board.GP14)
button_one.switch_to_input(pull=digitalio.Pull.DOWN)
button_two = digitalio.DigitalInOut(board.GP16)
button_two.switch_to_input(pull=digitalio.Pull.DOWN)

The code that turns the LED on for a random number of seconds between 5 and 10 remains the same.

led.value = True
time.sleep(random.randint(5, 10))
led.value = False

This time, however, you do not start a timer as you are not recording a reaction time, you are checking for which button is pressed first. So, the loop is significantly different. This time, you're repeatedly checking for a button press, possible from both button one and button two.

while True:
    if button_one.value:
        print("Player one wins!")
        break
    if button_two.value:
        print("Player two wins!")
        break

Based on whichever button is pressed first, the appropriate message prints to the serial console, and the code stops the loop.

Click in the serial console and press CTRL+D to restart the game, and play again!

This guide was first published on Jan 21, 2021. It was last updated on Mar 19, 2024.

This page (Reaction Game) was last updated on Mar 18, 2024.

Text editor powered by tinymce.