Line Follower Robot using Arduino

Published  June 18, 2015   76
S Saddam
Author
Arduino Line Follower Robot Project with Code and Circuit Diagram

Line follower Robot is a very simple robot that follows a line, either a black line or a white line. These type of robots are very simple to build and is often the first choice for beginners who are getting started with robotics. Basically, there are two types of line follower robots: one is a black line follower which follows the black line and the second is a white line follower which follows the white line. Line follower actually senses the line and follows it. Though the idea sounds simple, with a little more development, robots similar to this are practically used in many applications like factory floor management robots or warehouse robots. We also made an Arduino UNO Line Follower Robot, you can check out the demonstration video given below:

 

Concepts of Line Follower

The concept of working of line follower is related to light. We use here the behavior of light at the black and white surfaces. When light falls on a white surface it is almost fully reflected and in the case of a black surface light is completely absorbed. This behavior of light is used in building a line follower robot.

Concept of White Line Follower Robot

Concept of Black Line Follower Robot

In this Arduino based line follower robot, we have used IR Transmitters and IR receivers also called photodiodes. They are used for sending and receiving light. IR transmits infrared lights. When infrared rays falls on the white surface, it’s reflected back and caught by photodiodes which generate some voltage changes. When IR light falls on a black surface, light is absorbed by the black surface and no rays are reflected back, thus photo diode does not receive any light or rays. Here in this Arduino line follower robot when the sensor senses white surface then Arduino gets 1 as input and when senses black line Arduino gets 0 as input.

Since the Line follower robot is an interesting beginners project, we have also built it using different development boards other than Arduino, you can also check them out using the below link if interested 

 

 

Circuit Explanation

The whole Arduino line follower robot can be divided into 3 sections: sensor section, a control section, and driver section.

 

Sensor section:

This section contains IR diodes, potentiometer, Comparator (Op-Amp) and LED’s. The potentiometer is used for setting reference voltage at comparator’s one terminal and IR sensors are used to sense the line and provide a change in voltage at the comparator’s second terminal. Then the comparator compares both voltages and generates a digital signal at the output. Here in this line follower circuit, we have used two comparators for two sensors. LM 358 is used as a comparator. LM358 has inbuilt two low noise Op-amps.

 

Control Section:

Arduino Pro Mini is used for controlling the whole the process of the line follower robot. The outputs of comparators are connected to digital pin numbers 2 and 3 of Arduino. Arduino read these signals and send commands to driver circuit to driveline follower. 

 

Driver section:

The driver section consists of motor driver and two DC motors. The motor driver is used for driving motors because Arduino does not supply enough voltage and current to the motor. So we add a motor driver circuit to get enough voltage and current for the motor. Arduino sends commands to this motor driver and then it drives motors.

 

Working of Line Follower Robot using Arduino

Building a Line follower robot using Arduino is interesting. The line follower robot senses a black line by using a sensor and then sends the signal to Arduino. Then Arduino drives the motor according to sensors' output.

Working of Line Follower Robot using Arduino

Here in this project, we are using two IR sensor modules namely the left sensor and the right sensor. When both left and right sensor senses white then the robot moves forward.

Working of Arduino Line Follower Robot

If the left sensor comes on a black line then the robot turn the left side.

Turning left to line follower robot using arduino

If the right sensor sense black line then robot turn right side until both sensors comes at the white surface. When the white surface comes robot starts moving on forward again.

Turning right to line follower robot

If both sensors come on the black line, the robot stops.

Stopping Arduino Line Following Robot

Circuit Diagram

Line Follower Robot using Arduino: Circuit Diagram

The complete circuit diagram for arduino line follower robot is shown in the above image. As you can see the output of comparators is directly connected to Arduino digital pin number 2 and 3. And motor driver’s input pin 2, 7, 10 and 15 is connected to Arduino's digital pin number 4, 5, 6 and 7 respectively. And one motor is connected at the output pin of motor drivers 3 and 6 and another motor is connected at pin 11 and 14. 

 

Program Explanation

In the program, first of all, we defined input and output pin, and then in loop, we check inputs and sends output according to inputs to the output pin for the driving motor. For checking the input pin we used “if” statements. The complete line follower robot code can be found at the bottom of this page. 

Code for line follower robot

Code for arduino line follower

There are four conditions in this line following robot that we read by using Arduino. We have used two sensors namely the left sensor and the right sensor.

Input

Output

Movement

Of Robot

Left Sensor

Right Sensor

Left Motor

 

Right Motor

LS

RS

LM1

LM2

RM1

RM2

 

0

0

0

0

0

0

Stop

0

1

1

0

0

0

Turn Right

1

0

0

0

1

0

Turn Left

1

1

1

0

1

0

Forward

 

We write the arduino line follower code according to the conditions shown in table above.

 

Required Components

Arduino

In our Project, we have used a microcontroller to control whole the process of system that is ARDUINO. Arduino is an open-source hardware and very useful for project developments. There are many types of arduino like Arduino UNO, arduino mega, arduino pro mini, Lilypad etc. available in the market. Here we have used arduino pro mini in this project as arduino pro mini is small and so breadboard compatible. To burn the line follower robot arduino code we have used an FTDI burner.

