What You'll Build
A line follower robot is a small vehicle that detects a black line on a white surface and follows it automatically — no remote control, no human input. It reads the ground using infrared sensors, decides which way to steer, and drives accordingly. Every decision happens in milliseconds, dozens of times per second.
This is exactly the same principle used in factory floor AGVs (Automated Guided Vehicles) that carry materials between workstations in large manufacturing plants. The warehouse robots at Amazon India's fulfilment centres follow similar logic, scaled up significantly.
Components You Need
- Arduino Uno (or ESP32)
- 2x IR sensor modules (TCRT5000-based — the common blue/green boards with a potentiometer)
- L298N motor driver module
- 2x DC gear motors with wheels (the yellow TT motors are perfect — cheap and reliable)
- Robot chassis (acrylic or 3D-printed — even cardboard works for a first attempt)
- 1x castor wheel or ball caster for the front
- 7.4V LiPo battery or 6x AA batteries (9V)
- Breadboard, jumper wires
Total component cost from local electronics shops or Robocraze/Evelta: approximately Rs. 600–900 for everything.
How IR Sensors Work
The IR sensor module has two parts: an infrared LED that emits IR light downward, and a photodiode that detects reflected IR light. Here is what happens on different surfaces:
- White surface — reflects IR light strongly back to the detector → output pin goes HIGH (1)
- Black surface (the line) — absorbs IR light, very little reflection → output pin goes LOW (0)
So the sensor outputs LOW when it is over the line and HIGH when it is off the line. Remember this — it feels backwards at first but makes sense once you wire it up.
Each IR sensor has a small blue potentiometer (trimmer). Turn it clockwise or anticlockwise while placing the sensor over the black line and then over the white surface, watching the indicator LED. Adjust until the LED clearly turns on over white and off over black. Calibration is the most important step — poorly calibrated sensors make everything worse.
Direct sunlight and strong indoor lights can confuse IR sensors — the extra IR in the environment tricks the detector. If your robot works indoors but fails near a window, ambient light is the culprit. A small cardboard shroud around the sensors blocks side light and dramatically improves reliability.
Wiring It Up
Take your time with wiring. A wrong connection here is the most common reason robots do not work on first test.
IR Sensors to Arduino
- Left sensor VCC → Arduino 5V, GND → Arduino GND, OUT → Arduino digital pin 2
- Right sensor VCC → Arduino 5V, GND → Arduino GND, OUT → Arduino digital pin 3
L298N Motor Driver
- L298N VCC → 9V battery positive (NOT Arduino 5V — the motors need their own power)
- L298N GND → battery negative AND Arduino GND (common ground is essential)
- L298N 5V output → Arduino Vin (powers Arduino from the motor battery — no second battery needed)
- ENA → Arduino pin 5 (PWM, left motor speed)
- IN1 → Arduino pin 6, IN2 → Arduino pin 7 (left motor direction)
- ENB → Arduino pin 9 (PWM, right motor speed)
- IN3 → Arduino pin 10, IN4 → Arduino pin 11 (right motor direction)
- Motor A outputs → left DC motor terminals
- Motor B outputs → right DC motor terminals
The Code
Before looking at code, understand the logic in plain English:
- Both sensors on white = robot is centered on the line = go straight
- Left sensor on black, right on white = robot has drifted right = turn left to correct
- Right sensor on black, left on white = robot has drifted left = turn right to correct
- Both sensors on black = robot is at a junction or end = stop (or choose a default)
// Line Follower Robot — Two IR Sensors + L298N
// IR sensors output LOW on black line, HIGH on white
// IR Sensor pins
const int LEFT_SENSOR = 2;
const int RIGHT_SENSOR = 3;
// L298N Motor Driver pins
const int ENA = 5; // Left motor speed (PWM)
const int IN1 = 6; // Left motor direction
const int IN2 = 7;
const int ENB = 9; // Right motor speed (PWM)
const int IN3 = 10; // Right motor direction
const int IN4 = 11;
// Speed settings (0–255)
const int BASE_SPEED = 180; // Normal forward speed
const int TURN_SPEED = 140; // Speed of the faster wheel while turning
const int SLOW_SPEED = 80; // Speed of the slower wheel while turning
void setup() {
pinMode(LEFT_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
Serial.begin(9600);
}
// Helper: drive left motor forward at given speed
void leftMotor(int speed) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, speed);
}
// Helper: drive right motor forward at given speed
void rightMotor(int speed) {
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, speed);
}
// Helper: stop all motors
void stopMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
void loop() {
int leftVal = digitalRead(LEFT_SENSOR); // LOW = on line
int rightVal = digitalRead(RIGHT_SENSOR); // LOW = on line
if (leftVal == HIGH && rightVal == HIGH) {
// Both on white — go straight
leftMotor(BASE_SPEED);
rightMotor(BASE_SPEED);
Serial.println("STRAIGHT");
}
else if (leftVal == LOW && rightVal == HIGH) {
// Left sensor on black — robot drifted right — turn left
// Slow down left motor, keep right motor going
leftMotor(SLOW_SPEED);
rightMotor(TURN_SPEED);
Serial.println("TURN LEFT");
}
else if (leftVal == HIGH && rightVal == LOW) {
// Right sensor on black — robot drifted left — turn right
// Keep left motor, slow down right motor
leftMotor(TURN_SPEED);
rightMotor(SLOW_SPEED);
Serial.println("TURN RIGHT");
}
else {
// Both on black — junction or end of line
stopMotors();
Serial.println("STOP");
}
}
Making It Better
The on/off logic above works but gives a jerky, zigzag movement on curves. The robot overcorrects, swings too far, then overcorrects the other way. This is where improvements come in.
Speed-Based Turning (Simple Improvement)
Instead of a fixed SLOW_SPEED, use the sensors to calculate how much to slow down proportionally. The farther from the line, the more aggressively you turn. This is the beginning of proportional control.
More Sensors for Smoother Curves
With 5 sensors in an array (like the TCRT5000 array module), you can detect exactly how far off-center you are — not just left/right/center. This gives you much finer position data to work with, which is essential for PID control.
Why PID Would Make This Much Smoother
PID control calculates corrections based not just on where you are now (P), but how long you have been off (I), and how fast you are drifting (D). A PID-controlled line follower glides smoothly around curves instead of zigzagging. Read the next article to understand PID and see how to apply it to this exact robot.
Add a pushbutton on pin 4 as a start/stop toggle. Press once to start the robot, press again to stop. This way you can place the robot on the line before it starts moving, and stop it cleanly. It also looks much more professional in a demo.
