Line Follower Robot using Arduino UNO: How to Build (Step-by-Step Guide)

Published  January 21, 2025   2
User Avatar RAJESH
Author
Arduino based Line Follower Robot
Arduino based Line Follower Robot

A Line Follower Robot (LFR) is one of the most popular Arduino robotics projects that teaches core concepts of automation and sensor integration. This step-by-step guide will show you how to build a professional-grade line follower robot using Arduino UNO, with complete code explanations and troubleshooting tips. Perfect for beginners and intermediate makers alike, this project combines hardware interfacing, sensor calibration, and motor control fundamentals. 

GIF showing Line Follower Robot in Action

To build this project you will need some basic knowledge of Arduino, the estimated build time for this project is 2-3 hours with beginner or intermediate skill level and the total cost for this project will be between $25 to $30. 

Components Required to Build a Line Follower Robot

  1. Arduino Uno - 1Nos

  2. L293D motor driver- 1Nos

  3. IR sensor module -2 Nos

  4. 7.4 or 9V battery -1 Nos

  5. BO motor - 2 Nos

  6. Motor wheel - 2 Nos

  7. Castor wheel - 1 Nos

  8. Hobby robot chassis - 1 Nos

  9. Wires

  10. Screws

Apart from the above components you will also need a few tools like, a soldering iron and solder, wire stripper, screwdriver set, hot glue gun, etc. Also, it is recommended to have a few additional components like a battery voltage monitor, power switch , and extra IR sensor module just in case you want to troubleshoot the robot later. 

Image showing components required to build a line follower robot

Working Principle of Line Following Robot

As stated earlier, a line follower robot (LFR) follows a line, and in order to follow a line, robot must detect the line first. Now the question is how to implement the line sensing mechanism in an LFR. We all know that the reflection of light on the white surface is maximum and minimum on the black surface because the black surface absorbs the maximum amount of light. So, we are going to use this property of light to detect the line. To detect light, either LDR (light-dependent resistor) or an IR sensor can be used.

 For this project, we are going with the IR sensor because of its higher accuracy. To detect the line, we place two IR sensors one on the left and the other on the right side of the robot as marked in the diagram below. We then place the robot on the line such that the line lies in the middle of both sensors. We have covered a detailed Arduino IR sensor tutorial which you can check to learn more about the working of IR sensors with Arduino Uno.

IR Sensor Module for line followinf robot

Infrared sensors consist of two elements, a transmitter and a receiver. The transmitter is basically an IR LED, which produces the signal and the IR receiver is a photodiode, which senses the signal produced by the transmitter. The IR sensors emits the infrared light on an object, the light hitting the black part gets absorbed thus giving a low output but the light hitting the white part reflects back to the transmitter which is then detected by the infrared receiver, thereby giving an analog output. Using the stated principle, we control the movement of the robot by driving the wheels attached to the motors, the motors are controlled by a microcontroller.

How does a Line Follower Robot Navigates?

A typical line follower robot has two sets of motors, let's call them left motor and right motor. Both motors rotate  on the basis of the signal received from the left and the right sensors respectively. The robot needs to perform 4 sets of motion which includes moving forward, turning left, turning right and coming to a halt. The description about the cases are given below.

Moving Forward:

Line Follower Robot Forward Navigation

In this case, when both the sensors are on a white surface and the line is between the two sensors, the robot should move forward, i.e., both the motors should rotate such that the robot moves in forward direction (actually both the motors should rotate in the opposite direction due to the placement of motors in our setup. But for the sake of simplicity, we will call the motors rotating forward.)

Turning LEFT:

Line Follower Left Navigation

In this case, the left sensor is on top of the dark line, whereas the right sensor is on the white part, hence the left sensor detects the black line and gives a signal, to the microcontroller. Since, signal comes from the left sensor, the robot should turn to the left direction. Therefore, the left motor rotates backwards and the right motor rotates in forward direction. Thus, the robot turns towards left side.

Turning RIGHT:

Line Follower Right Navigation

This case is similar to the left case, but in this situation only the right sensor detects the line which means that the robot should turn in the right direction. To turn the robot towards the right direction, the left motor rotates forward and the right motor rotates backwards and as a result, the robot turns towards the right direction.

Stopping:

Line Follower STOP Navigation

In this case, both the sensors are on top of the line and they can detect the black line simultaneously, the microcontroller is fed to consider this situation as a process for halt. Hence, both the motors are stopped, which causes the robot to stop moving.

Circuit Diagram and Assembling the Arduino Based Line Follower Robot

