Autonomous Line-Follower Robot with 5-Channel IR Sensor Array
Build a fast line-tracking robot using a 5-sensor infrared reflectance array (TCRT5000) and tuned PID steering control in C++.
Line followers are the foundation of industrial Automated Guided Vehicles (AGVs) in Amazon warehouses. This project teaches optical reflectance sensing, digital threshold calibration, and Proportional-Integral-Derivative (PID) steering algorithms to follow high-speed curves and sharp right angles smoothly.
Bill of Materials (BOM) & Components
| Component | Specifications | Qty | Estimated Cost |
|---|---|---|---|
| 5-Channel TCRT5000 Infrared Reflectance Sensor Bar | Adjustable sensitivity potentiometers with digital & analog outputs | 1 | $5.00 |
| Micro N20 High-Speed Metal Gearmotors (6V 600RPM) | High torque density with brass gearboxes and rubber micro wheels | 2 | $9.00 |
| TB6612FNG Dual Motor Driver Board | High-efficiency MOSFET H-Bridge (low voltage drop) | 1 | $4.50 |
| Arduino Nano or ESP32 Microcontroller | Fast 1kHz PID control loop calculation | 1 | $6.00 |
| 2S 7.4V 800mAh LiPo Battery & Mini Buck Converter | Lightweight high-discharge power source | 1 | $12.00 |
Electrical & Architecture Summary
5 digital IR outputs connect to Arduino A0-A4 pins. TB6612FNG driver receives PWM on D3/D5 and direction pins on D4/D6/D7/D8. IR sensor bar is mounted ~5mm above the track surface.
Step-by-Step Assembly & Configuration
Chassis Assembly & Sensor Bar Height Tuning
Mount the N20 gearmotors near the rear axle. Fix the 5-channel IR sensor bar at the extreme front of the chassis, exactly 4mm - 7mm above the ground.
- Ensure sensor bar is completely horizontal so all 5 phototransistors receive identical reflected light.
- Use electrical black tape (19mm width) on a white poster board for the test track.
Sensor Calibration & Weighted Error Calculation
Write calibration code to assign weighted error values (-2, -1, 0, +1, +2) across the 5 sensors depending on which sensors detect the black line.
- Calculate center line position: `error = (-2*S1) + (-1*S2) + (0*S3) + (1*S4) + (2*S5)`.
- Verify error output is 0 when the robot is perfectly centered on the line.
Implementing and Tuning the PID Steering Loop
Implement the PID formula: `correction = (Kp * error) + (Kd * dError) + (Ki * integral)`. Apply `base_speed ± correction` to left and right motors.
- Start with Kp only: increase Kp until the robot follows lines with gentle hunting oscillations.
- Add Kd to dampen oscillations and handle sharp 90-degree turns smoothly.