TechForLive
Robotics & DIY Electronics

Line Follower Robot Tutorial: Build One With Arduino (Circuit Diagram + Code)

Build a line follower robot step by step: parts list, wiring diagram for IR sensors and an L298N driver, tested-logic Arduino code, tuning tips and a PID upgrade.

Some links in this guide are affiliate links: if you buy, TechForLive may earn a commission at no extra cost to you. Details.

In short: a line follower is a small robot that drives along a dark line on a light floor (or the reverse). Two infrared sensors look at the floor, an Arduino decides which way to steer, and a motor driver powers two wheels. It is the classic first robot because it teaches sensors, logic and motor control in one weekend build.
  • Cost: roughly the price of an Arduino starter kit plus a robot chassis
  • Time: 3–5 hours for a first build
  • Skills: basic wiring, uploading a sketch; soldering is optional

Line followers are a textbook example of a mobile robot that navigates by following a marked path, the same idea used by some warehouse and factory robots that follow tape or magnetic strips. This guide builds the simplest reliable version first, then shows how to make it faster and smoother.

How a line follower works

An infrared (IR) sensor module has an IR LED and a phototransistor side by side. The LED shines on the floor; a white surface reflects lots of IR back, while a black line absorbs most of it. A comparator chip on the module (usually an LM393) turns that difference into a clean digital signal: one level for "line", the other for "floor". A small trimmer potentiometer sets the threshold.

With two sensors mounted either side of the line, the robot only needs four rules:

Left sensorRight sensorWhat it meansAction
FloorFloorLine is between the sensorsDrive forward
LineFloorRobot drifted rightTurn left
FloorLineRobot drifted leftTurn right
LineLineJunction, finish mark or lostStop (or follow your junction rule)

Parts list

  • Arduino Uno (or a compatible board such as a Nano)
  • 2 × IR obstacle/line sensor modules based on the TCRT5000 sensor, with digital output
  • L298N dual motor driver module (or a more efficient TB6612FNG board)
  • 2 × geared DC motors with wheels (the yellow "TT"/BO motors in most kits) and a front caster wheel
  • Chassis: a two-wheel robot car kit, acrylic plate or even stiff cardboard
  • Battery: 2 × 18650 Li-ion cells in a holder (about 7.4 V) or a 6 × AA pack (about 9 V)
  • Jumper wires, a switch, and black electrical tape for the track

Circuit diagram

Battery 7–9 Vvia ON/OFF switch L298N driver 12V in ← battery +GND ← battery − & Arduino GND5V out → Arduino 5V*ENA ENB IN1–IN4 ← ArduinoOUT1/2 = left · OUT3/4 = right Arduino Uno D2 ← left sensor OUTD3 ← right sensor OUTD5 → ENA · D6 → ENB (PWM)D7 D8 → IN1 IN2D9 D10 → IN3 IN4 Left IR sensorVCC 5V · GND · OUT Right IR sensorVCC 5V · GND · OUT Left motor Right motor + power5V*control (6 wires)signal
Wiring overview. *Only use the L298N's 5 V output to power the Arduino when the battery is under 12 V and the board's 5 V jumper is fitted; otherwise feed the Arduino from its own USB power bank. All grounds must be connected together.

Build it step by step

  1. Assemble the chassis. Fit the two motors at the back, the caster at the front, and mount the battery holder low and central so the robot does not tip.
  2. Mount the sensors. Fix both IR modules at the front, facing down, about 5–10 mm above the floor. Space them slightly wider than your line: with 19 mm electrical tape, about 25–30 mm apart works well, so both see floor when centred.
  3. Wire the motor driver. Battery + goes through the switch to the L298N's 12V terminal, battery − to GND. Left motor to OUT1/OUT2, right motor to OUT3/OUT4. Remove the ENA and ENB jumpers so the Arduino can control speed.
  4. Connect the Arduino. Wire the control pins as in the diagram, connect GND to GND, and power the Arduino (see the note under the diagram).
  5. Calibrate the sensors. Power up, hold each sensor over the floor and then over the tape, and turn its trimmer until the module's indicator LED changes cleanly between the two. Do this on the actual floor you will use.
  6. Lay a track. Start with a big oval of black tape on a light, matte surface. Avoid very tight corners until the robot is tuned.
  7. Upload the code below, put the robot on the line and switch on.

