TL;DR:
Two infrared sensors straddle a dark line. Each one answers a yes or no question: am I over the line?
Two yes/no answers give four cases, and each case maps to one motor action. That table is the whole program.
Speed is the main thing to tune. Too fast and the robot leaves the line before its code can react.
A line follower is the classic first autonomous robot because the whole problem fits on one page. A strip of dark tape runs across a light floor. The robot has to stay on it. There is no map, no math beyond comparing two numbers, and you can see at a glance whether it works.
What you will build
You will build a small two-wheel robot with two downward-facing infrared reflectance sensors, one on each side of the line. The robot drives forward when the line is between the sensors, and steers back toward it when either sensor touches it.
The idea is a loop: read the sensors, pick one of four actions, drive the motors, repeat. The loop should run hundreds of times per second, which is easy for a basic microcontroller board.
Parts and tools
A two-wheel chassis with two small DC gear motors and a caster or ball wheel at the back.
A microcontroller board you can program from a laptop (an Arduino-style board is used here).
A dual H-bridge motor driver, because the board's pins cannot power motors directly.
Two infrared reflectance sensors with a digital output, or analog ones read with a threshold.
A battery pack that matches your motors and driver, with a switch.
Black electrical tape, a light floor or white poster board, jumper wires, a small screwdriver.
Wiring and assembly
Mount the sensors on the front, pointing down, a few millimeters above the floor (your sensor's data sheet gives its best height). Space them wider than the tape. Say your tape is 20 mm wide and the sensors are 30 mm apart. Then when the robot is centered, each sensor sits 15 mm from the middle, which is 5 mm outside the tape edge, so both read the light floor.
Power the logic and motors separately: Motors cause electrical noise and big current spikes. Give the motor driver the battery pack directly and give the board its own supply or the regulated feed your kit describes. Join all the grounds together.
Connect the driver: Two direction pins and one speed (PWM) pin per motor go from the board to the driver. Choose PWM-capable pins for speed. The motor wires go to the driver outputs.
Connect the sensors: Each sensor needs power, ground and one signal wire. Put the left sensor on one input pin and the right on another, and write down which is which.
Check the wheels before the floor: With the robot lifted off the table, make both wheels spin forward from a test sketch. If one spins backward, swap that motor's two wires.
The logic: two sensors, four cases
Call a sensor 1 when it sees the dark line and 0 when it sees the floor. Two sensors give 2 × 2 = 4 combinations, and each one has an obvious meaning.
Left | Right | What it means | Action |
|---|---|---|---|
0 | 0 | Line is between the sensors | Drive straight |
1 | 0 | Line has drifted under the left sensor | Steer left |
0 | 1 | Line has drifted under the right sensor | Steer right |
1 | 1 | Both see dark: a crossing or stop mark | Stop |
If the line is under the left sensor, the robot has drifted to the right of the line, so it must turn left to bring the line back to the middle. That is the part beginners most often flip. If your robot runs away from the line, the table is probably mirrored, or the sensor pins are swapped.
The code, step by step
The program has a helper that sets both motor speeds and a loop that turns the table into calls. Speeds run from 0 to 255 in this style of board, where 255 is full power.
const int LEFT_SENSOR = 2;
const int RIGHT_SENSOR = 3;
const int BASE = 150; // cruising speed, 0 to 255
const int SLOW = 60; // inner wheel while turning
void setMotors(int left, int right) {
// write direction pins and PWM speed for your driver here
}
void setup() {
pinMode(LEFT_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
}
void loop() {
bool l = digitalRead(LEFT_SENSOR) == HIGH; // true = sees the line
bool r = digitalRead(RIGHT_SENSOR) == HIGH;
if (!l && !r) setMotors(BASE, BASE); // straight
else if (l && !r) setMotors(SLOW, BASE); // steer left
else if (!l && r) setMotors(BASE, SLOW); // steer right
else setMotors(0, 0); // stop
}Many reflectance modules output HIGH over the floor and LOW over dark tape, or the reverse. Print the two values to the serial monitor while you slide the robot over the tape and flip the comparison if needed. Do this before you debug anything else.
Test, measure and fix
Start on a long gentle curve, not a sharp corner. Set BASE low, for example 100. Place the robot on the line and switch it on.
Runs straight off the line: the table is mirrored or a sensor reads inverted. Check the serial values.
Wobbles left and right along the line: the correction is too strong. Raise
SLOWso the inside wheel slows less, for example from60to90.Leaves the line on curves: the robot is too fast for the response, or the correction too weak. Lower
BASEfirst.
To tune speed, raise BASE in steps of about 20 and run the same track each time. Write down the highest value where it finishes three laps in a row, then run 20 below that. A margin matters, because a battery that is nearly flat makes the robot slower, and tape corners are not all identical.
Make it better
The four-case logic only knows 'left', 'right' and 'fine', so it corrects in jerks. Add more sensors (five in a row is common) and you can tell how far off the line you are. Use that distance to steer by a proportional amount: the further off, the harder the turn. The same idea is the first step toward a PID controller, which the related PID article explains.
You can also use the both-dark case for something other than stopping, such as counting crossings so the robot turns at the third junction.
Stay safe
Before you power it: Use only low-voltage battery packs designed for your kit, never mains power. Check for loose wires touching each other, and switch off before changing wiring. Motor drivers can get warm under load, so do not hold a stalled wheel. Keep fingers clear of the wheels while the robot is on.
Frequently asked questions
Why does my robot work in the classroom but not in daylight?
Sunlight contains a lot of infrared, which can swamp the sensor's own infrared light. Shade the sensors with a little skirt of card or test indoors.
Can I use a camera instead of two sensors?
Yes, but it needs image processing and a stronger computer. Two sensors teach the control idea with much less to go wrong.
