Introduction
Build an autonomous robot that follows a black line using IR sensors and PID control algorithm. This comprehensive guide covers everything from design through implementation, testing, and deployment.
Build an autonomous robot that follows a black line using IR sensors and PID control algorithm.
Build an autonomous robot that follows a black line using IR sensors and PID control algorithm. This comprehensive guide covers everything from design through implementation, testing, and deployment.
TCRT5000 sensors emit infrared light and measure reflected intensity. White surface reflects IR strongly (low ADC value), black line absorbs IR (high ADC value or digital LOW). Mount five sensors in a row below the chassis front, spaced 15mm apart. Sensor 3 (center) detects line when robot is correctly positioned. Sensors 1-2 (left) and 4-5 (right) detect deviations. Calibrate in your operating environment — ambient light affects readings significantly.
10 components required for this project.
| # | Component | Purpose | Qty |
|---|---|---|---|
| 1 | Arduino Uno R3 | Main microcontroller | x1 |
| 2 | IR Sensor Module (TCRT5000) | Line detection array | x5 |
| 3 | L298N H-Bridge Motor Driver | Dual DC motor control | x1 |
| 4 | DC Gear Motors with Wheels (12V, 100RPM) | Drive wheels | x2 |
| 5 | Ball Caster Wheel | Front support wheel | x1 |
| 6 | 7.4V 2S LiPo Battery (2200mAh) | Power supply | x1 |
| 7 | Robot Chassis (acrylic) | Mechanical platform | x1 |
| 8 | 10kΩ Potentiometers | Sensor sensitivity adjustment | x2 |
| 9 | 220Ω Resistors | IR LED current limiting | x5 |
| 10 | Power Switch | Main power cutoff | x1 |
Follow these 7 steps carefully.
TCRT5000 sensors emit infrared light and measure reflected intensity. White surface reflects IR strongly (low ADC value), black line absorbs IR (high ADC value or digital LOW). Mount five sensors in a row below the chassis front, spaced 15mm apart. Sensor 3 (center) detects line when robot is correctly positioned. Sensors 1-2 (left) and 4-5 (right) detect deviations. Calibrate in your operating environment — ambient light affects readings significantly.
Mount motors on chassis rear with wheels. Install ball caster at front center. Solder motor wires securely — vibration causes poor connections to fail. Mount IR sensor array on a front-bottom bracket 5–8mm above the floor. Battery should be center-mounted for balanced weight distribution. Run all wiring through cable ties to prevent entanglement in wheels.
Start with simple bang-bang control: read 5 sensors. If center sensor sees line → go straight. If left sensors see line → turn right. If right sensors see line → turn left. If no sensor sees line → stop (line lost). This works but causes jerky, oscillatory motion especially at speed. Test on a white surface with 20–25mm wide black tape track.
Assign weights to sensors: positions [-2, -1, 0, 1, 2] for sensors [S1, S2, S3, S4, S5]. Error = sum(sensor_value × weight) / sum(sensor_values). This gives a continuous error value (-2 to +2) rather than discrete states. Negative error = line is left → steer left. Positive error = line is right → steer right. This smooth error signal is ideal input for PID controller.
Proportional: correction = Kp × error. Integral: correction += Ki × error × dt (eliminates steady-state offset). Derivative: correction += Kd × (error - prev_error)/dt (reduces overshoot). Motor speeds: LEFT = base_speed + correction, RIGHT = base_speed - correction (clamped 0–255). Start tuning: Kp=30, Ki=0, Kd=10. Increase Kp until oscillation, add Kd to dampen, add small Ki if needed.
With good PID tuning, the robot can navigate at higher speeds. Implement adaptive speed: when error is large (sharp curve), slow down (reduce base_speed). When error is near zero (straight), increase speed. This variable speed approach improves lap times on complex tracks by 30–40%. Set maximum speed limit to prevent motor stall on sharp turns where one wheel needs to reverse.
Test on various track features: straight sections (verify no drift), 90° corners, 180° U-turns, crossings (T and + intersections), and curved sections. Log sensor readings, error, and PID output to Serial Monitor at low speed. Common issues: sensor crosstalk (sensors too close), wheel slip (check floor surface/wheel rubber), PID windup on lost-line recovery, and insufficient torque at low PWM from motor startup friction.
Core code for line_follower_pid.ino:
#define S1 A0 // Leftmost
#define S2 A1
#define S3 A2 // Center
#define S4 A3
#define S5 A4 // Rightmost
#define IN1 7 #define IN2 8 #define ENA 9
#define IN3 10 #define IN4 11 #define ENB 5
float Kp=35, Ki=0.05, Kd=25;
float error=0, prev_error=0, integral=0;
int BASE = 150;
float readError() {
int s[5];
for(int i=0;i<5;i++) s[i] = 1023 - analogRead(A0+i); // Invert: black=high
float sum=0, wSum=0;
int weights[] = {-2,-1,0,1,2};
for(int i=0;i<5;i++) { sum += s[i]; wSum += s[i] * weights[i]; }
return (sum > 50) ? wSum/sum : prev_error; // Hold last error if line lost
}
void setMotors(int L, int R) {
L = constrain(L, -255, 255); R = constrain(R, -255, 255);
digitalWrite(IN1, L>0); digitalWrite(IN2, L<0); analogWrite(ENA, abs(L));
digitalWrite(IN3, R>0); digitalWrite(IN4, R<0); analogWrite(ENB, abs(R));
}
void loop() {
error = readError();
integral = constrain(integral + error, -100, 100);
float correction = Kp*error + Ki*integral + Kd*(error-prev_error);
prev_error = error;
setMotors(BASE + correction, BASE - correction);
delay(5);
}
Test Line Following Robot by verifying each subsystem individually before full integration.
Verify power voltages, check ground connections, use serial monitor for debug.
An interactive simulator will be available here — simulate circuits and run code in-browser without hardware.