Microcontroller Reaction Time Game

by charpoopoopoo in Circuits > Microcontrollers

46 Views, 0 Favorites, 0 Comments

Microcontroller Reaction Time Game

mohammadhosein-mohebbi-6dkoR1b4nt0-unsplash.jpg
download (1).jpg

Reaction time games are a type of game that tests how quickly the player or players can respond to something such as a light turning on or a noise. It works by measuring the time between the event and the players reaction, these games are simple but can help people improve the speed in which they process information and then also the time it takes them to make a response.

My aim is to make a functional reaction time game using a Pi Pico an LED some wires and a display screen, the problem I aim to solve is how to code the Pi Pico so that it can run a functional reaction time game with an LED and a display screen

Background and research

https://www.build-electronic-circuits.com/555-timer-reaction-game/

this design uses a 555 timer that is set up in astable mode, it continuously outputs clock pulses at frequencies set by the resistor and capacitor, it then uses a CD4026 decade counter to count these pulses and update the display reguarly. when the player presses the start button the counter begins counting pulses from the 555 timer, as soon as the stop button is presses the counting stops and the last value remains on the 7 segment display

its components are a 9V battery, 555 timer, CD4026, 7 segment display, resistors, capacitors and push buttons.

the limitations are that you cant code it as all the parts are analog and have a set function, thus reducing its utility.

https://www.digikey.ie/en/maker/projects/build-a-reaction-time-mini-game-using-an-arduino/68669aa944ff4f6f8987a940f2025047?srsltid=AfmBOoqAi4VLEF9SP5qBpq3qe6GUihnq2vr8nYYUFuH2NLhE0a1JsypQ

this design uses an Arduino (microcontroller) and when the player presses the start button the Arduino chooses a random delay, after that delay an LED turns on and the player has to press the button as fast as possible, the Arduino calculates the time between the LED turning on and the player pressing the button it then shows the result on the screen display.

Components, Arduino Uno, push button, LED, resistors, jumper wires and a breadboard

a few limitations when constructing this project is its coding complexity as Arduinos require a much more specific code when compared to using Pi picos and analog parts.


Supplies

Pi Pico

LED

Wires

Momentary Push Button

LCD with I2C

70 ohms resistor

Identify Where to Place Pins

Screenshot 2025-10-15 120414.png
  1. Raspberry Pi Pico:
  2. GP0 connects to Pushbutton Pin 1 (in)
  3. GND connects to Pushbutton Pin 2 (in)
  4. GND connects to 16x2 I2C LCD GND
  5. GP1 connects to 16x2 I2C LCD SDA
  6. GP3 connects to 16x2 I2C LCD SCL
  7. VBUS connects to 16x2 I2C LCD VCC
  8. GP27 connects to Resistor pin2
  9. GND connects to LED Two Pin (Red) Cathode
  10. Pushbutton:
  11. Pin 1 (in) connects to Raspberry Pi Pico GP0
  12. Pin 2 (in) connects to Raspberry Pi Pico GND
  13. 16x2 I2C LCD:
  14. GND connects to Raspberry Pi Pico GND
  15. SDA connects to Raspberry Pi Pico GP1
  16. SCL connects to Raspberry Pi Pico GP3
  17. VCC connects to Raspberry Pi Pico VBUS
  18. Resistor:
  19. pin1 connects to LED Two Pin (Red) Anode
  20. pin2 connects to Raspberry Pi Pico GP27
  21. LED Two Pin (Red):
  22. Anode connects to Resistor pin1
  23. Cathode connects to Raspberry Pi Pico GND


The Code and What It Does

# === Import necessary modules ===
from machine import Pin, I2C
from time import ticks_ms, ticks_diff, sleep_ms
from urandom import getrandbits, randint
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

# === Pin definitions ===
BUTTON_PIN = 0 # Button connected to GPIO 0 (GP0)
LED_PIN = 1 # LED connected to GPIO 1 (GP1)

# === I2C setup for LCD ===
I2C_ID = 0 # Use I2C bus 0 on the Pico
SDA_PIN = 4 # I2C SDA connected to GP4
SCL_PIN = 5 # I2C SCL connected to GP5
I2C_ADDR = 0x27 # Typical I2C address for LCD (check yours with an I2C scanner)
I2C_NUM_

Firstly this code sets up the raspberry Pi Pico to use a Button LED and LCD screen. it processes libraries and allows randomness in the code and timing. the code allows the Pico to display messages through the LCD, measure time in milliseconds and generate code in random.it prepares the hardware for the reaction time code in the Pico.

# Initialize button and LED
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
led = Pin(LED_PIN, Pin.OUT)
led.value(0)
# Display startup message
lcd.clear()
lcd.putstr("Press button to")
lcd.move_to(0, 1)
lcd.putstr("start test")

This code sets up the LED and the Button. The code basically ensures that the button starts off and that the LCD displays "press button to start test" this tells the player that the game is ready to be started by pressing the button

# === Variables ===
waiting_for_reaction = False
led_on_time = 0
button_pressed = False

while True:
if button.value() == 1 and not button_pressed:
button_pressed = True
sleep_ms(20) # debounce

if not waiting_for_reaction:
# --- Start the test ---
lcd.clear()
lcd.putstr("Get ready...")
print("Get ready...")

# Random delay before LED turns on (1–4 seconds)
delay = randint(1000, 4000)
sleep_ms(delay)

# Turn LED ON and start timing
led.value(1)
led_on_time = ticks_ms()
waiting_for_reaction = True

lcd.clear()
lcd.putstr("LED ON! Press!")
print("LED ON! Waiting for button press...")

else:
# --- Button pressed after LED turned on ---
reaction_time = ticks_diff(ticks_ms(), led_on_time)

led.value(0)
waiting_for_reaction = False

lcd.clear()
lcd.putstr("Reaction time:")
lcd.move_to(0, 1)
lcd.putstr("{} ms".format(reaction_time))

print("Reaction time:", reaction_time, "ms")
sleep_ms(2000)

lcd.clear()
lcd.putstr("Press button to")
lcd.move_to(0, 1)
lcd.putstr("start test")

elif button.value() == 0:
button_pressed = False

sleep_ms(20)

This code is the heart of the reaction time game, it is the code that makes everything run smoothly. When the button is first pressed it starts of by displaying on the LCD "get ready" then waits a random 1-4 seconds, in that time randomly the LED will turn on, as soon as it lights up the player presses the button as quickly as possible. The code then calculates the reaction time in milliseconds turns the light of and displays the result on the LCD. the code then resets the game and the LCD shows "press button to start test" and waits for the button to be pressed