
How to Build a Line-Following Robot Step by Step
Building a line-following robot is one of the most exciting Arduino robotics projects for students, beginners, and electronics enthusiasts. It is a great way to learn about Arduino programming, infrared (IR) sensors, motor drivers, DC motors, and basic robotics.
A line-following robot is an autonomous robot car that detects and follows a black or white line drawn on a surface. It uses IR sensor modules to detect the line and an Arduino microcontroller to control the movement of its motors.
Whether you are a beginner learning robotics or a university student preparing an engineering project in Pakistan, this step-by-step guide will help you build your own Arduino line follower robot from scratch.
In this tutorial, we will use an Arduino Uno, two IR line-tracking sensors, an L298N motor driver, two DC geared motors, and a robot car chassis. You will also learn the circuit connections, Arduino code, working principle, testing process, and common troubleshooting solutions.
1. What Is a Line-Following Robot?
A line-following robot is an autonomous robotic vehicle that follows a predefined path using infrared sensors.
The path is usually a black line drawn on a white surface, although a white line on a dark surface can also be used.
The robot uses IR line-tracking sensors to identify the difference between the line and the surrounding surface. The Arduino Uno processes the sensor signals and sends commands to the L298N motor driver to control the left and right DC motors.
For example:
-
When the robot detects the line in the centre, it moves forward.
-
When the line moves towards the left sensor, the robot turns left.
-
When the line moves towards the right sensor, the robot turns right.
-
When the robot loses the line, it attempts to search for it again.
This simple working principle makes a line follower robot an excellent beginner robotics project.
2. Components Required to Build a Line-Following Robot
Before starting the assembly, collect the following electronic components.
| Component | Quantity | Purpose |
|---|---|---|
| Arduino Uno | 1 | Main controller |
| IR Line Tracking Sensor | 2 | Detects black line |
| L298N Motor Driver | 1 | Controls DC motors |
| DC Geared Motors | 2 | Drives robot wheels |
| Robot Car Chassis | 1 | Holds components |
| Wheels | 2 | Robot movement |
| Caster Wheel | 1 | Balance and support |
| Battery Pack | 1 | Power supply |
| Jumper Wires | As required | Electrical connections |
| Power Switch | 1 | Turns robot ON/OFF |
You can find Arduino development boards, IR sensors, motor drivers, DC motors, and other robotics accessories through electronics component stores in Pakistan, including Electroway.org.
3. Understanding the Working Principle of a Line Follower Robot
The line-following robot works using infrared light reflection.
An IR line-tracking sensor generally contains two main parts:
-
IR LED transmitter: Sends infrared light towards the surface.
-
IR receiver: Detects the reflected infrared light.
A white surface generally reflects more infrared light, while a black line absorbs more of it.
The sensor module processes this reflected signal and provides a digital output to the Arduino.
The Arduino reads the sensor outputs and decides which motor should move faster or slower.
For this project, we will use two IR sensors:
These sensors will be mounted at the front of the robot, close to the ground.
4. Circuit Diagram and Arduino Pin Connections
Now let’s connect the Arduino Uno, IR sensors, and L298N motor driver.
A. IR Sensor Connections
Connect the two IR tracking sensors to Arduino Uno.
| IR Sensor Pin | Arduino Pin |
|---|---|
| Left Sensor VCC | 5V |
| Left Sensor GND | GND |
| Left Sensor OUT | D2 |
| Right Sensor VCC | 5V |
| Right Sensor GND | GND |
| Right Sensor OUT | D3 |
B. L298N Motor Driver Connections
The L298N motor driver controls the direction and speed of both DC motors.
| L298N Pin | Arduino Pin |
|---|---|
| ENA | D5 (PWM) |
| IN1 | D8 |
| IN2 | D9 |
| IN3 | D10 |
| IN4 | D11 |
| ENB | D6 (PWM) |
| GND | Arduino GND |
C. DC Motor Connections
| Motor Driver Output | Connection |
|---|---|
| OUT1 | Left Motor Terminal 1 |
| OUT2 | Left Motor Terminal 2 |
| OUT3 | Right Motor Terminal 1 |
| OUT4 | Right Motor Terminal 2 |
D. Power Supply Connections
-
Connect the suitable motor battery positive terminal to the L298N motor supply input.
-
Connect battery negative to L298N GND.
-
Connect Arduino GND to L298N GND.
-
Power the Arduino through a suitable USB or regulated power source.
Important: All circuit grounds must be connected together. Do not connect the motor supply voltage directly to Arduino 5V. Also, make sure the battery voltage is suitable for your motors and L298N module.
For PWM speed control, remove the ENA and ENB jumpers if your L298N module has them fitted, and connect the enable pins to Arduino D5 and D6.
5. Positioning the IR Sensors
Sensor placement is very important for proper line-following robot operation.
Follow these instructions:
-
Install the left IR sensor on the front-left side of the robot.
-
Install the right IR sensor on the front-right side.
-
Keep both sensors close to the ground.
-
Position them so that the black line can pass between or underneath their sensing areas.
-
Adjust the sensor potentiometers until they reliably distinguish black and white surfaces.
The ideal sensor height depends on your IR sensor module. Start around 5–10 mm above the surface and adjust according to the manufacturer’s recommendations.
6. Line-Following Robot Working Logic
Before uploading the Arduino program, understand how the robot will respond to sensor signals.
For this example, we will assume that the IR sensor output is:
-
LOW (0) = Black line detected
-
HIGH (1) = White surface detected
Some IR sensor modules use the opposite logic, so test your sensors before running the robot.
| Left Sensor | Right Sensor | Robot Action |
|---|---|---|
| LOW | LOW | Move Forward |
| LOW | HIGH | Turn Left |
| HIGH | LOW | Turn Right |
| HIGH | HIGH | Search for Line |
This logic allows the robot to follow a black line on a white surface.
If both sensors detect black, the robot moves forward. If the left sensor detects black, the robot steers left. If the right sensor detects black, it steers right.
When both sensors detect white, the robot has lost the line and will search in the last known direction.
7. Arduino Line-Following Robot Code
Now it’s time to program your Arduino Uno.
The following Arduino code uses two digital IR sensors and an L298N motor driver. It includes forward movement, left and right correction, and line recovery when both sensors lose the black line.
// Arduino Line Following Robot
// Arduino UNO + 2 IR Sensors + L298N
// IR Sensor Pins
#define LEFT_SENSOR 2
#define RIGHT_SENSOR 3
// L298N Motor Driver Pins
#define ENA 5
#define IN1 8
#define IN2 9
#define ENB 6
#define IN3 10
#define IN4 11
// Motor Speeds (0-255)
int forwardSpeed = 140;
int turnSpeed = 130;
// Sensor logic
// LOW = Black line
// HIGH = White surface
int lastDirection = 0;
// -1 = Left, 1 = Right
void setup() {
pinMode(LEFT_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
Serial.begin(9600);
stopMotors();
}
void loop() {
int leftValue = digitalRead(LEFT_SENSOR);
int rightValue = digitalRead(RIGHT_SENSOR);
Serial.print("Left: ");
Serial.print(leftValue);
Serial.print(" Right: ");
Serial.println(rightValue);
if (leftValue == LOW && rightValue == LOW) {
moveForward();
}
else if (leftValue == LOW && rightValue == HIGH) {
turnLeft();
lastDirection = -1;
}
else if (leftValue == HIGH && rightValue == LOW) {
turnRight();
lastDirection = 1;
}
else {
// Both sensors lost the line
searchLine();
}
}
// Move both motors forward
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, forwardSpeed);
analogWrite(ENB, forwardSpeed);
}
// Turn left
void turnLeft() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, turnSpeed);
}
// Turn right
void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, turnSpeed);
analogWrite(ENB, 0);
}
// Search for the lost line
void searchLine() {
if (lastDirection == -1) {
turnLeft();
}
else if (lastDirection == 1) {
turnRight();
}
else {
stopMotors();
}
}
// Stop both motors
void stopMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
Important code adjustments:
-
If your IR sensors detect black as HIGH instead of LOW, reverse the sensor conditions in the program.
-
If one motor rotates backward, reverse its two motor wires or its direction logic.
-
Adjust
forwardSpeedandturnSpeedaccording to your robot’s weight and motor specifications. -
The example uses two sensors, so it is suitable for a simple line-following robot. More sensors can improve tracking accuracy and corner handling.
8. How to Upload the Program to Arduino Uno
Follow these steps to upload your line-following robot program:
-
Download and install the Arduino IDE on your computer.
-
Connect the Arduino Uno to your laptop using a USB cable.
-
Open Arduino IDE.
-
Copy the line-following robot code into the editor.
-
Select Tools → Board → Arduino AVR Boards → Arduino Uno.
-
Select the correct COM port.
-
Click the Verify button to check the code.
-
Click Upload to transfer the program to Arduino.
After successful uploading, disconnect USB if you are changing to your external power arrangement, check your wiring, and power the robot.
Official software: Download Arduino IDE.
9. Testing Your Line-Following Robot
After completing the wiring and uploading the program, test your robot on a flat white surface with a black electrical tape track.
Perform these tests:
Test 1: Straight Line
Place the robot on a straight black line. It should move forward while keeping the line between its sensors.
Test 2: Left Turn
Create a left curve in the track. The robot should adjust its direction towards the left.
Test 3: Right Turn
Create a right curve. The robot should steer towards the right.
Test 4: Line Recovery
Move the robot away from the line. Both sensors should lose the black line, and the robot should search in its last known direction.
Start with a slow motor speed. Increase the speed gradually after the robot follows the track reliably.
10. Common Problems and Their Solutions
Even after connecting all components correctly, your Arduino line-following robot may not work perfectly on the first attempt.
Here are some common problems and their solutions.
| Problem | Possible Solution |
|---|---|
| Robot does not move | Check battery, motor wiring and L298N connections |
| Robot moves backward | Reverse motor wires or direction logic |
| Robot turns in the wrong direction | Swap sensor connections or correct steering logic |
| IR sensors do not detect black | Adjust sensor potentiometers |
| Robot moves too fast | Reduce motor PWM speed |
| Robot loses the line | Adjust sensor placement and turning speed |
| One motor is slower | Check motor condition and power supply |
| Arduino resets frequently | Check battery capacity and power wiring |
Why Is My Line-Following Robot Not Following the Black Line?
The most common reasons include incorrect IR sensor calibration, wrong sensor logic, poor sensor positioning, incorrect motor polarity, and excessive motor speed.
Use the Arduino Serial Monitor at 9600 baud to check the sensor readings.
Place the sensors over a white surface and then over a black line. Confirm that the readings match the logic used in your code.
11. How to Improve Your Arduino Line-Following Robot
Some useful upgrades include:
-
Five-channel IR sensor array: Detects the line more accurately and handles curves better.
-
PID control: Improves steering accuracy and reduces unnecessary movement.
-
Motor speed optimization: Adjusts motor speeds for smoother movement.
-
Rechargeable battery: Provides a convenient portable power solution.
-
Bluetooth control: Allows additional wireless monitoring and manual control.
-
ESP32 upgrade: Can add Wi-Fi and Bluetooth connectivity for advanced robotics experiments.
A PID-based line follower uses proportional, integral, and derivative calculations to adjust motor speeds based on how far the robot is from the centre of the line. This is a useful next step after mastering basic two-sensor control.
12. Applications of Line-Following Robots
Line-following robots are not only educational projects. They also demonstrate important principles used in industrial automation and autonomous navigation.
Some applications include:
-
Educational robotics competitions
-
Engineering and university projects
-
Robotics learning laboratories
-
Automated material transportation demonstrations
-
Industrial automation prototypes
-
Autonomous vehicle research
-
Warehouse navigation experiments
These applications make line-following robots an excellent introduction to robotics engineering.
Frequently Asked Questions (FAQs)
1. Which Arduino board is best for a line-following robot?
Arduino Uno is an excellent choice for a basic line-following robot because it provides sufficient digital pins and processing capability for two or more IR sensors and an L298N motor driver.
2. How many IR sensors are needed for a line follower robot?
A basic line-following robot can work with two IR sensors. However, three-, five-, or more-channel IR sensor arrays can provide better line detection and tracking accuracy.
3. Can I make a line-following robot without Arduino?
Yes. Other microcontrollers and dedicated control circuits can operate line-following robots. However, Arduino Uno is popular because it is easy to program and has extensive learning resources.
4. Why does my Arduino line follower move in the wrong direction?
This usually happens because the motor wires are reversed or the motor direction logic is incorrect. Check the L298N output connections and adjust the motor direction commands.
5. Can I use an L298N motor driver for a line-following robot?
Yes. The L298N is a commonly used dual H-bridge motor driver that can control two DC motors independently. Make sure its voltage and current ratings are suitable for your motors.
6. Which surface is best for a line-following robot?
A smooth white surface with a clearly visible black line is a good starting point. High-contrast tracks help IR sensors distinguish the line from the surrounding surface.
7. What is the difference between a two-sensor and five-sensor line follower?
A two-sensor robot uses simple left and right detection to control steering. A five-sensor robot can detect more positions across the track, allowing more accurate steering and better support for advanced control methods such as PID.
Conclusion
Building a line-following robot using Arduino Uno is an excellent way to learn practical robotics, electronics, and programming.
With just two IR sensors, an L298N motor driver, two DC geared motors, and an Arduino Uno, you can create a robot that automatically follows a black line.
Start with the basic circuit and program explained in this tutorial. Once your robot works reliably, experiment with additional IR sensors, motor speed adjustments, and PID control to improve its performance.
If you are looking for Arduino boards, IR sensors, L298N motor drivers, DC geared motors, batteries, and other robotics components in Pakistan, explore Electroway.org. You can also explore the Electroway app for your electronics component shopping needs.
Keep building, keep experimenting, and turn your robotics ideas into reality!
