Project · ESP32 · Neural network

AI line follower car

Four IR sensors look at the floor. A small neural network turns what they see into a speed for the left motor and a speed for the right. The car below is driven by the trained network from the code repository.

Sensors, left to right
0000
Motor speed
L
0
R
0
Laps0
Lost line0
Following the line
Drag to orbit · scroll to zoom
Line style
30 px · 81 mm
Speed
Camera
 

How it decides

INPUT

4 IR sensors

Each sensor reads 1 over the tape and 0 over the floor. That gives 16 possible patterns, such as 0110 when the line is in the middle.

4 → 7 → 7 → 2

Tiny network

Two hidden layers of 7 neurons. 107 numbers in total, 428 bytes, 91 multiply-adds per decision. No Jetson needed.

OUTPUT

2 motor speeds

Left and right wheel speed between 0 and 1. An ESP32 runs this 200 times per second and drives a TB6612FNG motor driver.

Where it breaks

Lost-line events in 120 sDemo loopOvalWinding
Gap 81 mm000
Gap 135 mm608
Gap 189 mm1818
Gap 243 mm291411

Try it above: set the line to dashed and drag the gap slider to the right. With all four sensors at 0 the network can only drive straight, so on a curve a long gap takes the car off the line.

What is real, and what is not yet

Simulation only

Every number on this page comes from simulation. The firmware has not run on a real car yet, so real sensors, motors and floor friction are still untested.

It copies a rule today

With 16 sensor patterns the network matches a steering rule written by hand. The plan is to train it on logged driving from the real car, with analog sensor readings.

Parts