Back to projects
Tier 3 · Code the Real WorldmediumAge 10+60 min

Reaction Game

Press the button as fast as you can when the light flashes. See your time.

What you’ll learn

  • Reading a button with digitalRead and INPUT_PULLUP.
  • Measuring milliseconds between events with millis().
  • Printing data back to your computer with Serial.

What you need

ItemQty~CostWhere to buy
Arduino Uno (or Nano clone)1GHS 120.00
Online — official Uno is ~GHS 375; a Nano clone is ~GHS 75.
USB cable for the board1GHS 15.00
Probably already in the kit.
Red LED + 220 Ω1GHS 2.25
Online.
Tactile push-button1GHS 4.50
Online.
Piezo buzzer1GHS 15.00
Online.
Half-size breadboard + jumpers1GHS 30.00
Online.
Total~GHS 186.75

Build it step by step

  1. Wire LED on D6, button on D2, buzzer on D8

    Standard pull-down for the button (or use INPUT_PULLUP and look for LOW).

  2. Upload the code

    Code waits a random 2-5 s, flashes the LED, listens for the button.

  3. Open the Serial Monitor

    9600 baud. Each round prints "X ms" — your reaction time.

  4. Get four friends, take turns

    Best of three counts.

How it works

millis() returns the number of milliseconds since the Arduino booted. Stash it the moment the LED comes on, stash it again the moment the button reads pressed, subtract — that’s your reaction time. Anything below 200 ms is suspicious; humans average 250 ms.

If something’s not working

Always reads 0 ms
  • · Button pin is floating — add a 10 kΩ pull-down or use INPUT_PULLUP and invert the read.
Serial Monitor blank
  • · Wrong baud rate. Match to Serial.begin(9600).

Try this next

Track the best of 5 in a variable and announce it with a longer buzzer tone.

Add a "false-start" detector — buzz angrily if you press before the LED.

The code

Paste this into the Arduino IDE and hit Upload. Each line tells the board what to do, in order — the more lines you read, the more you understand.

// Reaction game
const int LED = 6;
const int BTN = 2;
const int BUZ = 8;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  pinMode(BUZ, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(A0));
}

void loop() {
  delay(random(2000, 5000));   // wait 2-5s
  digitalWrite(LED, HIGH);
  unsigned long t0 = millis();
  while (digitalRead(BTN) == HIGH) {} // wait for press
  unsigned long t1 = millis();
  digitalWrite(LED, LOW);
  Serial.print(t1 - t0);
  Serial.println(" ms");
  tone(BUZ, 880, 100);
  delay(2000);
}