An autonomous robot that follows a black line on a white track using infrared reflectance sensors and differential drive. This repo contains two control approaches:
- Basic 2-sensor bang-bang controller:
main.ino(quick start) - Advanced 5-sensor PID controller:
main_pid.ino(smooth, fast, handles sharp turns and gaps)
Infrared sensors emit IR light and measure reflectance from the surface. White reflects more, black reflects less. The controller estimates the line position and adjusts motor speeds to keep the robot centered.
- MCU: Arduino Uno (ATmega328P) or Nano
- Motor driver: L298N (or L293D; wiring identical PWM/IN logic)
- Motors: 2× TT/DC gear motors with wheels
- Sensors:
- Basic: 2× digital IR line sensors (left/right)
- Advanced: 5× analog IR reflectance sensors (array)
- Power: 2S Li-ion/LiPo (7.4V) or 6×AA pack; separate motor/logic rails recommended
- Chassis: lightweight acrylic or 3D-printed; ball caster
- Misc: jumpers, spacers, switch, battery holder
Tip: For competitions, prefer a 5–8 sensor analog array and a rigid, light chassis.
ENABLE_LEFT_MOTOR→ L298N ENA (PWM) → Arduino D5LEFT_MOTOR_PIN1/PIN2→ L298N IN1/IN2 → Arduino D9/D10ENABLE_RIGHT_MOTOR→ L298N ENB (PWM) → Arduino D6RIGHT_MOTOR_PIN1/PIN2→ L298N IN3/IN4 → Arduino D7/D8- Motor outputs: L298N OUT1–OUT2 → left motor, OUT3–OUT4 → right motor
- Power: Motor VIN (battery), +5V logic to Arduino 5V, common GND
- Basic (digital):
- Right sensor DO → Arduino D11 (
IR_SENSOR_RIGHT) - Left sensor DO → Arduino D12 (
IR_SENSOR_LEFT) - VCC 5V, GND common; trim pots adjust thresholds
- Right sensor DO → Arduino D11 (
- Advanced (analog 5×):
- S0..S4 → Arduino
A0..A4(SENSOR_0..SENSOR_4) - VCC 5V, GND common
- S0..S4 → Arduino
File: main.ino
Behavior:
- Both white → drive straight
- Right sees black → steer right
- Left sees black → steer left
- Both black → stop (e.g., junction/finish)
Tweak MOTOR_SPEED to set base speed.
File: main_pid.ino
Features:
- Automatic sensor calibration on startup (3s)
- Weighted average line position (0..4000)
- PID correction for smooth, fast tracking
- Line-loss handling that steers back towards last seen direction
Key parameters:
BASE_SPEED(default 150),MAX_SPEED(255)- PID gains:
Kp,Ki,Kd CALIBRATION_TIME_MS(default 3000)
Upload either sketch as-is depending on your hardware.
- Start with
Ki = 0, moderateBASE_SPEED(120–160). - Increase
Kpuntil it oscillates, then back off ~20%. - Raise
Kdto damp oscillations and improve cornering. - Add small
Kionly if there’s consistent bias/drift.
Symptoms:
- Wobble → decrease
Kpor increaseKd - Slow response → increase
Kp - Corner overshoot → increase
Kdor reduceBASE_SPEED
- Assemble hardware per wiring above.
- Place robot over the track; ensure strong contrast (matte black line on matte white).
- Upload
main.inofor a quick test, thenmain_pid.inofor competition performance. - With PID sketch, on power-up it calibrates for ~3s. Slowly move the robot across the line during this period.
- Adjust gains and
BASE_SPEEDto your track.
main.ino— basic digital 2-sensor controllermain_pid.ino— advanced analog 5-sensor PID controllerREADME.md— documentation, wiring, tuning
- Robot veers consistently: check sensor heights (2–3 mm), parallelism, and add
Kislightly. - Random jitter: ensure clean power, common ground, decouple with 100 µF near driver.
- Misses sharp turns: increase
Kd, or raiseBASE_SPEEDslightly for better angular authority. - Loses line on gaps: ensure line contrast; the sketch biases towards last known direction.
MIT