← All Projects

Line Following Robot with IR Sensors

376 viewsFebruary 26, 2026
ArduinoRobotIR SensorL298NMotorRobotics

Build a two-wheeled robot that autonomously follows a black line on a white surface using IR reflectance sensors. Uses an L298N motor driver to control two DC gear motors with differential steering.

Components & Supplies

×1Arduino Uno R3
×1L298N Motor Driver Module
×2IR Reflectance Sensor (TCRT5000)
×2DC Gear Motor (3-6V)
×1Robot Chassis Kit (2WD)
×14x AA Battery Holder

Circuit Connections

ComponentPinPinComponent
ArduinoD5 (PWM)ENAL298N
ArduinoD6IN1L298N
ArduinoD7IN2L298N
ArduinoD8IN3L298N
ArduinoD9IN4L298N
ArduinoD10 (PWM)ENBL298N
ArduinoD2OUTIR Sensor L
ArduinoD3OUTIR Sensor R

How It Works

A line following robot is one of the most popular beginner robotics projects. This robot uses two IR reflectance sensors mounted underneath, pointing downward. Each sensor returns a digital HIGH when it detects a reflective (white) surface and LOW when it detects a dark (black) line.

The Arduino reads both sensors and steers the robot: if the left sensor sees the line, turn left; if the right sensor sees it, turn right; if both see it, go straight; if neither sees it, stop.

arduinoLine Follower Sketch
// Motor A (Left)
const int enA = 5;
const int in1 = 6;
const int in2 = 7;
// Motor B (Right)
const int enB = 10;
const int in3 = 8;
const int in4 = 9;
// IR Sensors
const int leftSensor = 2;
const int rightSensor = 3;
const int motorSpeed = 150;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);
  Serial.begin(9600);
}

void moveForward() {
  analogWrite(enA, motorSpeed);
  analogWrite(enB, motorSpeed);
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
}

void turnLeft() {
  analogWrite(enA, 0);
  analogWrite(enB, motorSpeed);
  digitalWrite(in1, LOW); digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
}

void turnRight() {
  analogWrite(enA, motorSpeed);
  analogWrite(enB, 0);
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, LOW); digitalWrite(in4, LOW);
}

void stopMotors() {
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

void loop() {
  int left = digitalRead(leftSensor);
  int right = digitalRead(rightSensor);

  if (left == LOW && right == LOW) moveForward();
  else if (left == LOW && right == HIGH) turnLeft();
  else if (left == HIGH && right == LOW) turnRight();
  else stopMotors();

  delay(10);
}