11:07:37 AM NPT Arduino, Robotics & IoT tutorials for Nepal
Beginner

Line Follower Robot with Arduino and IR Sensors (Complete Build)

Build a line follower robot with Arduino Uno, two IR sensors and an L298N driver. Parts, wiring table, tested logic, code, tuning and fixes.

On this page
Line Follower Robot with Arduino and IR Sensors (Complete Build)

A line follower is the classic first robot for BE and diploma students. It follows a black line on a white floor using infrared (IR) sensors, and it teaches you sensors, motor drivers and simple control logic in one build. It is also a regular event at college tech fests, so the same robot can win you a certificate.

This guide builds a two-sensor version with an Arduino Uno and an L298N motor driver. At the end there is a section on upgrading to three or five sensors for smoother turns.

What you need

Scroll sideways to see all columns.

Item Qty Notes
Arduino Uno (or compatible) 1 Use a classic 5 V Uno or a compatible board with the same pinout
IR sensor module (reflective, with digital output) 2 The small blue board with an emitter, a receiver and a potentiometer
L298N dual motor driver module 1 Drives two DC motors
BO (gear) motors with wheels 2 3 V to 9 V yellow gear motors are common
Caster (ball) wheel 1 Third support point
Robot chassis 1 Acrylic kit, or cut your own from a board
Battery pack for motors 1 A protected battery pack or AA pack sized for the motors and driver
USB power bank (for the Arduino) 1 Keeps the Arduino separate from motor noise
Black electrical tape and a white board or sheet 1 To make the track
Jumper wires, screwdriver, switch 1 set

Connection table

The motors must run from their own battery, not from the Arduino 5 V pin. Motors pull a lot of current and cause voltage dips that reset the Arduino. Connect all grounds together so the signals share a reference.

Scroll sideways to see all columns.

Pin or terminal Connects to
Left IR sensor OUT Arduino D2
Right IR sensor OUT Arduino D3
Both IR sensors VCC / GND Arduino 5 V / GND
L298N ENA Arduino D5 (PWM)
L298N IN1, IN2 Arduino D7, D8
L298N IN3, IN4 Arduino D9, D10
L298N ENB Arduino D6 (PWM)
L298N OUT1, OUT2 Left motor
L298N OUT3, OUT4 Right motor
L298N 12V terminal Motor battery +
L298N GND terminal Motor battery -, and an Arduino GND pin
L298N 5V terminal Arduino 5 V for driver logic, with the 5V-EN jumper removed
Arduino power USB power bank into the USB port

Remove the two small jumper caps on ENA and ENB so that the PWM pins can control speed. Remove the separate 5V-EN jumper before feeding 5 V into the L298N logic terminal.

How it works

An IR sensor module has an IR LED and a photodiode side by side. Light bounces off a white surface back to the photodiode, but a black surface absorbs most of it. A comparator on the module turns this into a digital output. On most modules the output is LOW over white (reflective) and HIGH over black, but some boards are the opposite. Test yours first, as described below, and flip the constant in the code if needed.

Place the two sensors so the line sits between them. The decision table is then simple:

Scroll sideways to see all columns.

Left sensor Right sensor Meaning Action
White White Line is between the sensors or both have lost it Go forward
Black White Line drifted to the left Turn left
White Black Line drifted to the right Turn right
Black Black Crossing or end marker Stop

This is bang-bang control: the robot corrects only when a sensor sees black. It is easy to explain in a viva and works well at slow to moderate speed.

Step-by-step build

  1. Fix the two motors and the caster wheel on the chassis. Add the wheels.
  2. Mount the L298N and the Arduino on top. Mount the battery holder at the back for weight over the drive wheels.
  3. Fix the IR sensors at the front, facing the ground, about 5 mm to 15 mm above it. Leave a gap between them slightly wider than your tape (for 19 mm tape, about 25 mm to 30 mm apart).
  4. Wire everything using the connection table. Solder the motor leads, since loose motor wires are the number one cause of a robot that "sometimes works".
  5. Make a track: black tape, about 19 mm wide, on a white sheet or light floor. Start with a long gentle oval before using sharp corners.
  6. Upload the test sketch below first, then the full code.

Test the sensor logic first

Upload this and open the Serial Monitor at 9600 baud. Slide a piece of black tape under each sensor and note the value.

