Black Pill hardware stopwatch
Build it step by step
0 of 8 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V to the Black Pill.
Wire the display
CLK to PB8, DIO to PB9, power at 3.3 V.
Wire the buttons
PB0 and PB1 each to GND.
Install the TM1637 library
Choose the Black Pill settings.
Upload the sketch
The display shows zero.
Press the first button
Time counts up.
Press it again
Time stops.
Press the second button while stopped
Time resets.
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
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| TM1637 VCC | Black Pill 3.3 pin | Power at 3.3 V |
| TM1637 GND | Black Pill GND | Common ground |
| TM1637 CLK | Black Pill PB8 | Display clock |
| TM1637 DIO | Black Pill PB9 | Display data |
| Start and stop button | Black Pill PB0 and GND | Press to run or pause |
| Reset button | Black Pill PB1 and GND | Clears 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.
| Check | Expected |
|---|---|
| Start | Hundredths count up smoothly |
| Stop | The display holds the time |
| Reset while running | Nothing happens |
| Reset while stopped | Display returns to zero |
| Run for a minute | Display 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 →
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.