The circuit consists of mainly four parts: Two IR sensors, one motor drive, two motors, one Arduino, a battery and few connecting wires. The sensor senses the IR light reflected from the surface and feeds the output to the onboard op-amp comparator. When the sensor is situated over the white background, the light emitted by the sensor is reflected by the white ground and is received by the receiver. But when the sensor is above the black background, the light from the source is not reflected back to the sensor. The sensor senses the intensity of reflected light to give an output.

Arduino Line Follower Robot Circuit Diagram

 The sensor’s output is fed to the microcontroller, which gives commands to the motor driver to drive the motor accordingly. In our project, the Arduino Uno is programmed to make the robot move forward, turn right or turn left and stop according to the input coming from the sensor. The output of the Arduino is fed to the motor driver.

Why We Require a Motor Driver? 

The reason to use a motor driver here is because the output signal of an Arduino is not sufficient to drive the motor, furthermore, we need to rotate the motors in both directions, therefore we use a motor driver to drive the motor as required and also the motor driver is able to supply sufficient current to drive the motor. Here, we are using a L293D motor driver which is a dual h bridge motor driver and is sufficient for our 2 motors.

The L293D has 16 pins, the pinout of L293D is shown in the below diagram.

L293D Pinout

Connecting Motor Driver to Arduino UNO

Pin number 1 and 9 are the enable pins, we connect these two pins to a 5v input to enable the motor.

Pin number 1A, 2A, 3A, and 4A are the control pins.

For eg. The motor will turn to the right if the pin 1A goes low and 2A goes high, and the motor will turn to the left if 1A goes low and 2A high. So, we connect these pins to the output pins of the decoder.

Pins 1Y, 2Y, 3Y, and 4Y are the motor connection pins.

Note: Vcc2 is the motor driving voltage pin, and only used if you are using a high voltage motor.

Pin connection of Arduino Uno with the Motor driver are as follows: 

Motor Driver and Arduino Connections

Here, we are using a 7.4 li-ion battery to power the whole circuit. You can use any battery type from 6-12 volt. To move the robot, we need to use motors with low RPM but torque high enough to carry the weight of the robot. So, I chose two 60 RPM 6V Battery Operated, geared motors for this robot.

L298N Motor Driver for Line Following Robot 

When it comes to selecting the motor driver for a line-following robot we have two different options. One is the L293D module which we have discussed so far and the other is the L298N motor driver module. At the time of building this project the L293D module was the most popular option, but recently the cost of L298N motor driver module has greatly reduced so we are updating this project to use the L298N motor driver module. The circuit diagram to build a line following robot using L298N motor driver module is shown below 

Image showing the connections for Line following robot using L298N motor driver module

To make connections easy we have also marked the parts and labeled the wires, you can simply follow the above schematics and make the connections. Now that you understand the circuit for line following robot, lets actualy start to build the robot. 

Assembling the Line Follower Robot

Once we have understood the connection of all the components, we can start assembling our LFR. I have explained the step-by-step assembly procedure of the Robot in the video provided at the bottom of the page.

Arduino based Line Follower Robot

To make this robot, first we need a robot body; here I am using a homemade chassis. You can either use a readymade chassis or build one yourself. Now, place the BO motors to the chassis with the help of some hot glue as shown in the image below. 

Line Follower Motor Assembly

Next step is to place the motor driver on chassis and connect the motor wires to the output of the motor driver and bend the IR LED and sensor as shown in the image.

IR LED Sensor

Then place the sensors on the downside of the robot, adjust the sensors according to the track width and robot width. Remember that one sensor is for left side detection and another is for the right side detection.

DIY Arduino Uno Line Follower Robot

Now place the Arduino uno using glue and connect the sensor output pins to digital pin 2 and 4 of the Arduino.

Line Follower Robot using Arduino Uno

Connect the VCC pins to 5volt and the ground pins to ground. Now, connect the enable pins of the motor driver to pin 5 and 8 of Arduino and connect the motor driver input pins to pin number 6, 7, 9 and 10 of Arduino respectively. Finally, connect the battery with the circuit and place the battery on chassis. Here, I have connected everything with jumper wires. To make a permanent setup, you can directly solder everything together. Then turn the board upside down and with the help of hot glue gun, attach the castor wheels as shown in the image below.

Line Follower Robot Chasis Board

Finally, add the wheels. For extra safety, I have added a plastic sheet as a bumper too.

Arduino Line Follower Robot Working

Arduino Line Follower Robot Code

The programming part of line follower robot is very simple and we require only basic Arduino functions. The complete program for this project can be found at the bottom of this page. The explanation of this program is as follows:

