Projects / Toy track lap timer

Toy track lap timer

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire the sensor

    VCC to 5V, GND to GND, OUT to D2.

  2. NANO

    Wire the display

    CLK to D5, DIO to D6, plus power.

  3. NANO

    Wire the button

    D3 to GND.

  4. NANO

    Upload the sketch

    The display shows 0000.

  5. NANO

    Wave a hand in front of the sensor

    The first pass starts the clock.

  6. NANO

    Wave again after a few seconds

    The lap time appears.

  7. NANO

    Wait two seconds

    The display alternates last and best lap.

  8. NANO

    Press the reset button

    Times clear to zero.

In short

How do you build a toy track lap timer?

An IR beam across a toy car track times each lap and shows the best time. You need Arduino Nano, IR obstacle avoidance sensor module, TM1637 4-digit display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
5

Parts list

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

Wiring

FromToWhy
Sensor VCCNano 5VPower
Sensor GNDNano GNDGround
Sensor OUTNano D2Goes LOW when a car passes in front
TM1637 CLKNano D5Display clock
TM1637 DIONano D6Display data
TM1637 VCC and GNDNano 5V and GNDPower
ButtonNano D3 to GNDReset the times

Code

The infrared obstacle module sends out a beam and watches for the reflection. When a car passes in front, the reflection appears and the output goes low, which triggers an interrupt on pin D2. The interrupt only records the time; the main loop does the work, which keeps the interrupt very short. The first pass starts the clock, and every later pass is a completed lap: the lap time is the time since the previous pass. A minimum lap time of 1.5 seconds stops one car from being counted twice as it rolls through. The display shows the lap in seconds and hundredths, and every two seconds it switches between the last lap and the best lap. The reset button clears all the times. Mount the sensor on the side of the track at the start line, aiming across the lane at the passing cars. Adjust the little screw on the module until it triggers on a car but not on the floor or the far wall. Bright sunlight can confuse infrared sensors, so test indoors.

Runs on a virtual Arduino Nano 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 SENSOR = 2;
const int RESET_BTN = 3;
const int CLK = 5, DIO = 6;
TM1637Display display(CLK, DIO);

const unsigned long MIN_LAP_MS = 1500;     // ignore a second trigger from the same car
volatile unsigned long triggerAt = 0;
volatile bool triggered = false;

unsigned long lastPass = 0;
unsigned long lastLap = 0, bestLap = 0;
unsigned long showSwitch = 0;
bool showBest = false;

void onBeam() {
  triggerAt = millis();
  triggered = true;
}

void showSeconds(unsigned long ms) {
  // ms to a number like 12.34 on four digits, with the dot after the second digit
  unsigned long hundredths = ms / 10;
  if (hundredths > 9999) hundredths = 9999;
  display.showNumberDecEx((int)hundredths, 0b01000000, true);
}

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(RESET_BTN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SENSOR), onBeam, FALLING);
  display.setBrightness(5);
  display.showNumberDec(0, true);
}

void loop() {
  if (digitalRead(RESET_BTN) == LOW) {
    lastPass = 0; lastLap = 0; bestLap = 0;
    display.showNumberDec(0, true);
    delay(300);
  }

  if (triggered) {
    noInterrupts();
    unsigned long t = triggerAt;
    triggered = false;
    interrupts();
    if (lastPass == 0) {
      lastPass = t;                           // first pass starts the clock
    } else if (t - lastPass > MIN_LAP_MS) {
      lastLap = t - lastPass;
      lastPass = t;
      if (bestLap == 0 || lastLap < bestLap) bestLap = lastLap;
      showBest = false;
      showSwitch = millis();
      showSeconds(lastLap);
    }
  }

  // After a lap, alternate between the last lap and the best lap every 2 seconds
  if (lastLap > 0 && millis() - showSwitch > 2000) {
    showSwitch = millis();
    showBest = !showBest;
    showSeconds(showBest ? bestLap : lastLap);
  }
}

Test checklist

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

CheckExpected
First passNo time shown, clock starts
Second pass after 5 secondsAbout 5.00 shown
Faster third lapBest lap updates
Two triggers within 1.5 secondsCounted once
Reset pressedZeroes

If it does not work

Never triggers

Turn the sensitivity screw and check the OUT wire.

Triggers constantly

Aim away from reflective surfaces and lower the sensitivity.

Display blank

Check CLK and DIO and the library.

Times look doubled

Two triggers per lap, raise MIN_LAP_MS.

Wrong decimal point

Change the 0b01000000 colon mask for your display.

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

Ask Lark AI

Common questions

What parts do I need for the toy track lap timer?

Arduino Nano, IR obstacle avoidance sensor module, TM1637 4-digit display, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the toy track lap timer take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the toy track lap timer works?

First pass: No time shown, clock starts. Second pass after 5 seconds: About 5.00 shown. Faster third lap: Best lap updates.

Never triggers: what should I check?

Turn the sensitivity screw and check the OUT wire.

Triggers constantly: what should I check?

Aim away from reflective surfaces and lower the sensitivity.

Display blank: what should I check?

Check CLK and DIO and the library.

Times look doubled: what should I check?

Two triggers per lap, raise MIN_LAP_MS.