Arduino Line Follower Robot Project with Code and Circuit Diagram

Line follower Robot is a machine which follows a line, either a black line or white line. Basically there are two types of line follower robots: one is black line follower which follows black line and second is white line follower which follows white line. Line follower actually senses the line and run over it.


Concepts of Line Follower

Concept of working of line follower is related to light. We use here the behavior of light at black and white surface. When light fall on a white surface it is almost full reflected and in case of 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 photo diodes. They are used for sending and receiving light. IR transmits infrared lights. When infrared rays falls on white surface, it’s reflected back and catched by photodiodes which generates some voltage changes. When IR light falls on a black surface, light is absorb 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 sensor senses white surface then arduino gets 1 as input and when senses black line arduino gets 0 as input.


Circuit Explanation

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


Sensor section:

This section contains IR diodes, potentiometer, Comparator (Op-Amp) and LED’s. 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 comparator’s second terminal. Then comparator compares both voltages and generates a digital signal at output. Here in this line follower circuit we have used two comparator for two sensors. LM 358 is used as comparator. LM358 has inbuilt two low noise Op-amps.


Control Section:

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


Driver section:

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


Working of Line Follower Robot using Arduino

Working of line follower is very interesting. Line follower robot senses black line by using 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 left sensor and right sensor. When both left and right sensor senses white then robot move forward.

Working of Arduino Line Follower Robot

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

Turning left to line follower robot using arduino

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

Turning right to line follower robot

If both sensors comes on black line, robot stops.

Stopping Arduino Line Following Robot

Circuit Diagram

Line Follower Robot using Arduino: Circuit Diagram

Complete circuit diagram for arduino line follower robot is shown in the above iamge. As you can see 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 output pin of motor driver 3 and 6 and another motor is connected at pin 11 and 14. 


Program Explanation

In program, first of all we defined input and output pin, and then in loop we checks inputs and sends output according to inputs to output pin for driving motor. For checking input pin we used “if” statements.

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 sensor namely left sensor and right sensor.




Of Robot

Left Sensor

Right Sensor

Left Motor


Right Motor





















Turn Right







Turn Left









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


Required Components


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 program we have used 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 which consists IR LED/photodiode pair, potentiometer, LM358, resistors and LED. IR sensor transmits Infrared light and photo diode receives the infrared light.

IR Module


Power Supply  

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


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