Arduino pro Mini

 

L293D Motor Driver

L293D is a motor driver IC which has two channels for driving two motors. L293D has two inbuilt Transistor Darlington pair for current amplification and a separate power supply pin for giving external supply to motors.

L293D Motor Driver IC

 

IR Module:

IR Module is sensor circuit that consists IR LED/photodiode pair, potentiometer, LM358, resistors and LED. IR sensor transmits Infrared light and photodiode receives the infrared light.

IR Module

 

Power Supply  

I have added a voltage regulator to get 5 volts for Arduino, comparator and motor driver. And a 9-volt battery is used to power the circuit.

Code

/*------ Arduino Line Follower Code----- */
/*-------defining Inputs------*/
#define LS 2      // left sensor
#define RS 3      // right sensor

/*-------defining Outputs------*/
#define LM1 4       // left motor
#define LM2 5       // left motor
#define RM1 6       // right motor
#define RM2 7       // right motor

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
}

void loop()
{
  if(digitalRead(LS) && digitalRead(RS))     // Move Forward
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // Turn right
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // turn left
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);
  }
  
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // stop
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);
  }
}

Video

Comments

Submitted by gabriel on Tue, 07/17/2018 - 18:32

In reply to by santhosh kumar

Permalink

please what is the programing language am still studing 2 langauges. but for the others am fine

Submitted by Rishav Agrahari on Thu, 01/21/2016 - 19:20

Permalink

Will there be no headder file included in program ? if yes, can u give the complete program.And which software u used to burn program on arduino uno.

Submitted by Rudra on Sun, 02/28/2016 - 21:54

Permalink

Sir , plz tell how to connect the ir sensor

Submitted by Sudhansu on Mon, 03/07/2016 - 13:24

In reply to by Rudra

Permalink

How many pins does your sensor have?
If 3: you can see they will be marked as follows: VCC, GND and the third pin isOutput. Connect VCC to 5V and GND to Ground and then connect the Output pin to the Analog/Digital pin of Arduino..any way u like it. If you are following this particular code then connect the output of LEFT SENSOR to digital pin 2 of your Arduino and the output of RIGHT SENSOR to digital pin 3 of the Arduino. Hope this helps!!
In case of 4 pins; the pins are: VCC, GND, Analog(A), Digital(D). In that case if you are following this code then connect the DIGITAL pins of both the sensors in the same way as above,i.e., to digital pins 2 and 3 of the Arduino...
Hope this helps!!

Submitted by Devjeet Mandal on Thu, 03/10/2016 - 20:09

Permalink

Hello, I m not so families with these things. But I wanted to know.. If I have IR module.. Then do I need to put all the resistance, LM358,diode etc as instructed on circuit.

Submitted by Khurram Mahmood on Mon, 03/14/2016 - 03:33

Permalink

I am using arduino uno, will the same code work and my robot only has 2 motors for the Weels and 2 IR sensors ( 2 receivers and 2 emitters).

Submitted by Maitry gandhi on Mon, 04/04/2016 - 11:10

Permalink

Hi I am making pick and place robotic arm along with line follower so can anyone help me in coding section I am using 4 DC motors 2 for base and 1for shoulder 1for wrist and I am using limit switch too plz its too much important for me

Submitted by harish on Wed, 05/25/2016 - 18:58

Permalink

hi sir nice project
i have two ir sensor
but both a sensor are same
[i.e] connection of LM358 1st pin output
but i don't have a 7th pin output
what can i do sir

Submitted by ashin on Tue, 06/07/2016 - 18:18

Permalink

its nice.u did a good job. continue your new projects and also share .All things,connecting' s etc are shown very clearly .its very clean.sounds good....

Submitted by Pedro on Thu, 07/21/2016 - 05:50

Permalink

