Line Follower Robot using Arduino
A line follower robot is one of the most popular beginner robotics projects. It drives by itself along a black line on a light surface and turns left or right whenever it drifts off the line. In this guide you will build one using an Arduino Uno, two IR line sensors and an L298N motor driver.
You get the full parts list, wiring tables, ready-to-upload code, troubleshooting tips and upgrade ideas. Already built our obstacle avoiding robot? You can reuse the same chassis, motors and driver. New to Arduino? Read our essential Arduino sensors guide first, or browse more Arduino mini projects.
How a line follower robot works
The robot repeats three steps many times every second:
1. Sense
Each IR sensor shines infrared light at the floor and checks how much comes back. A black line reflects very little, a white surface reflects a lot.
2. Think
The Arduino reads the left and right sensors and decides where the line is: in the middle, on the left or on the right.
3. Act
If the line is in the middle, it drives straight. If the line moves to one side, it turns that way to get back on track.
Components required
Arduino Uno or compatible board
The brain of the robot. It reads the sensors and controls the motors.
2 IR line sensor modules
Each module has an IR LED and a receiver. It tells the Arduino whether it is over the black line or the white floor.
L298N motor driver module
The Arduino cannot power motors directly. The driver switches the motor current for it.
2WD robot chassis kit
The base that holds everything. Usually includes motors, wheels and a caster wheel.
Battery pack, 7 to 12 V
Powers the motors and the Arduino. Two 18650 cells in a holder (7.4 V) work well.
Jumper wires and switch
Connect the modules. A small slide switch makes it easy to turn the robot on and off.
Black tape and a white surface
The track. Stick 2 to 3 cm wide black electrical tape on a white floor, a white sheet or chart paper.
Wiring: pin connections
Use these pins so the code below works without changes. Keep the two jumper caps on the ENA and ENB pins of the L298N so the motors are always enabled. The code controls speed through the IN pins.
Circuit diagram: Arduino Uno, two IR sensors, L298N motor driver, two DC motors and battery. Pin positions are simplified for clarity. Scroll sideways on a phone.
IR sensors to Arduino
| 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 |
L298N to Arduino
| L298N pin | Arduino pin |
|---|---|
| IN1 | D5 |
| IN2 | D6 |
| IN3 | D9 |
| IN4 | D10 |
| GND | GND (common ground) |
Motors and battery
| Connection | Goes to |
|---|---|
| Left motor | L298N OUT1 and OUT2 |
| Right motor | L298N OUT3 and OUT4 |
| Battery + | L298N 12V terminal and Arduino VIN, through the switch |
| Battery − | L298N GND and Arduino GND |
Always connect the Arduino GND to the L298N GND. Without a common ground the robot will behave randomly. Wiring problems cause most failures, so see wrong sensor connections and incorrect power supply if something does not work.
Steering logic
| Left sensor | Right sensor | Where is the line? | Robot does |
|---|---|---|---|
| White | White | In the middle, between the sensors | Drive forward |
| Black | White | Under the left sensor | Turn left |
| White | Black | Under the right sensor | Turn right |
| Black | Black | Both on the line, for example a junction or the end | Stop |
Step-by-step assembly
Build the base
Fix the two motors and wheels to the chassis and add the caster wheel at the back or front.
Mount the electronics
Fix the Arduino and the L298N on the chassis with screws or double-sided tape. Keep the battery pack low and in the middle.
Fit the IR sensors
Mount both sensors at the front, facing down, about 1 cm above the floor. Space them slightly wider than the line so the line passes between them.
Wire the motors
Connect the left motor to OUT1 and OUT2 and the right motor to OUT3 and OUT4.
Wire the sensors and control pins
Follow the tables above for the IR sensors and the IN1 to IN4 pins.
Make the track
Stick a black line, 2 to 3 cm wide, on a white surface. Start with a straight line and gentle curves.
Add power and calibrate
Connect the battery through the switch. Turn the small screw (potentiometer) on each IR module until its LED reacts correctly: it should switch when the sensor moves between the white floor and the black line.
Arduino code
Open the Arduino IDE, paste the code, select your board and port, and click Upload. No extra library is needed.
Line follower robot
const int leftSensor = 2;
const int rightSensor = 3;
const int in1 = 5; // left motor
const int in2 = 6;
const int in3 = 9; // right motor
const int in4 = 10;
const int driveSpeed = 150; // straight speed, 0 to 255
const int turnSpeed = 130; // speed of the outer wheel in a turn
// Most IR modules output HIGH over a black line.
// If your robot reacts the wrong way, change this to false.
const bool lineIsHigh = true;
bool onLine(int pin) {
return (digitalRead(pin) == HIGH) == lineIsHigh;
}
// speed from -255 (reverse) to 255 (forward)
void setMotors(int left, int right) {
if (left >= 0) { analogWrite(in1, left); analogWrite(in2, 0); }
else { analogWrite(in1, 0); analogWrite(in2, -left); }
if (right >= 0) { analogWrite(in3, right); analogWrite(in4, 0); }
else { analogWrite(in3, 0); analogWrite(in4, -right); }
}
void setup() {
pinMode(leftSensor, INPUT);
pinMode(rightSensor, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
setMotors(0, 0);
}
void loop() {
bool left = onLine(leftSensor);
bool right = onLine(rightSensor);
if (!left && !right) {
setMotors(driveSpeed, driveSpeed); // line in the middle: go straight
} else if (left && !right) {
setMotors(0, turnSpeed); // line on the left: turn left
} else if (!left && right) {
setMotors(turnSpeed, 0); // line on the right: turn right
} else {
setMotors(0, 0); // both on the line: stop
}
}
How the code works
- onLine() reads a sensor and tells you if it is over the line. The lineIsHigh setting handles modules that work the other way round.
- setMotors() sets the speed and direction of each motor. Pins D5, D6, D9 and D10 all support PWM, so the IN pins can control speed.
- loop() checks both sensors and picks one of four moves: straight, left, right or stop.
- driveSpeed and turnSpeed are the two numbers to tune for your robot.
Test and tune your robot
Troubleshooting
| Problem | What to check |
|---|---|
| The robot does not move | Check the battery, the ENA and ENB jumper caps and the common ground. |
| It turns away from the line | Swap the left and right sensor wires, or set lineIsHigh to false. |
| It spins in circles | Swap the wires of one motor on the L298N. |
| It drives off the line on curves | Lower driveSpeed, and make sure the line is 2 to 3 cm wide with gentle curves. |
| The sensors give wrong readings | Adjust the potentiometer on each module, keep them about 1 cm from the floor and avoid sunlight. |
| Arduino resets when the motors start | The battery is weak or the supply is unstable. Use fresh cells. |
Upgrade ideas
Add a third sensor in the middle
A centre sensor makes junctions and sharp turns easier to handle.
Stop for obstacles
Add an ultrasonic sensor so the robot stops when something blocks the track.
Add Bluetooth control
Switch between line following and manual driving from your phone.
Show sensor status on a display
Add an LCD to see what each sensor is reading while you tune.
More ideas: see our Top 20 STEM projects and 20 Arduino mini projects.
Frequently asked questions
How many IR sensors does a line follower need?
Two sensors are enough for a simple robot. More sensors, for example three or five, make it smoother and let it handle sharp turns and junctions.
Can the robot follow a white line on a black floor?
Yes. Set lineIsHigh to false in the code, or recheck the potentiometer setting, so the logic matches your track.
What is the best line width?
Use 2 to 3 cm wide black tape. The two sensors should sit slightly wider apart than the line.
Can I use the obstacle avoiding robot chassis?
Yes. The motor and battery wiring is the same. Add the two IR sensors at the front and upload the line follower code.
Why does my robot wobble along the line?
It is correcting after every small drift. Lower the speed and keep the line in the middle between the sensors. Smoother control, such as a PID algorithm, is a good next step.
Robotics learning with Robopro.in
At Robopro.in, students and makers can find Arduino boards, sensors, motor drivers, motors and robotics kits for building practical projects.
Ready to build your line follower robot?
Get every part for this project in one place. Schools and STEM labs can also order in bulk.
Shop robotics kits Bulk orders for schools Contact usConclusion
A line follower robot teaches sensing, decision making and motor control in one fun project. Once it works, you can add more sensors, Bluetooth control or smarter steering and keep learning.