sketch-1.inoDownload code
// Sensor test - not compiled yet, check on your board
void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
}
void loop() {
  Serial.print("L=");
  Serial.print(digitalRead(2));
  Serial.print("  R=");
  Serial.println(digitalRead(3));
  delay(200);
}

Turn the small potentiometer on each module until its on-board LED changes cleanly between white and black.

Complete code

sketch-2.inoDownload code
// Two-sensor line follower - not compiled yet
const int LEFT_IR  = 2;
const int RIGHT_IR = 3;

const int ENA = 5;   // left motor speed (PWM)
const int IN1 = 7;
const int IN2 = 8;
const int IN3 = 9;
const int IN4 = 10;
const int ENB = 6;   // right motor speed (PWM)

// Change to LOW if your module outputs LOW over black
const int ON_LINE = HIGH;

const int BASE_SPEED = 130;  // 0-255, start low
const int TURN_SPEED = 160;  // speed of the outer wheel when turning

void setup() {
  pinMode(LEFT_IR, INPUT);
  pinMode(RIGHT_IR, INPUT);
  pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
}

// speeds: positive = forward, negative = backward
void drive(int left, int right) {
  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 leftOnLine  = (digitalRead(LEFT_IR)  == ON_LINE);
  bool rightOnLine = (digitalRead(RIGHT_IR) == ON_LINE);

  if (!leftOnLine && !rightOnLine) {
    drive(BASE_SPEED, BASE_SPEED);        // assume the line is between the sensors
  } else if (leftOnLine && !rightOnLine) {
    drive(0, TURN_SPEED);                 // line on left: turn left
  } else if (!leftOnLine && rightOnLine) {
    drive(TURN_SPEED, 0);                 // line on right: turn right
  } else {
    drive(0, 0);                          // both on black: stop
  }
}

If the robot turns away from the line instead of toward it, swap the two turn lines, or swap the two sensor pin numbers.

Testing and tuning

  • Wheels first. With the robot lifted, run drive(150,150). Both wheels should spin forward. If one spins backward, swap that motor's two wires.
  • Sensors on the track. Hold the robot over the line and check the on-board sensor LEDs react correctly.
  • Speed. Start with a BASE_SPEED around 120 to 140. Raise it in steps of 10 only when the robot follows the line without wobbling.
  • Turns. For tight corners, make the inside wheel go slightly backward, for example drive(-100, TURN_SPEED), for a sharper pivot.
  • Lighting. Strong sunlight contains IR and can confuse the sensors. Test indoors first or shade the sensors with a small cardboard hood.

Troubleshooting

Scroll sideways to see all columns.

Symptom Likely cause Fix
Robot spins in circles Motor wires reversed on one side Swap the wires of the wrong motor
Turns away from the line Sensor logic inverted Check the sensor test and set ON_LINE for black. Confirm D2 is the left sensor
Arduino resets when motors start Motors sharing the Arduino supply Use separate battery and power bank, common GND
Sensors always read the same Potentiometer not tuned, or too far from the ground Adjust the pot; lower the sensors to 5 mm to 15 mm
Overshoots corners and leaves the track Speed too high Lower BASE_SPEED, or add a third sensor
Motors hum but do not turn PWM too low or battery weak Raise speed, charge the battery
Works on one floor, not another Shiny or dark floor Use a white sheet; re-tune the pot

Upgrade: three or five sensors

With three sensors (left, centre, right) the robot always has the line under the centre sensor, so you can drive straight until a side sensor trips. With five sensors you can give each sensor a weight and compute the error as a weighted average of positions, then correct with a PID loop. Start with proportional control only (P), add D if the robot wobbles, and keep I at zero until the rest works.

FAQ

Can I use a 9 V battery for the motors?

A 9 V battery has a small capacity and gives poor current. Use a holder with two 18650 cells or several AA cells instead.

Why does the L298N make my motors slower than the battery voltage?

The L298N has an internal voltage drop of roughly 1.5 V to 2 V per side, so the motors get less than the battery supplies. Choose motors rated for the voltage left over.

Do I need an Arduino? Can I build it without code?

Yes, a comparator circuit like an LM393 can drive a motor driver directly, but the Arduino version is easier to tune and to extend.

How do I make it faster?

Use more sensors for smoother control, lower the robot's weight, and tune a PID loop rather than only raising motor speed.

Related tutorials

See all Robotics →