Translate

Arduino DIY Line Following Robot Using 4 IR Sensors

 1. Introduction

Prototype Diagram

A Line Following Robot is one of the most popular beginner robotics projects. It is widely used in robotics competitions, industrial automation, and educational demonstrations.

The robot follows a predefined path, usually a black line on a white surface or a white line on a black surface. The robot continuously detects the position of the line using sensors and adjusts the movement of the motors accordingly.

In this project, we build a 4-sensor line following robot using Arduino. Two IR sensors are placed at the front and two IR sensors at the back. This configuration improves stability and helps the robot detect the line more accurately, especially during turns.

The sensors detect whether the robot is aligned with the line. Based on the sensor readings, the Arduino controls the motor driver to move the robot forward, left, or right.

This project is ideal for students, robotics beginners, and electronics enthusiasts who want to understand sensor-based robot navigation.


2. Components Required

ComponentQuantity
Arduino Uno        1
L298N Motor Driver Module        1
IR Sensor Modules        4
DC Gear Motors            2
Robot Chassis        1
Wheels        2
Castor Wheel            1
Battery (7.4V or 9V)        1
Connecting WiresAs required
Breadboard (optional)        1

3. Circuit and ConnectionsPinout Diagram of Arduino Uno R3

Pinout Diagram of L298N Motor Driver Module

Pinout Diagram of IR Sensor Module

IR Sensor Connections

SensorArduino Pin
Front Left Sensor        D2
Front Right Sensor        D3
Back Left Sensor        D4
Back Right Sensor        D5

Motor Driver Connections

L298N Pin    Arduino Pin
    IN1        D6
    IN2        D7
    IN3        D8
    IN4        D9

Power Connections

  • L298N 12V → Battery Positive

  • L298N GND → Battery Ground

  • L298N 5V → Arduino 5V

  • All sensor VCC → 5V

  • All sensor GND → GND


4. Circuit Working

The robot uses four infrared sensors to detect the line.

Front Sensors

  • Detect the line while the robot is moving forward.

  • Help correct direction quickly.

Back Sensors

  • Help confirm alignment with the line.

  • Improve stability during turns.

Sensor Principle

IR sensors work by emitting infrared light and measuring reflection.

  • White surface → reflects IR → sensor output HIGH

  • Black line → absorbs IR → sensor output LOW

The Arduino reads these signals and determines whether the robot should:

  • Move forward

  • Turn left

  • Turn right

  • Stop


5. Arduino Code

// Sensor pins
int FL = 2; // Front Left
int FR = 3; // Front Right
int BL = 4; // Back Left
int BR = 5; // Back Right

// Motor pins
int IN1 = 6;
int IN2 = 7;
int IN3 = 8;
int IN4 = 9;

void setup()
{
pinMode(FL, INPUT);
pinMode(FR, INPUT);
pinMode(BL, INPUT);
pinMode(BR, INPUT);

pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}

void loop()
{

int fl = digitalRead(FL);
int fr = digitalRead(FR);
int bl = digitalRead(BL);
int br = digitalRead(BR);

// Forward
if(fl==0 && fr==0)
{
forward();
}

// Turn Left
else if(fl==0 && fr==1)
{
left();
}

// Turn Right
else if(fl==1 && fr==0)
{
right();
}

// Stop
else
{
stopRobot();
}

}

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

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

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

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

6. Code Working

The program starts by defining the sensor pins and motor driver pins.

Inside the loop function, the Arduino continuously reads the values from all four IR sensors.

If the front sensors detect the black line, the robot moves forward.

If the left sensor detects the line, the robot turns left to re-align with the path.

If the right sensor detects the line, the robot turns right.

If the robot loses the line completely, the motors stop to prevent the robot from moving in the wrong direction.

The back sensors help maintain balance and ensure the robot stays centered on the line.


7. Tips for Best Performance

✔ Adjust the IR sensor potentiometer for proper sensitivity
✔ Use a clear black electrical tape line on a white surface
✔ Maintain a consistent lighting environment
✔ Keep sensors close to the ground (1–2 cm)
✔ Use gear motors with moderate speed


8. Applications

Line following robots are widely used in many industries and learning environments.

Some common applications include:

  • Automated warehouse vehicles

  • Industrial material transport systems

  • Robotic competitions

  • Smart delivery robots

  • Educational robotics projects

These robots demonstrate the fundamentals of automation, sensors, and feedback control systems.


9. Conclusion

In this project, we built a Line Following Robot using Arduino and four IR sensors. Two sensors at the front detect the line direction, while two sensors at the back help maintain stability and alignment.

This configuration provides better accuracy compared to simple two-sensor robots. It is an excellent project for students to learn about robotics, sensors, motor control, and embedded programming.

With further improvements such as PID control, speed optimization, and additional sensors, this robot can be made faster and more intelligent.

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.