Introduction: Pi Pico Reaction Light Game
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
Pi Pico
Buzzer 5V
Arcade Button x 2
Wires x 9
Step 1: 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()
Step 2: Wiring Diagram
Wiring Diagram to follow when putting together pico
Step 3: Ground Wires
Connect ground pins to each other
be sure to do the right pins and don't connect the 2 sides
Step 4: Anode Wires
Connect anode wires
Make sure each has their own wires
Step 5: Buzzer Wires
Connect both anode and ground wires to buzzer
Step 6: Button Pico Connections
Connect wires to corresponding pin on the pico

