RC Lap Timer with an Infrared Gate
An IR break-beam across the finish line records every lap to the millisecond and shows a leaderboard.
Racing friends is more fun with real numbers. When a car breaks the infrared beam, gpiozero triggers an event and the Pi records the lap time.
Mission steps
Mount the beam
Place the emitter and receiver on opposite sides of the finish line, a few centimetres above the track.
Wire the receiver
Receiver output to GPIO17 with the pull-up enabled, power from 3V3 or 5 V as the sensor requires (output must be 3.3 V safe).
Run the timer
Each break starts a new lap; a minimum lap time ignores double triggers.
Show results
Print laps to the OLED or serve a live leaderboard web page.
Race
Add a start countdown with a buzzer and an LED tree.
Code
from gpiozero import Button
from signal import pause
import time
gate = Button(17, pull_up=True)
last, laps, MIN_LAP = None, [], 2.0
def crossed():
global last
now = time.monotonic()
if last and now - last >= MIN_LAP:
laps.append(now - last)
print('Lap %d: %.3f s best %.3f s' % (len(laps), laps[-1], min(laps)))
if not last or now - last >= MIN_LAP:
last = now
gate.when_pressed = crossed
pause()Tip: test every script on the bench before installing it in a moving machine.
Questions
Multiple cars?
A single beam cannot tell cars apart. Use one lane per beam, or IR transponders on each car for real race timing.