/*-------definning 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);


Comments (53)

  • santhosh kumar's picture
    santhosh kumar


    Sep 13, 2015
  • Rishav Agrahari's picture
    Rishav Agrahari

    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.

    Jan 21, 2016
  • Rudra's picture

    Sir , plz tell how to connect the ir sensor

    Feb 28, 2016
  • Sudhansu's picture

    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!!

    Mar 07, 2016
  • Sudhansu's picture

    Best description of an LFR n the internet!!! Thanks buddy...

    Mar 07, 2016
  • Devjeet Mandal's picture
    Devjeet Mandal

    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.

    Mar 10, 2016
  • Khurram Mahmood 's picture
    Khurram Mahmood

    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).

    Mar 14, 2016
  • manish pandey's picture
    manish pandey

    This is the perfect platform whr we cn get any information related to arduino..really it is nice1

    Apr 01, 2016
  • Maitry gandhi's picture
    Maitry gandhi

    Can you plz tell me if I am doing pick and place robotic arm along with line follower what should be my code plzzzz I am using arduino mega 2560 and 4 DC motors

    Apr 03, 2016
  • Maitry gandhi's picture
    Maitry gandhi

    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

    Apr 04, 2016
  • harish's picture

    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

    May 25, 2016
  • ashin's picture

    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....

    Jun 07, 2016
  • Pedro's picture

    `LS' was not declared in this scope

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

    What should i do?

    Jul 21, 2016
  • Maddy's picture

    Check if you declared the LS properly in the code.

    Aug 20, 2016
  • alejandro's picture

    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 ?

    Jul 25, 2016
  • Maddy's picture

    You can reduce the driving Voltage by using some voltage regulator like LM317.

    Aug 20, 2016
  • talal's picture

    produce some delay in program

    Sep 24, 2016
  • KRISHNAPRIYA N's picture

    void setup() {
    // put your setup code here, to run once:

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

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

    Aug 11, 2016
  • teena choudhary's picture
    teena choudhary

    your program is correct.....

    Nov 30, 2016
  • soham's picture

    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

    Sep 14, 2016
  • Shaff's picture

    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.

    Oct 07, 2016
  • Abhishek's picture

    Use a simple 9v Battery, and connect is according to Circuit Diagram.

    Oct 15, 2016
  • Giap's picture

    Sir , could you list all the components needed for this project ? thank you .

    Oct 10, 2016
  • isra's picture

    If you'll be using arduino, what changes to the ckt will be made?

    Oct 18, 2016
  • Annie's picture

    How to turn the line follower at exact 90 degree. it would be better if the reply is given the the exact code. thanks.

    Oct 19, 2016
  • Maddy's picture

    It follows Black line, whatever is the path, even 90 degree.

    Oct 27, 2016
  • m faizal iskandar 's picture
    m faizal iskandar

    Basic program and setup for Line Follower Robot

    Nov 12, 2016
  • Jibril's picture

    Please which sensor did you use and where can i buy it

    Dec 06, 2016
  • Abhishek's picture

    We have used 2 IR sensors or IR modules which mainly consists one IR LED and one Photo diode (Black LED type component). They are available online as well as offline on stores.

    Dec 31, 2016
  • Ikram ul haq's picture
    Ikram ul haq

    Cant upload coding in Arduino fault is showing
    "Arduino: 1.8.0 (Windows 10), Board: "Arduino/Genuino Uno"

    Sketch uses 1178 bytes (3%) of program storage space. Maximum is 32256 bytes.
    Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
    avrdude: ser_open(): can't open device "\\.\COM1": The system cannot find the file specified.

    Problem uploading to board. See for suggestions.

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences."

    Please help

    Jan 09, 2017
  • Yuvin Kavisinghe's picture
    Yuvin Kavisinghe

    I have had this problem previously. To fix it I changed the COM port. You can do this by going to tools > port > then select the final port listed.

    Jan 12, 2017
  • Nour's picture

    Thank you for sharing this.
    Do you have a schematic diagram of the connection between the components?

    Jan 21, 2017
  • Arsl's picture

    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.

    Feb 18, 2017
  • aswath's picture

    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.

    Feb 27, 2017
  • nihal's picture

    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 its should movie to right side.but program here shows to turn left.plz clear my doubt as iam a beginner.

    Mar 05, 2017
  • Noor's picture

    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.

    Sep 27, 2017
  • ravi's picture

    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

    Apr 03, 2017
  • IBTAHAJ ALI's picture

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

    Apr 04, 2017
  • Aryan's picture

    Hv u used 4 motors?????
    Please tell fast as I have to prepare one for inter school.....

    May 02, 2017
  • Marco's picture

    I'm new to Arduino.

    I guess this would work as well?

    digitalWrite ( LM1, digitalRead ( RS ) ? HIGH : LOW );
    digitalWrite ( LM2, LOW );

    digitalWrite ( RM1, digitalRead ( LS ) ? HIGH : LOW );
    digitalWrite ( RM2, LOW );

    Clearly, I'm applying some knowledge from CS and electronics. One is C ternary operator, the other is simplification of boolean algebra expressions (

    May 18, 2017
  • S SUDHA's picture

    how to write a arduino code using analog for line follower robot.

    Jul 29, 2017
  • Chetan Dhudse's picture
    Chetan Dhudse

    Can you please provide me..the programming for arduino based WHITE line follower

    Aug 17, 2017
  • pk's picture

    I need a audrino program 6array line follower robot

    Sep 12, 2017
  • Surya's picture

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

    Sep 12, 2017
  • k7's picture

    Sir pls tell the circuit diagram for a ir array using arduino uno

    Sep 20, 2017
  • k7's picture

    Sir pls tell the circuit diagram for a ir array using arduino uno

    Sep 20, 2017
  • tariqnadeem's picture

    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

    Sep 21, 2017
  • AISHA's picture

    What is a waiter robot?

    Sep 21, 2017
  • MD Javed's picture
    MD Javed

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

    Oct 11, 2017
  • leonardo's picture

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

    Oct 11, 2017
  • gaurav 's picture

    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

    Oct 11, 2017

Leave a comment