Build Line Follower Robot using Arduino

Here’s a complete guide to build a Line Follower Robot using Arduino. This includes hardware requirements, circuit design, and Arduino code.


Components Required

  1. Arduino UNO (or Nano)
  2. L298N Motor Driver Module
  3. IR Sensor Module (2 or 3 recommended)
  4. BO Motors with Wheels (2)
  5. Swivel Castor Wheel (1)
  6. Chassis (any basic robot car body)
  7. Battery (Li-ion 7.4V or 9V with holder)
  8. Jumper wires, screws, etc.

Wiring Guide

IR Sensor (2-sensor configuration example):

  • Left Sensor (IR1):
    • VCC → 5V (Arduino)
    • GND → GND
    • OUT → D2
  • Right Sensor (IR2):
    • VCC → 5V
    • GND → GND
    • OUT → D3

L298N Motor Driver:

  • IN1 → D4 (Motor A)
  • IN2 → D5
  • IN3 → D6 (Motor B)
  • IN4 → D7
  • ENA → 5V jumper enabled
  • ENB → 5V jumper enabled
  • OUT1 → Left Motor
  • OUT2 → Left Motor
  • OUT3 → Right Motor
  • OUT4 → Right Motor
  • VCC → Battery + (6V–12V)
  • GND → Battery – and Arduino GND

Logic

  • Black line on white surface (black reflects less IR, white reflects more).
  • IR sensor reads:
    • White = HIGH
    • Black = LOW
IR1 (Left)IR2 (Right)Action
LOWLOWForward
LOWHIGHTurn Left
HIGHLOWTurn Right
HIGHHIGHStop (off line or idle)

Arduino Code:

// Motor Pins
#define IN1 4
#define IN2 5
#define IN3 6
#define IN4 7

// IR Sensors
#define LEFT_SENSOR 2
#define RIGHT_SENSOR 3

void setup() {
  // Motor pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  // IR Sensor pins
  pinMode(LEFT_SENSOR, INPUT);
  pinMode(RIGHT_SENSOR, INPUT);

  Serial.begin(9600);
}

void loop() {
  int leftStatus = digitalRead(LEFT_SENSOR);
  int rightStatus = digitalRead(RIGHT_SENSOR);

  Serial.print("Left: ");
  Serial.print(leftStatus);
  Serial.print(" | Right: ");
  Serial.println(rightStatus);

  if (leftStatus == LOW && rightStatus == LOW) {
    // Move forward
    forward();
  } else if (leftStatus == LOW && rightStatus == HIGH) {
    // Turn left
    turnLeft();
  } else if (leftStatus == HIGH && rightStatus == LOW) {
    // Turn right
    turnRight();
  } else {
    // Stop
    stopMotors();
  }
}

void forward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void stopMotors() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

Assembly Tips

  • Place IR sensors in front of the car, close to the ground (~1cm gap).
  • Test sensors individually before final wiring.
  • Use a black electrical tape on a white surface as a test track.