Projects / Line follower robot

Line follower robot

A two-wheel robot follows a black line on a white floor using two IR reflective sensors and an L298N motor driver.

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. UNOROBOT

    Build the chassis

    Fit both motors, the wheels and the caster. Mount the Uno and the L298N on top with spacers.

  2. UNO

    Mount the sensors

    Fix both sensors at the front, about 1 cm above the floor and 2 to 3 cm apart, so the line (about 2 cm wide) fits between them.

  3. UNO

    Wire the motors

    Left motor to OUT1/OUT2, right motor to OUT3/OUT4. Remove the ENA and ENB jumpers and wire the driver pins to the Uno.

  4. UNO+

    Wire power

    Battery through the switch to the L298N 12V terminal and the Uno VIN. Join all grounds.

  5. UNO

    Set the sensors

    Power up, hold each sensor over white and then black tape, and turn its trimmer until the module LED changes exactly at the edge.

  6. UNO

    Upload and test

    Lift the wheels, upload, cover each sensor with black tape and check each wheel's direction. Then run it on a taped loop.

In short

How do you build a line follower robot?

A two-wheel robot follows a black line on a white floor using two IR reflective sensors and an L298N motor driver. You need Arduino Uno R3, L298N Dual H-Bridge Module, TCRT5000 IR reflective sensor module × 2, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2, Rocker switch (KCD1, 2-pin) and Jumper wires male to female (40 pcs). It takes 6 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Battery + (through the rocker switch)L298N 12V terminal and Uno VINPower for motors and board
Battery −L298N GND and Uno GNDOne shared ground
L298N OUT1 / OUT2Left motorLeft wheel
L298N OUT3 / OUT4Right motorRight wheel
L298N ENA / ENBUno D5 / D6 (remove the ENA and ENB jumpers)Speed by PWM
L298N IN1 / IN2 / IN3 / IN4Uno D7 / D8 / D9 / D10Direction
Left sensor DO / Right sensor DOUno A0 / A1Line readings (used as digital pins)
Sensor VCC / GNDUno 5V / GNDSensor power

Code

Arduino IDE, board "Arduino Uno". No extra libraries. If the robot turns away from the line, change BLACK to LOW.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int ENA = 5, IN1 = 7, IN2 = 8, IN3 = 9, IN4 = 10, ENB = 6;
const int LEFT = A0, RIGHT = A1;
const int BLACK = HIGH;        // most TCRT5000 modules output HIGH over a black line
const int SPEED = 150, TURN = 120;

void drive(int l, int r) {
  digitalWrite(IN1, l >= 0 ? HIGH : LOW); digitalWrite(IN2, l < 0 ? HIGH : LOW);
  digitalWrite(IN3, r >= 0 ? HIGH : LOW); digitalWrite(IN4, r < 0 ? HIGH : LOW);
  analogWrite(ENA, abs(l)); analogWrite(ENB, abs(r));
}

void setup() {
  int outs[] = {ENA, IN1, IN2, IN3, IN4, ENB};
  for (int p : outs) pinMode(p, OUTPUT);
  pinMode(LEFT, INPUT); pinMode(RIGHT, INPUT);
  delay(2000);                 // time to put the robot on the line
}

void loop() {
  bool l = digitalRead(LEFT) == BLACK, r = digitalRead(RIGHT) == BLACK;
  if (!l && !r) drive(SPEED, SPEED);       // line between the sensors: go straight
  else if (l && !r) drive(-TURN, TURN);     // line under the left sensor: turn left
  else if (!l && r) drive(TURN, -TURN);     // line under the right sensor: turn right
  else drive(0, 0);                         // both on black: stop at the end mark
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Both sensors over whiteBoth wheels turn forward at the same speed
Left sensor over blackLeft wheel reverses, right wheel forward: the robot turns left
Both sensors over blackBoth wheels stop
Battery voltage at the L298N7.4 to 8.4 V with fresh 18650 cells

If it does not work

A wheel turns the wrong way

Swap that motor's two wires at the L298N outputs.

The robot spins in circles

The sensor logic is inverted. Change BLACK to LOW, or adjust the sensor trimmers.

Motors hum but do not move

Raise SPEED, check the ENA/ENB jumpers are removed and the wires go to D5/D6, and charge the cells.

The Uno resets when the motors start

The cells are weak or the wiring is thin. Use good cells and a direct, short ground wire.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the line follower robot?

Arduino Uno R3, L298N Dual H-Bridge Module, TCRT5000 IR reflective sensor module × 2, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2, Rocker switch (KCD1, 2-pin) and Jumper wires male to female (40 pcs). Robot car chassis kit 2WD (acrylic): Comes with two BO motors, wheels and a caster. 18650 cell 2600 mAh (branded) × 2: Charge them with a proper Li-ion charger.

How long does the line follower robot take to build?

About 120 minutes for a intermediate maker, in 6 steps.

How do I know the line follower robot works?

Both sensors over white: Both wheels turn forward at the same speed. Left sensor over black: Left wheel reverses, right wheel forward: the robot turns left. Both sensors over black: Both wheels stop.

A wheel turns the wrong way: what should I check?

Swap that motor's two wires at the L298N outputs.

The robot spins in circles: what should I check?

The sensor logic is inverted. Change BLACK to LOW, or adjust the sensor trimmers.

Motors hum but do not move: what should I check?

Raise SPEED, check the ENA/ENB jumpers are removed and the wires go to D5/D6, and charge the cells.

The Uno resets when the motors start: what should I check?

The cells are weak or the wiring is thin. Use good cells and a direct, short ground wire.