A ball rolls through gate A and then gate B. The elapsed time and distance between detection planes give its average speed over that interval. This is a satisfying measurement, provided the project does not confuse a timer’s printed digits with the accuracy of the complete optical and mechanical system.

Make the object interrupt a real beam
IR means infrared, light outside normal human vision. Each Adafruit 2167 pair contains an emitter and powered receiver. The receiver uses an open-collector output: it can pull the signal low but needs a resistor to establish high. In this documented module, a broken beam produces a low signal. The 3 mm description identifies the emitter size, not a guaranteed 3 mm measurement aperture.
Use an opaque ball, not a clear glass marble that may transmit or redirect infrared. First pass the object slowly through each gate and verify it reliably blocks the beam. If the geometry is unreliable, use an opaque flag on a small cart and keep the same timing experiment. Mounts and beam alignment are part of the measurement, not decorative finishing touches.
Parts and equipment
- Arduino Uno R3 with ATmega328P; two Adafruit product 2167 3 mm IR emitter/receiver pairs. Use the specified modules, not bare LEDs and phototransistors.
- Two 10 kΩ pullups, breadboard, a straight track, opaque ball or opaque cart flag, ruler and secure mounts. USB cable for 5 V power and serial output.
Every connection
| Pin or part | Connection and purpose |
|---|---|
| Power | Uno 5V → red wire of both emitters and both receivers. Their black wires → Uno GND. These are complete powered modules. |
| Gate A | First receiver white/yellow signal → D2; separate 10 kΩ resistor D2 → 5 V. |
| Gate B | Second receiver white/yellow signal → D3; separate 10 kΩ resistor D3 → 5 V. |
| Geometry | Mount each emitter opposite its matching receiver, across the track. Place the two detection planes nominally 200 mm apart along travel. Aim beams through the opaque object at equal height. |
Electrical and timing scale
The specified modules accept the Uno’s 5 V arrangement. Each 10 kΩ signal pullup supplies about 0.5 mA while low. The modules already contain their operating circuitry; a bare infrared LED would need a different design. Keep the two optical paths sufficiently separated and shielded that one emitter does not confuse the other receiver. Bright sunlight can interfere with this simple optical arrangement.
The Uno R3 uses a 16 MHz ATmega328P. D2 and D3 provide the two external interrupt inputs used here. An interrupt lets the processor record a transition promptly without waiting for a serial print to finish. The handlers only capture micros() and set flags; the main loop copies multi-byte timestamps atomically, meaning an interrupt cannot split the copy into inconsistent halves.
One object, one direction, one result
The state machine first waits for both beams to remain clear for 100 ms. It then arms for gate A. Gate B arriving first is invalid; a repeated A before B is invalid; an A without a B within two seconds times out. The accepted interval is 20 ms through two seconds. These bounds are an experiment policy, not the sensor’s complete speed specification.
If both interrupt flags are waiting, the code orders their timestamps before feeding the state machine. A second unprocessed edge on one gate marks overflow and invalidates the run instead of pretending that nothing was lost. Unsigned timestamp subtraction handles a timer wrap across one short interval. The two-second limit keeps that reasoning well inside the timestamp range.
Complete program
/* Uno R3, two Adafruit 2167 receivers, A=D2 and B=D3, active LOW.
Serial 115200. Set measured spacing in metres before taking data. */
#include <stdint.h>
struct GateTimer {
uint8_t phase; /* 0 clear first, 1 armed, 2 running, 3 result, 4 invalid */
uint32_t started, elapsed;
void arm() { phase=1; elapsed=0; }
void edge(uint8_t gate, uint32_t now) {
if (phase==1) {
if (gate==0) { started=now; phase=2; }
else phase=4;
} else if (phase==2) {
if (gate==0) phase=4;
else {
elapsed=(uint32_t)(now-started);
phase=(elapsed>=20000 && elapsed<=2000000) ? 3 : 4;
}
}
}
void timeout(uint32_t now) {
if (phase==2 && (uint32_t)(now-started)>2000000) phase=4;
}
};
#ifndef TC_HOST_TEST
#include <Arduino.h>
#include <util/atomic.h>
volatile uint8_t gotA=0, gotB=0, overflowed=0;
volatile uint32_t timeA, timeB;
void onA() { if (gotA) overflowed=1; else { timeA=micros(); gotA=1; } }
void onB() { if (gotB) overflowed=1; else { timeB=micros(); gotB=1; } }
GateTimer timer={0,0,0};
const float SPACING_M=0.200f;
uint32_t clearSince=0;
bool timingClear=false;
void setup() {
pinMode(2,INPUT); pinMode(3,INPUT); // external 10k pullups
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(2),onA,FALLING);
attachInterrupt(digitalPinToInterrupt(3),onB,FALLING);
Serial.println("Clear both beams; one object, A then B.");
}
void loop() {
uint8_t a,b,over; uint32_t ta,tb;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
a=gotA; b=gotB; ta=timeA; tb=timeB; over=overflowed;
gotA=gotB=overflowed=0;
}
uint8_t before=timer.phase;
if (over && timer.phase) timer.phase=4;
else if (a && b) {
// Intervals are below 2 s, so signed subtraction orders wraparound timestamps.
if ((int32_t)(ta-tb)<=0) { timer.edge(0,ta); timer.edge(1,tb); }
else { timer.edge(1,tb); timer.edge(0,ta); }
} else if (a) timer.edge(0,ta);
else if (b) timer.edge(1,tb);
timer.timeout(micros());
if (timer.phase==3 && before!=3) {
Serial.print("dt_us="); Serial.print(timer.elapsed);
Serial.print(" average_m/s=");
Serial.println(SPACING_M*1000000.0f/timer.elapsed,3);
}
if (timer.phase==4 && before!=4) Serial.println("INVALID order / repeat / timeout / range");
bool clear=digitalRead(2) && digitalRead(3);
if (timer.phase==0 || timer.phase>=3) {
if (!clear) timingClear=false;
else if (!timingClear) { clearSince=millis(); timingClear=true; }
else if ((uint32_t)(millis()-clearSince)>=100) {
bool armed=false;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (!gotA && !gotB && digitalRead(2) && digitalRead(3)) {
overflowed=0; timer.arm(); armed=true;
}
}
timingClear=false; if (armed) Serial.println("ARMED");
}
} else timingClear=false;
}
#endif
Calculate average speed and its uncertainty
Enter the measured spacing in SPACING_M. With d = 0.200 m and elapsed time t = 0.250 s, average speed v = d/t = 0.800 m/s. The program converts microseconds to seconds through its factor of one million. If the ball accelerates, this is an average over the distance between gates, not a direct reading of its final speed.
Suppose your estimate of detection-plane spacing is 200 ± 2 mm. Distance alone contributes about 1% relative uncertainty. The product specifies response time below 2 ms; unequal detection delays and mechanical alignment matter far more than the microsecond label on the output. As a conservative illustrative budget, assigning ±4 ms to the interval gives 4/250 = 1.6% at a 250 ms run. Adding that to the 1% distance term gives roughly 2.6% by a worst-case linear estimate, before other effects. This is a proposed error budget, not calibrated accuracy.
At 20 ms, the same illustrative 4 ms term is 20%, so very fast runs are poor candidates for precise estimates with these modules. Moving the gates farther apart can improve the relative timing contribution, but may increase speed change over the measured distance. Matching optical geometry matters as much as adding more printed decimal places.
Build, check, then collect
- Wire with USB disconnected. Fix both pairs securely and inspect each signal with a simple blocked/clear test. Confirm clear reads high and your opaque object gives a solid low.
- Open the complete listing in Arduino IDE for Uno R3, compile and upload. Use Serial Monitor at 115200 baud. A target Uno toolchain was not available during local authoring; perform this target build before the bench run.
- Clear both gates and wait for ARMED. Pass a card through A then B slowly enough to observe a result within the two-second limit. Try B first and A without B; both should be rejected.
- Measure gate spacing along the object’s travel, update SPACING_M and repeat. Release one ball from the same point for at least ten runs. Record every valid interval and the rejected attempts, rather than selecting only close-looking numbers.
- Change release height while keeping the gates fixed. Compare mean interval and spread. Report the test geometry, approximate timing budget and whether the object was rolling or sliding.
Software tests exercise event ordering, repeats, timeout and timestamp wrap using the actual GateTimer code on a host. This circuit has not been physically tested; no sensor latency, timing accuracy or measured ball speed has been established. Do not put fingers or objects into an exposed high-speed mechanical rig; this experiment is intended for a small hand-released tabletop ball or cart.
Challenge: why can two identical sensors still produce a timing offset?
Their threshold, alignment, illumination and response delay need not match. Equal part numbers reduce some differences but do not replace a calibration or uncertainty estimate.
Sources and related lessons
- Adafruit 2167 exact breakbeam specifications
- Adafruit wiring and active-low behavior
- Arduino Uno R3 hardware
- Arduino attachInterrupt reference
Continue with why a voltage divider drops under load, measuring current through a resistor with a voltmeter, and the interactive circuit lessons.



