← All projects
03 / Embedded · Sensor Fusion · Deep Learning

ML Automotive Safety: Collision Detection

A collision-warning retrofit that estimates the distance and closing speed of the vehicle ahead and turns them into a green / yellow / red alert. There are two implementations: YOLOv8 vision in Python and an ultrasonic Arduino unit, both driven by the Berkeley PATH warning model. Pitched at Campus Startup Night.

YOLOv8nvehicle detection, conf ≥ 0.45
HC-SR04ultrasonic ranging on Arduino
PATH modelkinematic warning distance
3-level alertgreen / yellow / red LEDs
Demo: the ultrasonic unit reacting as a car approaches.
Arduino Nano, HC-SR04 ultrasonic sensor and three alert LEDs on perfboard
Arduino Nano, HC-SR04 and the three alert LEDs on perfboard.
YOLOv8 detecting cars in dashcam footage with distance and speed overlays
YOLOv8 on dashcam footage: each car gets a box with its estimated distance and relative speed.
VISION PATH · yolo_rearalgo.py Cameravideo frame YOLOv8nbbox width w px Pinhole ranged = 1.8·800 / w ULTRASONIC PATH · rearalgorithm.ino HC-SR04trig D6 · echo D7 Echo timed = t·0.034/2 2 reads, 100 msv = |d₂−d₁| / 0.1 PATH warning model R = ½(v_F²/α − v_L²/α) + v_F·τ + R_min Risk ratio r = d / R LEDs D11 · D10 · D9
Both sensing paths feed the same warning model. The camera can classify what is ahead. The ultrasonic sensor gives a direct range that doesn't depend on lighting.

Try it: the PATH warning model

live calculation
SAFE

The same thresholds as the firmware: red if d/R < 0.7 and v ≥ 0.3 m/s, yellow if d/R < 0.85 and v ≥ 0.2 m/s, otherwise green. Defaults match the Arduino build (α = 6 m/s², τ = 0.7 s, Rmin = 2 m, vL = 0).

Vision: distance from a single camera

With one camera there's no stereo depth, so yolo_rearalgo.py uses the pinhole camera model. A car is about 1.8 m wide, so if its bounding box is w pixels wide and the focal length is f ≈ 800 px, the distance is:

distance = CAR_WIDTH × FOCAL_LENGTH / box_width_px

Differencing that distance between frames (Δd/Δt, using wall-clock timestamps) gives the lead vehicle's relative speed. YOLOv8n runs at a confidence threshold of 0.45, and the alert is drawn onto the video as colour-coded text.

The PATH model

The warning distance comes from the Berkeley PATH collision-warning algorithm. It adds the distance you'd cover during your reaction time to the braking-distance difference between the two vehicles, plus a minimum buffer:

Rwarning = ½·(vF²/α − vL²/α) + vF·τ + Rmin

The alert level then comes from the ratio d / R, so the system warns earlier at higher closing speeds, which is exactly when you need more time.

Embedded: the Arduino unit

rearalgorithm.ino is the hardware prototype. An HC-SR04 fires a 10 µs trigger pulse. pulseIn() measures the echo, and distance is duration × 0.034 / 2 cm (speed of sound, there and back). Two readings 100 ms apart give the closing speed.

// PATH (Berkeley) warning distance
Rwarning = 0.5 * ((vF*vF/alpha) - (vL*vL/alpha))
         + (vF * tau) + Rmin;
float ratio = distance / (Rwarning * 100);
if (ratio < 0.7 && vF >= 0.3)       red();
else if (ratio < 0.85 && vF >= 0.2) yellow();
else                                green();

Below 0.05 m/s the unit skips the calculation and stays green, so a parked car doesn't set it off. I tuned the parameters for bench testing: α = 6 m/s², and τ = 0.7 s to make the LEDs react faster.

Why two sensors? The camera understands what is ahead but its range estimate is only as good as the width assumption. The ultrasonic sensor gives accurate range at short distances but doesn't know what it's measuring. Fusing them is what makes a cheap retrofit trustworthy.