Projects / Marble run lap timer

Marble run lap timer

Beginner1.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Make a short track

    Card or a plastic gutter with a gentle slope.

  2. NANO

    Fix the two sensors

    Start and finish, a measured distance apart.

  3. NANO

    Wire the sensors

    Start OUT to D2, finish OUT to D3.

  4. NANO15.0 mASSD1306

    Wire the OLED and button

    OLED to A4 and A5, button to D4.

  5. NANO

    Set TRACK_M

    To the measured distance in metres.

  6. NANO

    Release a marble

    The time and speed appear.

  7. NANO

    Press reset

    And try a steeper slope.

In short

How do you build a marble run lap timer?

Two light gates at the start and the finish of a marble run time each marble to a thousandth of a second, and the screen shows the best time. Change the slope or the track and see what changes. Use a marble too big to swallow, and keep it from small children. You need Arduino Nano, IR obstacle avoidance sensor module × 2, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 80 minutes.

Level
Beginner
Time
About 80 min
Parts
6

Parts list

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

Wiring

FromToWhy
Start gate sensor OUTNano D2Low when the marble passes the start
Finish gate sensor OUTNano D3Low when the marble passes the finish
Both sensors VCC and GNDNano 5V and GNDSensor power
Reset buttonNano D4 and GNDClears the last run
OLED SDA and SCLNano A4 and A5I2C data and clock
OLED VCC and GNDNano 5V and GNDPower for a 0.96 inch module

Code

Each gate is an infrared sensor module with the marble rolling in front of its beam. The two sensor outputs go to D2 and D3, the Nano's two interrupt pins, so the Nano notes the time in microseconds the instant a marble passes, whatever else it is doing. The start gate begins the timer and the finish gate stops it, and the sketch shows the time to a thousandth of a second, the speed, worked out from the distance between the gates, which you set in TRACK_M, and the best time so far. If the finish gate is never reached, the run times out after ten seconds. The reset button gets ready for the next marble. Place each sensor so the marble passes about a centimetre from it, shade them from bright light, and turn each sensor's small knob until it sees the marble but not the track. These modules take about a millisecond to react, so the times are good for comparing runs but are not precision measurements. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager. Use a marble too large to swallow, and keep marbles away from small children. Try changing the slope of the track, or the material of the track, and see which makes the marble faster.

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

// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const uint8_t START_PIN = 2;               // interrupt pins on the Nano
const uint8_t FINISH_PIN = 3;
const uint8_t BTN = 4;
const float TRACK_M = 0.50;                // distance between the two gates, in metres

enum State { READY, RUNNING, DONE };
volatile State state = READY;
volatile unsigned long t0 = 0, t1 = 0;

unsigned long bestUs = 0;
bool shown = false;
unsigned long runStarted = 0;

void startISR() {
  if (state == READY) { t0 = micros(); state = RUNNING; }
}

void finishISR() {
  if (state == RUNNING) { t1 = micros(); state = DONE; }
}

void drawReady(const char *msg) {
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print(msg);
  oled.setCursor(0, 54);
  oled.print(F("Best: "));
  if (bestUs) { oled.print(bestUs / 1000.0, 3); oled.print(F(" s")); } else oled.print(F("-"));
  oled.display();
}

void setup() {
  pinMode(START_PIN, INPUT);
  pinMode(FINISH_PIN, INPUT);
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  attachInterrupt(digitalPinToInterrupt(START_PIN), startISR, FALLING);
  attachInterrupt(digitalPinToInterrupt(FINISH_PIN), finishISR, FALLING);
  drawReady("Ready. Release marble");
}

void loop() {
  if (state == RUNNING) {
    if (runStarted == 0) { runStarted = millis(); drawReady("Running..."); }
    if (millis() - runStarted > 10000) {                 // never reached the finish: give up
      noInterrupts(); state = READY; interrupts();
      runStarted = 0;
      drawReady("Timed out. Ready");
    }
  }
  if (state == DONE && !shown) {
    noInterrupts(); unsigned long us = t1 - t0; interrupts();
    shown = true;
    runStarted = 0;
    if (bestUs == 0 || us < bestUs) bestUs = us;
    float secs = us / 1000000.0;
    oled.clearDisplay();
    oled.setTextColor(SSD1306_WHITE);
    oled.setTextSize(2);
    oled.setCursor(0, 0);
    oled.print(secs, 3);
    oled.print(F(" s"));
    oled.setTextSize(1);
    oled.setCursor(0, 24);
    oled.print(F("Speed: "));
    oled.print(TRACK_M / secs, 2);
    oled.print(F(" m/s"));
    oled.setCursor(0, 54);
    oled.print(F("Best: "));
    oled.print(bestUs / 1000000.0, 3);
    oled.print(F(" s"));
    oled.display();
    Serial.print(F("Time: "));
    Serial.println(secs, 3);
  }
  if (digitalRead(BTN) == LOW) {
    noInterrupts(); state = READY; interrupts();
    shown = false;
    runStarted = 0;
    drawReady("Ready. Release marble");
    delay(300);
  }
}

Test checklist

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

CheckExpected
Marble passes the startScreen says Running
Marble passes the finishTime and speed appear
Faster runBest time updates
No finish in ten secondsTimed out and ready
Reset buttonReady for the next marble

If it does not work

Times look far too small

A sensor is picking up the track, turn its knob down.

Never starts

Turn the start sensor's knob up and aim it at the marble's path.

Starts but never finishes

Check the finish sensor's wiring to D3.

Blank screen

Check the OLED wires and try address 0x3D.

Speed looks wrong

Check TRACK_M matches the distance between the gates.

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

Ask Lark AI

Common questions

What parts do I need for the marble run lap timer?

Arduino Nano, IR obstacle avoidance sensor module × 2, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the marble run lap timer take to build?

About 80 minutes for a beginner maker, in 7 steps.

How do I know the marble run lap timer works?

Marble passes the start: Screen says Running. Marble passes the finish: Time and speed appear. Faster run: Best time updates.

Times look far too small: what should I check?

A sensor is picking up the track, turn its knob down.

Never starts: what should I check?

Turn the start sensor's knob up and aim it at the marble's path.

Starts but never finishes: what should I check?

Check the finish sensor's wiring to D3.

Blank screen: what should I check?

Check the OLED wires and try address 0x3D.