Projects / Blue Pill reaction timer

Blue Pill reaction timer

Intermediate2.2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM329.2 mA

    Wire the LED

    PA1 through 330 ohms, cathode to GND.

  3. STM32

    Wire the button

    PA0 to GND.

  4. STM32

    Upload the sketch

    Then switch to USB power.

  5. STM32

    Open the serial monitor

    115200 baud.

  6. STM32

    Press the button

    Wait for the light.

  7. STM329.3 mA

    Press again when the LED lights

    Your time prints.

In short

How do you build a blue Pill reaction timer?

A Blue Pill lights an LED after a random wait and times how fast you press a button, printing the result over USB serial. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LED 5 mm red, Resistor 330 Ω, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 130 minutes.

Level
Intermediate
Time
About 130 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
LED anode330 ohm resistor to Blue Pill PA1Go light
LED cathodeBlue Pill GNDReturn
ButtonBlue Pill PA0 and GNDPress to start and to react
USB cableBlue Pill USB socketSerial output (after programming, unplug the ST-Link power wire)

Code

A reaction timer measures the gap between a light turning on and your finger pressing a button. The wait before the light is random, so you cannot guess it, and pressing early is counted as a false start. When the LED switches on the sketch records the time in microseconds and loops until the button goes low, then prints the difference in milliseconds. Checking the button in a tight loop is fine here because nothing else needs to happen. The random generator is seeded from a floating analog pin and the clock so each power-up gives a different sequence. A healthy person typically reacts in around two to three tenths of a second, but the number depends on tiredness, the button and the finger, so treat it as a game and not as a test of anything. The Blue Pill's USB socket acts as a serial port, so the results appear in the serial monitor.

Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int LED = PA1;
const int BTN = PA0;

unsigned long best = 0;

void waitRelease() {
  while (digitalRead(BTN) == LOW) {}
  delay(40);
}

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(115200);
  randomSeed(analogRead(PA2) ^ micros());            // PA2 is left floating on purpose
  delay(500);
  Serial.println("Reaction timer: press the button to start a round.");
}

void loop() {
  if (digitalRead(BTN) == HIGH) return;
  waitRelease();
  Serial.println("Get ready...");
  digitalWrite(LED, LOW);

  unsigned long wait = random(2000, 5001);
  unsigned long t0 = millis();
  while (millis() - t0 < wait) {
    if (digitalRead(BTN) == LOW) {                    // pressed too early
      Serial.println("False start! Wait for the light.");
      waitRelease();
      return;
    }
  }

  digitalWrite(LED, HIGH);
  unsigned long start = micros();
  while (digitalRead(BTN) == HIGH) {
    if (micros() - start > 2000000UL) {               // 2 second limit
      digitalWrite(LED, LOW);
      Serial.println("Too slow, try again.");
      return;
    }
  }
  unsigned long ms = (micros() - start) / 1000;
  digitalWrite(LED, LOW);
  waitRelease();

  Serial.print("Reaction: "); Serial.print(ms); Serial.println(" ms");
  if (best == 0 || ms < best) { best = ms; Serial.println("New best!"); }
  Serial.print("Best so far: "); Serial.print(best); Serial.println(" ms");
  Serial.println("Press the button for another round.");
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual STM32 Blue Pill (F103C8) with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Reaction timer: press the button to start a round.

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Press before the lightFalse start message
Press after the lightTime in milliseconds
No press for 2 secondsToo slow message
Several roundsBest time is kept
Random waitDifferent each round

If it does not work

Nothing in the monitor

Check USB support is CDC and the port.

LED never lights

Check polarity and PA1.

Times are very short

Check the button is not bouncing, and wiring to PA0.

Same waits every power-up

Leave PA2 unconnected for the seed.

Upload fails

Check the ST-Link wires and boot jumpers.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the blue Pill reaction timer?

STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LED 5 mm red, Resistor 330 Ω, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the blue Pill reaction timer take to build?

About 130 minutes for a intermediate maker, in 7 steps.

How do I know the blue Pill reaction timer works?

Press before the light: False start message. Press after the light: Time in milliseconds. No press for 2 seconds: Too slow message.

Nothing in the monitor: what should I check?

Check USB support is CDC and the port.

LED never lights: what should I check?

Check polarity and PA1.

Times are very short: what should I check?

Check the button is not bouncing, and wiring to PA0.

Same waits every power-up: what should I check?

Leave PA2 unconnected for the seed.