What You'll Build
In this project you'll build a small wheeled robot that follows a line drawn on the floor. Using a pair of infrared sensors mounted beneath the chassis, the robot continuously reads the surface beneath it and adjusts its motor speeds to stay on the line — automatically, without any remote control.
It's a deceptively simple project that introduces one of the most important ideas in robotics: feedback. The robot doesn't just execute a fixed sequence of movements. It reads its environment, compares what it sees to what it wants, and corrects its behavior accordingly. That loop — sense, compare, act — is the foundation of almost every autonomous system ever built.
What You'll Learn
By completing this project you'll gain hands-on experience with several foundational robotics concepts:
- Infrared sensors — how reflectance-based sensors detect surface contrast
- Differential drive — how a two-wheeled robot steers by varying motor speeds
- Feedback control — using sensor readings to continuously correct behavior
- Microcontrollers — reading sensors and controlling motors from embedded code
- Calibration — tuning sensor thresholds and motor responses for reliable performance
- Autonomous behavior — a robot that makes decisions without human input
These concepts appear throughout robotics at every level of complexity. A line-following robot is one of the best ways to encounter them all in a single, tangible project.
Components You'll Need
This project uses common, widely available components. The exact models you choose will depend on your budget and what's available to you — the concepts apply regardless of brand.
- Microcontroller board (with digital and analog I/O pins)
- Two-wheel robot chassis kit
- Two DC gear motors (usually included with the chassis)
- Motor driver module
- IR reflectance sensor module (two sensors, or a multi-sensor array)
- Battery holder and appropriate batteries
- Caster wheel (for balance at the rear)
- Jumper wires
- Mounting hardware
- Black electrical tape or a printed line track (for the test surface)
Before purchasing, verify that your microcontroller's operating voltage is compatible with your motor driver and sensors. Most beginner-friendly components operate at 3.3V or 5V logic — check the datasheets.
How a Line-Following Robot Works
Before building, it helps to understand the system architecture. A line-following robot is a closed-loop control system:
IR SENSORS
↓
MICROCONTROLLER
↓
COMPARE TO TARGET
↓
MOTOR DRIVER
↓
LEFT MOTOR + RIGHT MOTOR
↓
(robot moves, sensors read new position)
↓
(loop repeats)
The key word is closed. The robot's output — its movement — changes the input it receives from the sensors. This feedback loop is what makes the robot self-correcting.
Infrared Sensors
Each IR reflectance sensor has an infrared LED and a photodetector. The LED emits infrared light downward onto the surface. The photodetector measures how much light bounces back.
Dark surfaces (like a black line) absorb most of the infrared light — the sensor reads low reflectance. Light surfaces (like white paper or a light floor) reflect most of the light — the sensor reads high reflectance. This contrast is what the robot uses to detect the line.
With two sensors mounted side by side at the front of the chassis:
- Both sensors over the light surface: robot is on track, drive straight
- Left sensor over the line: robot has drifted right, turn left to correct
- Right sensor over the line: robot has drifted left, turn right to correct
- Both sensors over the line: robot is centered on a wide line or at a junction
Differential Drive
A two-wheeled robot steers by running its motors at different speeds. To turn left, slow down the left motor and speed up the right. To turn right, do the opposite. To drive straight, run both motors at the same speed.
This is called differential drive, and it's the simplest and most common drive system for small mobile robots. The line-following logic maps directly onto it: when the robot drifts off the line, adjust the motor speeds to steer back.
The Feedback Loop
The microcontroller runs a continuous loop: read the sensors, decide what correction is needed, set the motor speeds accordingly. This happens many times per second. The faster the loop runs and the more precisely the motor speeds are adjusted, the smoother the robot follows the line.
A simple two-sensor system uses binary logic: the sensor is either over the line or it isn't. A more sophisticated system uses a sensor array and proportional control — the further the robot is from the line, the stronger the correction. You'll start with the simple version and can upgrade later.
Build the Chassis
Assembly follows the same basic steps as any two-wheel chassis build:
- Attach the two gear motors to the motor mounts on the chassis platform.
- Press or screw the drive wheels onto the motor shafts.
- Mount the caster wheel at the rear for balance.
- Mount the microcontroller and motor driver on the chassis platform using standoffs or foam tape.
- Mount the IR sensors at the front of the chassis, facing downward, positioned so they sit approximately 5–10mm above the surface when the robot is on flat ground. The sensors should be spaced so that when the robot is centered on the line, one sensor is just to the left of the line and one is just to the right.
- Secure the battery holder in a balanced position.
Sensor height and spacing are important. Too high and the sensors lose contrast sensitivity. Too low and they may scrape the surface. Too far apart and the robot loses the line on gentle curves. Too close together and it can't detect drift early enough to correct smoothly.
Connect the Electronics
The wiring relationships are the same as for any two-wheel robot with sensors added.
Power
Your battery supplies power to the motor driver (which powers the motors) and to the microcontroller (which powers the logic and sensors). Check your motor driver's documentation — many include a regulated 5V output you can use to power the microcontroller and sensors.
IR Sensors to Microcontroller
Each IR sensor module has at least three pins: power, ground, and output. The output pin goes to a digital input pin on your microcontroller. Some modules also provide an analog output that gives a continuous voltage proportional to reflectance — this is useful for more advanced control but not required for a basic build.
Most IR sensor modules include a small potentiometer that lets you adjust the detection threshold. You'll use this during calibration.
Microcontroller to Motor Driver
Connect the motor driver's control inputs to digital output pins on your microcontroller. You'll need direction pins and PWM speed pins for each motor channel. Refer to your motor driver's datasheet for the exact pinout.
Motor Driver to Motors
Connect each motor's two terminals to the corresponding output terminals on the motor driver. Swapping these connections reverses the motor's direction.
Always verify voltage and current ratings before connecting power. Check the datasheet for each component.
Programming the Robot
The core program is a continuous loop that reads the sensors and sets motor speeds. Here it is in pseudocode:
LOOP
read left sensor
read right sensor
IF left sensor sees line AND right sensor does not
turn left (slow left motor, speed up right motor)
ELSE IF right sensor sees line AND left sensor does not
turn right (slow right motor, speed up left motor)
ELSE IF neither sensor sees line
drive straight (both motors at full speed)
ELSE (both sensors see line)
stop or handle junction
END LOOP
Translate this into your microcontroller's programming language. The structure stays the same regardless of platform.
Motor Speed and Turning Behavior
The simplest implementation uses fixed motor speeds: full speed ahead when on track, one motor stopped when correcting. This works but produces jerky, oscillating behavior on curves.
A smoother approach uses proportional correction: when the robot is slightly off the line, apply a small correction. When it's further off, apply a larger one. This requires analog sensor readings or a sensor array, but produces noticeably better tracking.
Start with the simple fixed-speed version. Once it's working reliably, experiment with proportional control.
Calibration in Code
Your code will need a threshold value to decide whether a sensor reading means "over the line" or "over the surface." This threshold depends on your specific sensors, surface, and lighting conditions.
A good approach is to add a calibration routine at startup: the robot reads both sensors over the light surface and over the dark line, then sets the threshold automatically at the midpoint. This makes the robot more robust to different surfaces and lighting conditions.
Testing
Test in stages. This makes problems much easier to isolate.
Step 1 — Test the Sensors
Before connecting the motors, write a simple program that reads both sensors and prints the values to your serial monitor. Hold the robot over the light surface and over the line and confirm the readings are clearly different. Adjust the sensor threshold potentiometers if needed until the contrast is reliable.
Step 2 — Test Each Motor
Write a short program that runs each motor forward and backward for one second. Confirm each motor spins in the correct direction. Swap the motor wires at the driver output if a motor runs backward.
Step 3 — Test Straight Driving
Run both motors forward and confirm the robot drives in a straight line. Adjust PWM values if it curves.
Step 4 — Test the Correction Logic
Place the robot on the line and manually trigger each sensor condition by covering one sensor with your finger. Confirm the robot turns in the correct direction for each case.
Step 5 — Full Line Test
Place the robot on a simple oval or straight track and let it run. Start slowly. Observe where it loses the line and tune your threshold and motor speeds until it tracks reliably.
Troubleshooting
Robot Doesn't Detect the Line
Check sensor height — 5–10mm above the surface is typical. Adjust the threshold potentiometer on the sensor module. Ensure the surface has sufficient contrast (matte black tape on white paper works well; glossy surfaces can cause problems).
Robot Oscillates Wildly
The correction is too aggressive. Reduce the speed difference between the two motors during a correction turn, or slow the overall speed. Proportional control will also help significantly.
Robot Loses the Line on Curves
The robot is moving too fast for its correction loop to keep up, or the sensors are spaced too far apart. Reduce speed or move the sensors closer together.
Robot Turns the Wrong Way
Your sensor-to-correction mapping is inverted. Swap the left and right sensor assignments in your code, or swap the motor wires for one motor.
Inconsistent Behavior Under Different Lighting
Ambient light — particularly sunlight — can overwhelm IR sensors. Test in consistent lighting conditions, or implement a calibration routine that adapts to the current environment at startup.
Robot Runs Off the Track at Junctions
Your code needs to handle the case where both sensors see the line simultaneously. Decide on a behavior for this case — continue straight, stop, or turn — and implement it explicitly.
Ways to Upgrade the Project
More Sensors
Replace the two-sensor setup with a five or eight-sensor array. More sensors give you a more precise measurement of how far the robot is from the line center, enabling smoother proportional control and better handling of curves and junctions.
Proportional-Integral-Derivative (PID) Control
A PID controller is the standard approach for smooth, precise line following. It uses not just the current error (how far off the line the robot is) but also how fast the error is changing and how long it has been accumulating. Implementing a PID controller on a line follower is an excellent introduction to control theory.
Speed Optimization
Once the robot follows the line reliably at low speed, gradually increase the speed and retune the control parameters. High-speed line following is a competitive robotics discipline — the same principles apply, but the tolerances become much tighter.
Junction Handling
Add logic to detect T-junctions and crossings, and make decisions about which path to take. This introduces simple navigation and path-planning concepts.
Encoders
Add wheel encoders to measure distance traveled. This lets the robot track its position along the line and make decisions based on how far it has traveled — for example, stopping after a specific distance or counting laps.
Wireless Control
Add a Bluetooth or Wi-Fi module to switch between line-following mode and manual control from a phone or computer.
What to Learn Next
This project introduces several topics worth exploring further:
- PID control — the standard framework for feedback control in robotics and engineering
- Sensor arrays and signal processing — getting more information from multiple sensors
- Motor control and encoders — precise speed and position control
- Path planning — making decisions about routes and navigation
- ROS 2 — the professional framework for building more complex autonomous systems
- Obstacle avoidance — combining line following with distance sensing for a more capable robot
The obstacle-avoiding robot in our Projects series is a natural companion to this build — the two projects share the same chassis and motor control foundation, and combining their behaviors is a rewarding next step.
Summary
A line-following robot is one of the most instructive beginner projects in robotics because it makes feedback control tangible. You can watch the robot correct itself in real time, see what happens when the correction is too aggressive or too slow, and directly observe the relationship between sensor readings and motor behavior.
The skills you build here — reading sensors, controlling motors, writing feedback loops, calibrating thresholds, and testing systematically — are the same skills used in every autonomous robot, from a warehouse AGV to a self-driving vehicle. The scale changes. The principles don't.