Learn

An Arduino line follower robot, sensor to steering

Intermediate

Or see every lesson.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: a line-following cartlive0.000 s 0.00x
Click to open it in the editor
A 5-channel line sensor on A0 to A4 and two motors on a TB6612FNG, on a simulated ATmega328P. Drag the tape across under the sensor.

A line follower is the first robot most robot car kits build. A row of sensors under the front looks at the floor, the Arduino works out where a strip of black tape is, and it slows one wheel and speeds up the other to keep the tape in the middle. It is a small, complete lesson in sensing, deciding and driving.

The circuit above is an Uno with a five-channel line sensor on A0 to A4, a TB6612FNG motor driver, two gearbox motors and a pack of two 18650 cells. The cart does not move on the screen, so the floor moves instead: drag the slider on the sensor to slide the tape left or right, and watch the two wheels change speed. Click the swatch to lay a black bar across the track, and the cart stops at the finish.

What you need

  • An Arduino Uno
  • A 5-channel line tracking sensor board (five TCRT5000 sensors)
  • A TB6612FNG motor driver
  • Two 6 V gearbox motors on a two-wheel chassis
  • A battery pack for the motors (two 18650 cells here, 8.4 V when full)
  • Jumper wires and black electrical tape on a light floor

Wiring

Part and pinGoes toNote
Sensor S1 (left) to S5 (right)A4, A3, A2, A1, A0High over the black line
Sensor VCC and GND5 V (IOREF) and GND
TB6612 PWMA, AIN1, AIN2Pins 5, 2, 4The left motor
TB6612 PWMB, BIN1, BIN2Pins 6, 7, 8The right motor
TB6612 STBYPin 9Low puts the driver to sleep
TB6612 VCC5 VThe logic supply
TB6612 VMBattery +The motor supply
All groundsGNDBattery, driver and Uno joined

Reading the line with five sensors

Each sensor is an infrared LED and a light sensor pointing down. White floor reflects the light and black tape soaks it up, and a comparator on the board turns that into a plain digital signal: high over the black line, low over white. So the sketch reads the five channels with digitalRead(), even though they are wired to the analog pins.

The sensors are 15 mm apart. The sketch gives each one a place, from -30 mm for S1 on the left to +30 mm for S5 on the right, and averages the places of the sensors that see the tape. Under the middle sensor the line is at 0 mm. Between S3 and S4, both see it, and the average is 7 mm. That gives a position finer than the sensor spacing.

Steering: P and D

The position is the error: how far the line is from the middle. Both wheels start at a base speed of 120 out of 255. The sketch adds a turn to the left wheel and takes it off the right. The turn is KP times the error plus KD times how much the error changed since last time. KP pulls the cart back toward the line; KD damps the swing so it does not weave.

With the line under the middle sensor, the serial monitor says "line 0 mm left 120 right 120". Slide the tape to 15 mm right and it settles at "line 15 mm left 165 right 75": the left wheel faster, so the cart turns right, back over the line. Slide it under S1 and it says "line -30 mm left 30 right 180".

The main loop, from the line-following cart
void loop() {
  if (finished) return;

  int seen = 0;
  int sum = 0;
  for (int i = 0; i < 5; i++) {
    if (digitalRead(SENSORS[i]) == HIGH) {
      seen++;
      sum += (i - 2) * 15;
    }
  }

  if (seen == 5) {
    brake(leftMotor, rightMotor);
    finished = true;
    Serial.println("finish bar: stopped");
    return;
  }

  int left;
  int right;
  if (seen == 0) {
    // Lost it: turn toward where it was last.
    left = lastError >= 0 ? SEARCH : -SEARCH;
    right = -left;
    leftMotor.drive(left);
    rightMotor.drive(right);
    report("searching, last", lastError, left, right);
    delay(10);
    return;
  }

  int error = sum / seen;
  int turn = KP * error + KD * (error - lastError);
  lastError = error;
  left = clampSpeed(BASE + turn);
  right = clampSpeed(BASE - turn);
  leftMotor.drive(left);
  rightMotor.drive(right);
  report("line", error, left, right);
  delay(10);
}

Losing the line, and the finish bar

On a sharp bend the tape can slip past the outside sensor, and then no sensor sees anything. The sketch remembers the last error and spins toward it: one wheel forward at 110, the other back at 110, until a sensor finds the tape again. Drag the tape off the left edge and the serial monitor says "searching, last -30 mm left -110 right 110".

All five sensors black at once means a bar across the track. The sketch calls brake() on both motors and prints "finish bar: stopped".

Motors and power

The TB6612FNG is an H-bridge for two motors. For each one, two pins set the direction and a PWM pin sets the speed, and STBY has to be high or nothing moves. The SparkFun TB6612 library wraps that up: drive(150) runs a motor forward at 150, drive(-150) runs it backward, and brake() stops it.

Two full 18650 cells give 8.4 V and these motors are rated for 6 V, so the sketch never asks for more than 180 out of 255, about 5.9 V on average. In this circuit the Uno runs from USB; on a real cart the same pack would feed the Uno’s VIN pin through a switch.

Mokxi models the driver and the motors, but not the cart moving across a floor, so there is no momentum carrying it off a bend and no grip to lose. Those are what you tune KP, KD and the base speed for on a real track.

Common mistakes

Steering the wrong way. With the line to the right, the left wheel must run faster. If your cart turns away from the line, swap the plus and minus in the two speed lines, or swap one motor’s wires.

Reading the sensor upside down. On this board high means black. Some boards are the other way around; print the five readings first and check.

Mounting the sensors too high. A TCRT5000 needs to be a few millimeters above the floor. Past about a centimeter it cannot tell black from white.

Forgetting STBY. With STBY low or unconnected the driver sleeps and the motors never turn.

No shared ground between the battery, the driver and the Uno.

Too much speed. A cart that overshoots every bend needs a lower base speed or more KD before it needs a better sensor.

Questions

How does a line follower robot work?

Sensors under the front tell black tape from a light floor. The Arduino works out how far the tape is from the middle and slows the wheel on the side the tape is on, so the robot turns back over it.

How many sensors does a line follower need?

Two can follow a gentle line. Five, as here, give a position instead of just left or right, which allows smooth steering, and let the robot see a finish bar across the track.

What is PID in a line follower?

Proportional, integral and derivative: three ways of turning the error into a steering correction. This robot uses P and D. The P term turns harder the farther the line is; the D term damps the turn when the error is already shrinking.

Can I use an L298N instead of a TB6612FNG?

Yes, the wiring and code are nearly the same: two direction pins and a PWM pin per motor. The L298N loses about 2 volts in its transistors, so the motors get less of the battery. Mokxi has both drivers.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.