First step is to defined every Arduino pin that we are using. I started with motoring the driver pins and sensor pins. Here, I have commented on each line of code for your easy understanding. 

 #define enA 5 //Enable1 L293 Pin enA
 #define in1 6 //Motor1  L293 Pin in1
 #define in2 7 //Motor1  L293 Pin in1
 #define in3 9 //Motor2  L293Pin in1
 #define in4 10 //Motor2  L293 Pin in1
 #define enB 8 //Enable2 L298 Pin enB
 #define R_S 4 // Right sensor
 #define L_S 2 // Left sensor

In the loop section, declare the pin modes of each pin. Here, we need to read the output of IR sensors, hence I have defined those pins as an input. The motor needs to be driven by the Arduino, thus defining the motor driver pins as output. Finally, I pulled enable pin to high.

pinMode(R_S, INPUT);
pinMode(L_S, INPUT);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enB, OUTPUT);
digitalWrite(enA, HIGH);
digitalWrite(enB, HIGH);

In the loop section, we first read the values of IR sensors and then using ‘if condition’ control the movement of motor as per our requirements. The four movement conditions are explained below.

Moving forward:

if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){forward();} 

In this case, if Right Sensor and Left Sensor are on top of white colour then the robot should move in the forward direction, so we call the forward function.

(Note : here 0 means output of IR sensor is high as the sensor is on white surface)

Turning towards Right Side:

if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){turnRight();} 

if Right Sensor is detecting Black and Left Sensor is not detecting any black line, then it will call the Right function, initiating the protocol for making right turn by calling turnRight function.

Turning towards Left side:

 if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){turnLeft();} 

if Right Sensor is over white and the Left Sensor is detecting Black then it will call turnLeft function. It will initiate the steps for turning the robot in left direction.

Stopping the Robot:

 if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){Stop();} 

If the Right Sensor and Left Sensor are on the Black color then it will call the Stop function. In this case, the robot will come to a complete halt.

We have defined the 4 functions of the robot as Forward, turnLeft, turnRight and Stop. The code to perform these functions are given below:

Forward function:

void forward(){ 
digitalWrite(in1, HIGH); 
 digitalWrite(in2, LOW); 
 digitalWrite(in3, LOW); 
 digitalWrite(in4, HIGH);
 }

This means we are pulling to high the input 1 and 4 of the motor driver, as a result, both motors will move forward  same as this I made other functions

Turn Right Function:

void turnRight(){
digitalWrite(in1, LOW);  //Right Motor forword Pin
digitalWrite(in2, HIGH); //Right Motor backword Pin 
digitalWrite(in3, LOW);  //Left Motor backword Pin
digitalWrite(in4, HIGH); //Left Motor forword Pin
 }

Turn Left Function:

void turnLeft(){
 digitalWrite(in1, HIGH); //Right Motor forword Pin
 digitalWrite(in2, LOW);  //Right Motor backword Pin
 digitalWrite(in3, HIGH); //Left Motor backword Pin
 digitalWrite(in4, LOW);  //Left Motor forword Pin
 }

Stop Function:

