Pi Pico Reaction Light Game

by JimmyMortel123 in Circuits > Raspberry Pi

12 Views, 0 Favorites, 0 Comments

Pi Pico Reaction Light Game

Screenshot 2025-05-29 at 3.09.51 pm.png
Screenshot 2025-05-29 at 3.08.37 pm.png

This project uses a 5V buzzer and 2 buttons to test your reaction time versus a friend, it will allow you to use code and a few supplies to make your own reaction game.

Supplies

RGB LED x 2

Buzzer 5V

Arcade Button x 2

Pushbutton x 1

Wires x 14

Resistor 220Ω x 2

Code for Execution

from machine import Pin, PWM

import utime

import urandom


# GPIO pin definitions

RED_BUTTON_PIN = 21

BLUE_BUTTON_PIN = 27

RED_LED_PIN = 22

BLUE_LED_PIN = 18

BUZZER_PIN = 3


# Setup

red_button = Pin(RED_BUTTON_PIN, Pin.IN, Pin.PULL_UP)

blue_button = Pin(BLUE_BUTTON_PIN, Pin.IN, Pin.PULL_UP)

red_led = Pin(RED_LED_PIN, Pin.OUT)

blue_led = Pin(BLUE_LED_PIN, Pin.OUT)

buzzer = PWM(Pin(BUZZER_PIN))


def sound_buzzer_start(freq=1000):

buzzer.freq(freq)

buzzer.duty_u16(20000)


def sound_buzzer_stop():

buzzer.duty_u16(0)


def main():

while True:

red_led.value(0)

blue_led.value(0)


# Random delay between 1000 and 4999 ms

delay_ms = urandom.getrandbits(10) % 4000 + 1000

utime.sleep_ms(delay_ms)


# Start buzzer

sound_buzzer_start()

reaction_start = utime.ticks_ms()


winner = None


# Wait for a button press

while True:

if red_button.value() == 0:

winner = 'red'

red_led.value(1)

break

if blue_button.value() == 0:

winner = 'blue'

blue_led.value(1)

break

if utime.ticks_diff(utime.ticks_ms(), reaction_start) >= 500:

sound_buzzer_stop()

utime.sleep_ms(5)


reaction_time = utime.ticks_diff(utime.ticks_ms(), reaction_start)

sound_buzzer_stop()

print(f"{winner.upper()} wins! Reaction time: {reaction_time} ms")

utime.sleep(2)


# Start game

main()

Wiring Diagram

circuit_image (2).png

Wiring Diagram to follow when putting together pico

Ground Wires

Screenshot 2025-05-26 at 9.30.47 am.png

Connect ground pins to each other

be sure to do the right pins and don't connect the 2 sides

Anode Wires

Screenshot 2025-05-29 at 2.56.36 pm.png

Connect anode wires

Make sure each has their own wires

Buzzer Wires

Screenshot 2025-05-29 at 2.59.06 pm.png

Connect both anode and ground wires to buzzer

Button Pico Connections

Screenshot 2025-05-29 at 3.00.07 pm.png
Screenshot 2025-05-29 at 3.01.57 pm.png

Connect wires to corresponding pin on the pico