How to Build a Line Follower Robot: Parts and Steps
A line follower robot reads the contrast between a dark line and a light floor with infrared sensors, then steers its two drive wheels so the sensors stay over the line. Building one takes a microcontroller, two to five sensors, a motor driver, two geared motors and a battery. The harder part is the tuning, which is where most first attempts go wrong.
This walkthrough covers the parts, the wiring logic, the steering code and the tweaks that separate a robot that wobbles off the track from one that finishes it.
What you need
- An Arduino Uno or Nano (any board with enough digital or analog pins works)
- Two to five reflective IR sensor modules, such as the common TCRT5000 boards
- A dual H-bridge motor driver, for example an L298N or a smaller TB6612-type board
- Two DC gear motors with wheels, plus a caster or ball wheel at the front or back
- A chassis (acrylic, a cut piece of foam board, or a 3D-printed plate)
- A battery pack, with a separate regulated supply for the logic if the motors are noisy
- A track: black electrical tape, about 2 to 3 cm wide, on a matte white board
How the sensing works
An infrared reflective sensor has an emitter and a receiver side by side. A white surface bounces most of the light back, a black one absorbs it, so the receiver output changes between the two. Most modules have a small potentiometer that sets the threshold at which the digital output flips.
Two sensors are enough for a basic robot: one on each side of the line. With five sensors you can estimate where the line is, not just whether you have drifted, and that makes smooth steering possible. Mount them 5 to 15 mm above the floor, in front of the axle, and keep them at the same height. Uneven mounting is a common reason for a robot that “pulls” to one side.
Step 1: Assemble the chassis
Fix the motors so the wheels are parallel and the robot rolls straight when both motors get equal power. Put the heavy battery low and near the drive wheels. Place the caster at the opposite end from the sensors so it does not catch on the tape.
Step 2: Wire the electronics
- Connect the motor outputs to the driver’s two output terminals.
- Connect the driver’s input pins to Arduino pins, using two PWM-capable pins for the speed (enable) lines.
- Power the driver from the battery and join the grounds of the battery, driver and Arduino. A missing common ground is the classic “nothing moves” fault.
- Connect each sensor’s VCC and GND, and its output to a digital input.
Check the motor direction with a short test sketch before adding the line logic. If a wheel spins backward, swap that motor’s two wires.
Step 3: Write the steering logic
For a two-sensor robot the rule set is short:
- Both sensors see white: drive forward.
- Left sensor sees black: the line has moved left, so slow or stop the left motor and turn left.
- Right sensor sees black: mirror the above.
- Both see black: a junction or a stop mark, handled however your track needs.
A skeleton looks like this:
const int leftSensor = 2;
const int rightSensor = 3;
void loop() {
bool left = digitalRead(leftSensor); // HIGH = black on many modules
bool right = digitalRead(rightSensor);
if (!left && !right) forward();
else if (left && !right) turnLeft();
else if (!left && right) turnRight();
else stopMotors();
}
Whether black reads HIGH or LOW depends on the module, so print the values to the serial monitor and confirm before trusting the logic.
Step 4: Calibrate and tune
Set each sensor’s threshold on the actual track under the actual lighting. Strong sunlight carries infrared and can wash out the readings, so a shaded indoor test is more reliable at first.
If the robot zigzags, the turns are too sharp or the speed too high. Lower the base speed, and make corrections gentler by slowing one wheel rather than stopping it. Once that works, a five-sensor array with a proportional controller (the correction grows with the error) gives smoother lines. Adding derivative and integral terms makes it a full PID controller, which handles tight curves at higher speed, but only after the proportional version is stable.
Common mistakes
- Sensors too high, so the contrast between line and floor is weak.
- Too little track width between the sensors and the line edge, so the robot loses the line before it reacts.
- Motors of unequal speed, which needs a small compensation value in code.
- Battery voltage sagging mid-run, which changes the motor speed. Test with a freshly charged pack.
Frequently asked questions
Can I build one without a microcontroller?
Yes. Simple analog versions use comparators or transistors to switch the motors directly from the sensor output. They are limited to basic left-right correction and are harder to adjust, so a microcontroller is easier to learn from.
How many sensors do I need?
Two are enough to follow a gentle line. Five or more let the robot estimate the line position and handle sharp curves, crossings and speed, which matters in contests.
Why does my robot lose the line on curves?
Usually the speed is too high for the sensor spacing, or the correction is too abrupt. Reduce speed, move the sensors forward of the axle, and use proportional steering.
Line followers are a staple of student contests; the robotics events described on the robotics contest page, the Autobots robot contests and the radio-controlled monster truck event show the kind of challenges this skill leads to.