void Stop(){
 digitalWrite(in1, LOW); //Right Motor forword Pin 
digitalWrite(in2, LOW); //Right Motor backword Pin
digitalWrite(in3, LOW); //Left Motor backword Pin 
digitalWrite(in4, LOW); //Left Motor forward Pin

So, that’s all about coding. Now we need to upload the code to the Arduino and to do so, connect your Arduino with the computer using USB cable and upload this code using the Arduino IDE.

Testing and Calibrating Line Follower Robot

Arduino Line Follower Robot

We have assembled the robot and uploaded the code, so now its time to see it in action and if it is unable to follow the line then we’ll have to calibrate the robot. For that first place robot on a  black surface ( both sensors should be on top of the black surface) then adjust the variable resistor of IR Module until the output led of IR module become off. Next, place the robot on a white surface and check whether the led is turning on, if not, then just adjust the variable resistor. Repeat the process once again to be sure that the output LED is operating as per the requirement.

Now, since we have calibrated the robot, all we need to do is place the robot on top of the black line and see it in action. The below GIF shows the working of our line-follow robot . As you can see the the robot is able to follow the line without any problem. 

DIY Line Follower Robot

Troubleshooting Your Line Follower Robot

Even with careful assembly and coding, you might encounter some common issues with your line follower robot. Let's explore these problems and their solutions to help you get your robot running perfectly.

If your robot isn't following the line properly, first check your IR sensor calibration. Place your sensors over a white surface and adjust the potentiometer on the IR sensor module until the LED just turns off. Then test it over your black line - the LED should light up clearly. This sweet spot in calibration is crucial for reliable line detection.

When your robot moves but seems to ignore the line, check your sensor heights. If they're too far from the ground (more than 1cm), they won't detect the line reliably. Similarly, if they're too close (less than 2mm), they might get false readings. The ideal height is about 5mm from the surface.

Battery voltage significantly affects robot behavior. As batteries drain, motor speed and sensor sensitivity can change. Consider adding a voltage monitor or using a regulated power supply for consistent performance. Fresh batteries should measure between 7.4V to 9V for optimal performance.

Further Modifications and Upgrades

Once your basic line follower is working, there are several exciting upgrades you can implement to enhance its capabilities:

PID Control Implementation: Add PID (Proportional-Integral-Derivative) control to your code for smoother line following. This advanced control method helps your robot make more precise adjustments and handle curves better. It replaces the simple on-off control with proportional responses to line position.

Speed Control System: Add a potentiometer or switches to adjust speed of DC motor using Arduino. This is particularly useful when testing your robot on different courses. You can implement multiple speed modes for straight lines versus turns.

LCD Display Integration: Add a 16x2 LCD with Arduino display to show sensor readings, motor speeds, and battery voltage in real-time. This helps in debugging and makes your robot more interactive. You can display different parameters and even create a menu system for adjusting settings.

Additional Sensors: Consider adding a third IR sensor in the middle or ultrasonic sensors for obstacle detection. More sensors give your robot better awareness and allow for more complex behaviors like obstacle avoidance while line following.

The complete making of the line follower robot can also be found in the video linked at the bottom of this page. If you have any questions leave them in the comment section.

Frequently Asked Questions (FAQ) About Line Follower Robots

Q: What is a line following robot and how does it work? 

A: A line following robot is an autonomous robot designed to follow a visible line drawn on the ground. It uses infrared (IR) sensors to detect the line and follow it. When IR light hits a white surface, it reflects back to the sensor, but when it hits a black line, it absorbs the light. The robot uses this information to adjust its motors and follow the line

Q: What components do I need to build a basic line follower robot? 

A: the bare minimum components to build a line follower robot is a dev board like Arduino UNO (or equivalent), two IR sensors, an L293D motor driver, two DC gear motors (60-100 RPM), wheels, a chassis, a battery (7.4V or 9V), and connecting wires. The total cost is typically around $25-30, making it an affordable project for beginners.

Q: Which IR sensor is best for a line following robot? 

A: The TCRT5000 IR sensor module is highly recommended for line following robots due to its good detection range (2-30cm), built-in potentiometer for sensitivity adjustment, and stable performance. It's also widely available and compatible with Arduino's 5V operating voltage. But in this tutorial we have used the generic IR sensor module module because it is more cost effective.

Q: Why do we need a motor driver in a line follower robot? 

A: A motor driver is essential because Arduino's output pins can't provide enough current to drive DC motors directly. The L293D motor driver acts as an intermediary, providing the necessary current and voltage control for the motors while protecting the Arduino from potential damage.

Q: How can I improve my line follower robot's performance? 

A: You improve your line-following robot by implementing PID control for smoother movement, adding more sensors for better line detection, optimizing the sensor height (ideally 5-10mm from the ground), and ensuring proper battery voltage. 

Q: What are common problems with line follower robots and their solutions? 

A: Common issues include inconsistent line following (solution: adjust sensor calibration), jerky movements (solution: tune motor speeds or implement PID control), losing the line on sharp turns (solution: optimize sensor positioning or add more sensors), and erratic behavior (solution: check battery voltage and connections). But if you are facing more problems than this then you can write them in the comment section below and we will help you out. 

Q: Can a line follower robot detect different colored lines? 

A: While basic IR sensors work best with black lines on white surfaces, you can modify your robot to detect different colored lines by using color sensors (TCS230) instead of IR sensors. However, this increases complexity and cost and is not recommended. 

Q: What programming skills do I need to build a line follower robot? 

A: Basic knowledge of Arduino programming (C++) is sufficient. You should understand digital I/O, variables, if-else statements, and basic functions. Our tutorial includes fully commented code that's easy to understand and modify. Also if you are looking to learn programing or robotics this might be the best project to start your journey with. 

Projects Similar to Line Following Robot

Now that you have learned how to build a Line Following Robot using Arduino. Here are other similar Arduino Projects that you can try next. All these projects are built by us here in CircuitDigest and use more or less the same components that is used to build the line following Robot.

Obstacle Avoidance Robot using Arduino and Ultrasonic Sensor

Obstacle Avoidance Robot using Arduino and Ultrasonic Sensor

Obstacle Avoiding Robot is an intelligent device that can automatically sense the obstacle in front of it and avoid them by turning itself in another direction. This design allows the robot to navigate in an unknown environment by avoiding collisions, which is a primary requirement for any autonomous mobile robot.

Bluetooth-Controlled Car

Bluetooth-Controlled Car

This robot car can be controlled wirelessly with Bluetooth and an android app, and other than that we have placed RGB Neopixel LEDs on the front, back, and bottom of the robot to make it look cool. We will also build a custom android app with the MIT app inventor so that we can send commands directly via Bluetooth.

Fire Fighting Robot using Arduino

Fire Fighting Robot using Arduino

In this project, we will learn how to build a simple robot using Arduino that could move towards the fire and pump out water around it to put down the fire. It is a very simple robot that would teach us the underlying concept of robotics; you would be able to build more sophisticated robots once you understand the following basics.

Gesture Controlled Robot Using Arduino

Gesture Controlled Robot Using Arduino

Let's build a cool hand gesture-controlled robot that'll amaze your friends and family—it's like magic! This step-by-step guide helps you build a hand gesture-controlled robot using simple components like Arduino, MPU6050 Accelerometer, nRF24L01 Transmitter-Receiver Pair, and L293D motor driver module. We'll split the robot into two parts: the Transmitter and the Receiver.

Light Following Robot using Arduino UNO

Light Following Robot using Arduino UNO

Today, we are building a simple Arduino-based project: a light-following robot. This project is perfect for beginners, and we'll use LDR sensor modules to detect light and an MX1508 motor driver module for control.

Complete Project Code

// Arduino Line Follower Robot Code
//More Information: https://circuitdigest.com/microcontroller-projects/arduino-uno-line-follower-robot 
#define enA 5//Enable1 L293 Pin enA 
#define in1 6 //Motor1  L293 Pin in1 
#define in2 7 //Motor1  L293 Pin in1 
#define in3 9 //Motor2  L293 Pin in1 
#define in4 10 //Motor2  L293 Pin in1 
#define enB 8 //Enable2 L293 Pin enB 
#define R_S 4//ir sensor Right
#define L_S 2 //ir sensor Left
void setup(){ 
pinMode(R_S, INPUT); 
pinMode(L_S, INPUT); 
pinMode(enA, OUTPUT); 
pinMode(in1, OUTPUT); 
pinMode(in2, OUTPUT); 
pinMode(in3, OUTPUT); 
pinMode(in4, OUTPUT); 
pinMode(enB, OUTPUT);
digitalWrite(enA, HIGH); 
digitalWrite(enB, HIGH); 
delay(1000);
}
void loop(){  
if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){forward();}   //if Right Sensor and Left Sensor are at White color then it will call forword function
if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){turnRight();} //if Right Sensor is Black and Left Sensor is White then it will call turn Right function  
if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){turnLeft();}  //if Right Sensor is White and Left Sensor is Black then it will call turn Left function
if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){Stop();} //if Right Sensor and Left Sensor are at Black color then it will call Stop function
}
void forward(){  //forword
digitalWrite(in1, HIGH); //Right Motor forword Pin 
digitalWrite(in2, LOW);  //Right Motor backword Pin 
digitalWrite(in3, LOW);  //Left Motor backword Pin 
digitalWrite(in4, HIGH); //Left Motor forword Pin 
}
void turnRight(){ //turnRight
digitalWrite(in1, LOW);  //Right Motor forword Pin 
digitalWrite(in2, HIGH); //Right Motor backword Pin  
digitalWrite(in3, LOW);  //Left Motor backword Pin 
 digitalWrite(in4, HIGH); //Left Motor forword Pin 
}
void turnLeft(){ //turnLeft
digitalWrite(in1, HIGH); //Right Motor forword Pin 
digitalWrite(in2, LOW);  //Right Motor backword Pin 
digitalWrite(in3, HIGH); //Left Motor backword Pin 
digitalWrite(in4, LOW);  //Left Motor forword Pin 
}
void Stop(){ //stop
digitalWrite(in1, LOW); //Right Motor forword Pin 
digitalWrite(in2, LOW); //Right Motor backword Pin 
digitalWrite(in3, LOW); //Left Motor backword Pin 
digitalWrite(in4, LOW); //Left Motor forword Pin 
}
Video

Have any question realated to this Article?

Ask Our Community Members

Comments