Translate

DIY Line Following Robot Using Arduino and Two IR Sensors

 1. Introduction


A Line Following Robot is one of the most popular beginner robotics projects. It is designed to automatically follow a path drawn on the floor, usually a black line on a white surface.

The robot uses Infrared (IR) sensors to detect the line. When the sensors detect the black path, the robot adjusts the motors so that it stays on the track.

In this project, we will build a simple and cost-effective line following robot using Arduino and two IR sensors. The robot continuously reads the sensors and controls the motors using a motor driver module (L298N or L293D).

This project is ideal for:

  • Robotics beginners

  • Arduino learners

  • Engineering students

  • School robotics competitions

The concept used in this robot is sensor feedback and motor control.


2. Components Required

NoComponentQuantity
1Arduino UNO / Nano        1
2IR Sensor Module        2
3L298N Motor Driver        1
4DC Gear Motor        2
5Robot Chassis        1
6Robot Wheels        2
7Caster Wheel        1
8Battery (7.4V / 9V)        1
9Jumper WiresAs required
10Breadboard (optional)        1

3. Circuit and Connections

Pinout Diagram of Arduino Uno R3Pinout Diagram of L298N Motor Driver Module

Pinout Diagram of IR Sensor Module

IR Sensors to Arduino

Left IR Sensor
VCC → 5V
GND → GND
OUT → Arduino Pin 2

Right IR Sensor
VCC → 5V
GND → GND
OUT → Arduino Pin 3


Motor Driver to Arduino

Motor DriverArduino
IN18
IN29
IN310
IN411

Motors to Motor Driver

Left Motor → OUT1, OUT2
Right Motor → OUT3, OUT4


Power

Battery → Motor Driver VCC
Arduino powered via USB or battery


4. Circuit Working

The robot uses two IR sensors placed in front of the robot.

Each sensor detects the color of the surface:

  • White surface → Reflects IR → Sensor output HIGH

  • Black line → Absorbs IR → Sensor output LOW

The Arduino continuously reads both sensors.

Possible Conditions

Left Sensor    Right Sensor    Robot Action
    White    White    Move Forward
    Black    White    Turn Left
    White    Black    Turn Right
    Black        Black    Stop / Adjust

Based on these conditions, Arduino controls the motor driver module, which drives the two motors.


5. Arduino Code

// Line Following Robot using 2 IR Sensors

int leftSensor = 2;
int rightSensor = 3;

int IN1 = 8;
int IN2 = 9;
int IN3 = 10;
int IN4 = 11;

void setup()
{
pinMode(leftSensor, INPUT);
pinMode(rightSensor, INPUT);

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

void loop()
{

int left = digitalRead(leftSensor);
int right = digitalRead(rightSensor);

if(left == HIGH && right == HIGH)
{
forward();
}

else if(left == LOW && right == HIGH)
{
turnLeft();
}

else if(left == HIGH && right == LOW)
{
turnRight();
}

else
{
stopRobot();
}

}

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

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

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

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

6. Code Working

Sensor Reading

int left = digitalRead(leftSensor);
int right = digitalRead(rightSensor);

The Arduino reads the state of both IR sensors.


Forward Movement

If both sensors detect white surface, the robot moves forward.

forward();

Both motors rotate forward.


Left Turn

If the left sensor detects black, the robot moves left to return to the line.

turnLeft();

The right motor moves while the left motor stops.


Right Turn

If the right sensor detects black, the robot moves right.

turnRight();

The left motor runs while the right motor stops.


Stop Condition

If both sensors detect black, the robot stops.

stopRobot();

7. Tips for Best Performance

✔ Use matte black tape for the track.
✔ Keep sensors 1 cm above the ground.
✔ Adjust the IR sensor potentiometer for sensitivity.
✔ Use gear motors with good torque.
✔ Ensure battery voltage is stable.


8. Applications

Line following robots are widely used in:

  • Industrial automation

  • Warehouse robots

  • Hospital medicine delivery robots

  • Automated transport systems

  • Robotics competitions

  • Educational robotics projects


9. Conclusion

The DIY Line Following Robot using Arduino and two IR sensors is a great beginner robotics project. It helps students understand sensor interfacing, motor control, and automation concepts.

By modifying the code and adding more sensors, the robot can be made more accurate and capable of handling complex tracks.

This project is an excellent starting point for learning robotics, embedded systems, and Arduino programming.

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

Empowering students in Kerala with hands-on technical skills.