Line Following Robot using Arduino UNO

About Line Follower Robot

A line follower robot is a type of robot that can detect and follows a specific path. The path can be of any width and color. Generally, a dark color line over a light color background is preferred. We mainly use a black color line over a white color background. These type of robots uses either color sensor or IR sensor for the detection of the path. Once, the path is detected the robot will start moving along with the path. These types of robots are widely used in automation industries, e-commerce, and logistics industries. 

Working of Line Follower Robot

A line follower consists of a color sensor or an IR sensor for the detection of the path. In this tutorial, we will use IR sensors as they are cheap and easy to use. Here, we will use a black-colored path over a white background. The IR sensor that I am using here, outputs logic 1 when it detects white color and outputs logic 0 when it detects black color.

The line follower robot is equipped with two IR sensors. which are place in such a way that the black line always falls in the middle of the two sensors. The robot also consists of two motors that will control the movement of the robot.
When both the sensors are on the light background the IR rays from the IR sensor are reflected by the surface and we will get logic 1 from both the sensor and when one of the sensors is on a black line that particular sensor will output logic 0 because all the light emitted by the sensor got absorbed by the black line. 

In the first condition, when the line is in between the two sensors the robot should move in the forward direction. For that, both the motor should move in the forward direction.

Line follower robot working

In the second condition, when the left sensor is on the black line the robot should align it back to the previous position where the line is in between the two sensors, For that we will stop the left motor and rotate the right motor in the forward direction and thus the line will come again in between the two sensors. 

Line follower robot working

In the third condition, when the right sensor is on the black line the robot should align back to its previous position. So for that, we stop the right motor and rotate the left motor in the forward direction. the robot will align itself to the center position. 

Line follower robot working

And in the last condition is when both the sensors are on a black line that means this is the end of the line and the robot should stop. For that, both the motors should stop.

Line follower robot working

Components needed for Line Follower Robot

S.No

Component Name

Quantity

1

Arduino UNO

1

2

Breadboard

1

3

BO motor 100RPM

2

4

Ball Caster Wheel

1

5

9V battery and 5v Battery

1+1

6

Jumper Wires

30

7

L293D Motor Driver

1

8

IR sensors

2

9

Chassis

1

10

BO motor clamp

2

11

3mm Nut bolt

20

12

Spacers

2

Circuit diagram of Line Follower Robot

Line follower robot arduino circuit

Circuit working

Now, I will discuss the circuit diagram of the line follower robot.
So, we have our main component that is an Arduino UNO board that will be controlling all the motors and sensors, and then control the movement of the robot.

Then there are two IR sensors on for the left side and the other for the right side. The VCC wire of both the sensor is connected to the 5V pin of the Arduino UNO and the GND wire of both the sensor is connected to the GND pin of Arduino UNO. The data wire of the left sensor is connected to the 3rd pin of Arduino UNO and the data wire of the right sensor is connected to the 2nd pin of the Arduino UNO.

I have used different color wires for each of the connections and assign a pin number where they are connected. So, that it is easier for you to make connections. For example, the red wire is connected to the 5V pin of Arduino so I have mentioned 5V near the red wire and so on. 

Let us move to the other part of the circuit. So, we have two motors, one for the left wheel and the other for the right wheel and they are connected to the two terminals on the motor driver. You don’t have to worry about the polarity of the motor, we will change the input signal wire to switch between the polarity. 

For controlling the motion of the right motor we have two pins that are connected to the D4 and D5 pins of the Arduino UNO. Similarly, we have two pins to control the motion of the left motor which are connected to pin the D6 and D7 pins of the Arduino UNO. 

Now, these 6 pins are left. Starting two pins are for connecting 5 volts. You can use any of the pins as they are both internally connected. Then the next two pins are for the ground these two pins are also connected internally. One pin you have to connect to the GND pin of the Arduino UNO and another pin you have to connect to the GND of the External battery. And the last two pins are also connected internally these two pins are for connecting the positive wire of the external battery. 

I am using two separate batteries, one is for powering the Arduino UNO board and the other for powering the motors. I have used two le-ion cells in both batteries. In the first battery, I have connected them in series to get the output voltage of 8volts. These individuals’ cells when fully charged output 4 volts. So I have connected two in series. I will get 8 volts from them. You can use any other battery whose voltage is above 7.5 volts. 

In the next battery, I have connected the cell in a parallel configuration to get the voltage of 4 volts. These motors work on 3-6 volts. You can also replace this battery with a battery whose voltage is between 3.7- 6 volts and the current capacity is 2 amps.  

Assemble the robot as shown in the picture

Arduino code for Line follower Robot


//Define all the pins for sensors and motors
int sensorRight=2;
int sensorLeft=3;
int motorRight1=4;
int motorRight2=5;
int motorLeft1=6;
int motorLeft2=7;

void setup() {
  // Set the baud rate for serial communication
Serial.begin(9600);

//Set ir sensor pin as INPUT
pinMode(sensorLeft,INPUT);
pinMode(sensorRight,INPUT);
//Set motor pins as OUTPUT
pinMode(motorLeft1,OUTPUT);
pinMode(motorLeft2,OUTPUT);
pinMode(motorRight1,OUTPUT);
pinMode(motorRight2,OUTPUT);

//Set all the pin to HIGH by default
digitalWrite(motorLeft1,HIGH);
digitalWrite(motorLeft2,HIGH);
digitalWrite(motorRight1,HIGH);
digitalWrite(motorRight2,HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:
int leftSensorData=digitalRead(sensorLeft);
int rightSensorData=digitalRead(sensorRight);
Serial.print("SensorLeft:");
Serial.println(leftSensorData);
Serial.print("SensorRight:");
Serial.println(rightSensorData);

if((rightSensorData==HIGH)&&(leftSensorData==HIGH))//To move forward
{
  digitalWrite(motorRight1,LOW);
  digitalWrite(motorRight2,HIGH);
  digitalWrite(motorLeft1,HIGH);
  digitalWrite(motorLeft2,LOW);

}

else if((rightSensorData==HIGH)&&(leftSensorData==LOW))// To move left
{
  digitalWrite(motorRight1,LOW);
  digitalWrite(motorRight2,HIGH);
  digitalWrite(motorLeft1,HIGH);
  digitalWrite(motorLeft2,HIGH);
}

else if((rightSensorData==LOW)&&(leftSensorData==HIGH))// To move right
{
  digitalWrite(motorRight1,HIGH);
  digitalWrite(motorRight2,HIGH);
  digitalWrite(motorLeft1,HIGH);
  digitalWrite(motorLeft2,LOW);
}

else if((rightSensorData==LOW)&&(leftSensorData==LOW)) // To stop the robot
{
  digitalWrite(motorRight1,HIGH);
  digitalWrite(motorRight2,HIGH);
  digitalWrite(motorLeft1,HIGH);
  digitalWrite(motorLeft2,HIGH);
}

}

Important note: If your IR sensor outputs Logic 1 when it detects a black line and Logic 0 when it detects white background, change HIGH to LOW and LOW to HIGH in all the if and else if conditions.

Leave a Reply