`LS' was not declared in this scope

and arduino selects this sentence:
if(!(digitalRead(LS)) && !(digitalRead(RS))) // stop

What should i do?
Thanks.

Submitted by alejandro on Mon, 07/25/2016 - 23:10

Permalink

how can i slow the speed of my motor ? i already test it but i want to slow the speed of my motor what will i do to slow down the speed ?

Submitted by KRISHNAPRIYA N on Thu, 08/11/2016 - 21:50

Permalink

void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
pinMode(10,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int s1=digitalRead(9);
int s2=digitalRead(10);
if(s1==HIGH && s2==HIGH)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else if(s1==HIGH&&s2==LOW)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
}
else if(s1==LOW&&s2==HIGH)
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
}

can you please tell me this program is correct or not.

Submitted by soham on Wed, 09/14/2016 - 20:12

Permalink

Hi
can any one tell me how can we combine two codes means i am having the code for black line and another code for white line i want to combine both the cods so that if while traveling from black line when imediatly white line starts how can i combine the code please help

maybe you could add a third sensor in the middle ad add an IF statement that cheks the if the two sides sensors are sensing something and the middel sensor is sensing something else then do something (which is using the proper code for that line that is sensed by the middel sensor) that code be done by putting the two codes in external functions and calling them when needed .

Submitted by Shaff on Fri, 10/07/2016 - 15:06

Permalink

Can i know how do you connect the battery ?
which component that you connect it with ? i try to understand, but i cant find the answer.
tq

Submitted by m faizal iskandar on Sat, 11/12/2016 - 02:46

Permalink

Basic program and setup for Line Follower Robot

Submitted by Arsl on Sat, 02/18/2017 - 22:39

Permalink

Sir,
i have 4 way infrared tracking sensor could you please tell me how i connect this to arduino uno r3 and also tell me the code of this type infrared sensor.

Submitted by aswath on Mon, 02/27/2017 - 17:42

Permalink

can u say the program for which i can use 2 ir sensor for following black line and two ir sensor following white surface in single program plz.

Submitted by nihal on Sun, 03/05/2017 - 18:24

Permalink

if(digitalRead(LS) && !(digitalRead(RS))) // turn left
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
if it reads LS means left sensor has an output of 1,hence it is on white.RS doesnt reads means it has value of 0 and its on black line.so its should movie to right side.but program here shows to turn left.plz clear my doubt as iam a beginner.

You are right.. it will turn rights as right sensor reads 0 because its black surface so it will turn right. code is correct inside as Left motor is moving, right motor is stopped so it will turn right.

Submitted by ravi on Mon, 04/03/2017 - 22:52

Permalink

Can u give the code for both black and white background included in line
Some times they will give one half as black line on white background and other half as white line and black background

Submitted by IBTAHAJ ALI on Tue, 04/04/2017 - 22:16

Permalink

can i use arduino nano instead of mini ore can i use arduino uno intead of it..
plzzzzzz brother reply must..thanks in advance

Submitted by Surya on Tue, 09/12/2017 - 23:29

Permalink

Hi...can anyone pls tell me how this comparator works... What will be the output of comparator in black surface and white surface??

Submitted by tariqnadeem on Thu, 09/21/2017 - 10:27

Permalink

if anyone have a project of waiter robot connect me at []
at very very reasonable price
in this project
line following,obstacle detection,table to table order

Submitted by MD Javed on Wed, 10/11/2017 - 07:50

Permalink

The above program for line follower u have given is perfect to install in my arduino board ? Please answer me quick

Submitted by leonardo on Wed, 10/11/2017 - 10:47

Permalink

hello sir. I like to know whether can I use your code for arduino leonardo or shall I make some change?

Submitted by gaurav on Wed, 10/11/2017 - 15:32

Permalink

hllo
sir how can we make our bot slow by using analog we can control the voltage that is given to the motor so what changes are required in the codes that are given above

Submitted by Ashish on Sat, 11/11/2017 - 16:48

Permalink

in this code why none of the left motor 2 or righ motor 2 moves??

Submitted by Dariel on Tue, 12/05/2017 - 13:23

Permalink

How can i connect the output pins of IR sensor in arduino motor shield? what pin do i want to use? the analog or the digital pins.. im newbie about this robotics. please help

Submitted by martin on Tue, 02/13/2018 - 10:59

Permalink

can u please give me the coading of IR8 sensor ?
because im using IR8 sensor with 2 motors ...

Submitted by Lizzie on Mon, 02/19/2018 - 21:06

Permalink

Anyone got a fritzing of this circuit, or one similar?

Submitted by Navdeet Saini on Wed, 02/28/2018 - 20:33

Permalink

sir what type of changes can i do in these program so that they move 180degree after line is terminate,
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
pinMode(10,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int s1=digitalRead(9);
int s2=digitalRead(10);
if(s1==HIGH && s2==HIGH)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else if(s1==HIGH&&s2==LOW)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
}
else if(s1==LOW&&s2==HIGH)
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
}

You cannot make an exact 180 degree with your bot since t does not have a feedback. But you aim it to make the robot follow a line from A to B and then again from B to A then the folowing edit should work

void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
pinMode(10,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int s1=digitalRead(9);
int s2=digitalRead(10);
if(s1==HIGH && s2==HIGH)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else if(s1==HIGH&&s2==LOW)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
}
else if(s1==LOW&&s2==HIGH)
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
}

Submitted by Navdeet Saini on Sun, 03/04/2018 - 19:06

Permalink

SIR I TRYING TO MAKE A BOT WHICH FOLLOW A LINE AND WHEN LINE IS TERMINATE THEN THEY MOVE WITHOUT TOUCH THE OBSTACLE
I USE IR ARRAY AND IR ULTRASOUND SENSOR
BUT SIR HOW I CAN DO CODE SO THAT GIVE PREFFERENCE TO IR ARRAY SENSOR AND WHEN LINE IS TERMINATE THAN THEY MOVE WITH HELP OF ULTRASOUND SENSOR

Submitted by Long on Mon, 03/19/2018 - 10:17

Permalink

I need code for robot check line 5 sensors. It is very difficult with me.! who can help me here? please send me a code, i am thank you very much!