Arduino code

// TechForLive line follower: 2 IR sensors + L298N
const int L_SENSOR = 2, R_SENSOR = 3;
const int ENA = 5, IN1 = 7, IN2 = 8;   // left motor
const int ENB = 6, IN3 = 9, IN4 = 10;  // right motor
const int LINE = HIGH;   // many TCRT5000 modules read HIGH over black; change to LOW if yours is reversed
int baseSpeed = 150;     // 0-255, start slow
int turnSpeed = 120;

void setup() {
  pinMode(L_SENSOR, INPUT); pinMode(R_SENSOR, INPUT);
  int outs[] = {ENA, IN1, IN2, ENB, IN3, IN4};
  for (int p : outs) pinMode(p, OUTPUT);
}

void motors(int left, int right) {          // -255..255 each
  digitalWrite(IN1, left >= 0);  digitalWrite(IN2, left < 0);
  digitalWrite(IN3, right >= 0); digitalWrite(IN4, right < 0);
  analogWrite(ENA, abs(left));   analogWrite(ENB, abs(right));
}

void loop() {
  bool l = digitalRead(L_SENSOR) == LINE;
  bool r = digitalRead(R_SENSOR) == LINE;
  if (!l && !r)      motors(baseSpeed, baseSpeed);     // centred
  else if (l && !r)  motors(-turnSpeed, turnSpeed);    // line under left: pivot left
  else if (!l && r)  motors(turnSpeed, -turnSpeed);    // line under right: pivot right
  else               motors(0, 0);                     // both on line: stop
}

If the robot turns the wrong way, swap that motor's two wires (or swap IN1/IN2 in the code). If it ignores the line entirely, flip the LINE constant: modules differ in which level they output over black.

Free toolLED Resistor & Ohm's Law CalculatorRight resistor for LEDs and sensors.Open →

Tuning: from wobbly to smooth

  • Too fast, overshoots corners: lower baseSpeed. Cheap geared motors often will not start below about 80–100 PWM, so do not go too low.
  • Zig-zags on straights: instead of pivoting (one wheel backwards), slow the inner wheel: try motors(60, baseSpeed) for a gentle turn.
  • Loses the line on sharp bends: remember the last side that saw the line, and keep turning that way when both sensors see floor for more than a moment.
  • Works at night, fails by the window: sunlight contains infrared. Shield the sensors with a small skirt of black card.
  • Slows down as it runs: the L298N loses around 1.5–2 V internally, which matters on small batteries. Fresh cells or a TB6612FNG driver help.

Upgrade: 5 sensors and PID control

Competition line followers use a row of five to eight sensors (or an analog array such as the QTR series) and a PID controller. Each sensor gives a position, the code calculates how far the line is from centre (the error), and steers proportionally: a small error makes a small correction, a big one a big correction.

error = position - centre;            // e.g. -2..+2 with 5 sensors
integral += error;
derivative = error - lastError;
correction = Kp*error + Ki*integral + Kd*derivative;
motors(baseSpeed + correction, baseSpeed - correction);
lastError = error;

Tune with Ki = 0 first: raise Kp until the robot follows but oscillates, then add Kd until the oscillation settles. Only add a little Ki if it drifts to one side on long straights.

Can you build one without a microcontroller?

Yes. The original analog design uses each sensor's comparator output to switch a transistor that powers the opposite motor: when the left sensor sees the line, the right motor stops (or slows) and the robot turns left. It is a great lesson in comparators and transistors, but it cannot remember the last turn direction, handle junctions or run PID, which is why most builders now use an Arduino.

Quick answers

Why does my robot spin in circles?

One motor is wired backwards, or one sensor is reading "line" all the time. Check each sensor's indicator LED over floor and tape, then test each motor on its own.

Black line on white, or white on black?

Either works. For a white line on a dark floor, swap the meaning of LINE in the code.

What line width should I use?

Standard 19 mm electrical tape is the usual choice. Many robotics contests use lines between about 15 and 25 mm; check your event's rules.

Can I use a Raspberry Pi instead?

You can, but the Pi's GPIO pins are 3.3 V, so the sensor outputs must be 3.3 V-safe, and an Arduino's simple, instant start-up suits this job better. See our Raspberry Pi guide for where a Pi shines.