Projects / Black Pill hardware stopwatch

Black Pill hardware stopwatch

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V to the Black Pill.

  2. STM32

    Wire the display

    CLK to PB8, DIO to PB9, power at 3.3 V.

  3. STM32

    Wire the buttons

    PB0 and PB1 each to GND.

  4. STM3212:45

    Install the TM1637 library

    Choose the Black Pill settings.

  5. STM32

    Upload the sketch

    The display shows zero.

  6. STM32

    Press the first button

    Time counts up.

  7. STM32

    Press it again

    Time stops.

  8. STM32

    Press the second button while stopped

    Time resets.

In short

How do you build a black Pill hardware stopwatch?

A Black Pill counts hundredths of a second with a hardware timer interrupt and shows them on a TM1637 display, with a start and stop button and a reset button. You need STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), TM1637 4-digit display, Tactile push button 6 mm × 2 and Breadboard 830 Tie Points. It takes 8 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
5

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)
TM1637 VCCBlack Pill 3.3 pinPower at 3.3 V
TM1637 GNDBlack Pill GNDCommon ground
TM1637 CLKBlack Pill PB8Display clock
TM1637 DIOBlack Pill PB9Display data
Start and stop buttonBlack Pill PB0 and GNDPress to run or pause
Reset buttonBlack Pill PB1 and GNDClears the time while paused

Code

A delay call keeps the processor waiting and drifts when other code runs, so it is a poor way to time something. A hardware timer is a counter driven by the chip clock that runs by itself. The sketch sets timer 3 to overflow one hundred times a second and attaches a small function to that event. The function only adds one to a counter while the stopwatch is running, so the count is exact whatever the main loop is doing. The main loop reads the buttons and sends the digits to the TM1637 display. The count is copied with interrupts briefly switched off so a tick cannot change it half way through the read. Under 100 seconds the display shows seconds and hundredths, and after that it switches to minutes and seconds, so look at the colon and the size of the number. The Black Pill has a crystal, so the time is accurate over a long run, but a button press has a few milliseconds of bounce, which the small delay hides. The display module runs from 3.3 V here.

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).

#include <TM1637Display.h>

const int CLK = PB8, DIO = PB9;
const int BTN_RUN = PB0, BTN_CLR = PB1;

TM1637Display disp(CLK, DIO);
HardwareTimer *tick = new HardwareTimer(TIM3);

volatile uint32_t hundredths = 0;
volatile bool running = false;

void onTick() {                                  // runs every 10 ms from the timer
  if (running) hundredths++;
}

bool prevRun = HIGH, prevClr = HIGH;
unsigned long lastEdge = 0;

void setup() {
  pinMode(BTN_RUN, INPUT_PULLUP);
  pinMode(BTN_CLR, INPUT_PULLUP);
  disp.setBrightness(5);
  tick->setOverflow(100, HERTZ_FORMAT);          // 100 interrupts per second
  tick->attachInterrupt(onTick);
  tick->resume();
}

void loop() {
  bool r = digitalRead(BTN_RUN), c = digitalRead(BTN_CLR);
  if (millis() - lastEdge > 30) {                // simple debounce
    if (prevRun == HIGH && r == LOW) { running = !running; lastEdge = millis(); }
    if (prevClr == HIGH && c == LOW && !running) { hundredths = 0; lastEdge = millis(); }
  }
  prevRun = r; prevClr = c;

  noInterrupts();
  uint32_t h = hundredths;
  interrupts();

  int value;
  if (h < 10000) value = h;                      // SS.hh up to 99.99 s
  else value = ((h / 6000) % 100) * 100 + (h / 100) % 60;   // then MM:SS
  disp.showNumberDecEx(value, 0b01000000, true); // colon on, leading zeros on
  delay(10);
}

Test checklist

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

CheckExpected
StartHundredths count up smoothly
StopThe display holds the time
Reset while runningNothing happens
Reset while stoppedDisplay returns to zero
Run for a minuteDisplay changes to minutes and seconds after 100 seconds

If it does not work

Display stays blank

Check CLK, DIO and power, and the pin names.

Time runs fast or slow

Check the board crystal and the timer frequency setting.

Buttons act twice

Check wiring to GND and the debounce.

Digits look odd

Check the display is a 4-digit TM1637 with a colon.

Upload fails

Check the ST-Link wires and the board settings.

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

Ask Lark AI

Common questions

What parts do I need for the black Pill hardware stopwatch?

STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), TM1637 4-digit display, Tactile push button 6 mm × 2 and Breadboard 830 Tie Points.

How long does the black Pill hardware stopwatch take to build?

About 150 minutes for a intermediate maker, in 8 steps.

How do I know the black Pill hardware stopwatch works?

Start: Hundredths count up smoothly. Stop: The display holds the time. Reset while running: Nothing happens.

Display stays blank: what should I check?

Check CLK, DIO and power, and the pin names.

Time runs fast or slow: what should I check?

Check the board crystal and the timer frequency setting.

Buttons act twice: what should I check?

Check wiring to GND and the debounce.

Digits look odd: what should I check?

Check the display is a 4-digit TM1637 with a colon.