Blog

Line Follower Robot using Arduino

line-follower-robot

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.

🎯 LevelBeginner
⏱️ Build timeAbout 1 to 2 hours
🧩 BoardArduino Uno or compatible
🧠 You will learnIR sensors, motor control, steering logic

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.

🛞

2 DC gear motors with wheels

Drive the left and right wheels.

🚗

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.

Any stationery shop

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.

Line follower robot wiring diagram Arduino Uno connected to two IR line sensors, an L298N motor driver, two DC motors and a battery with a switch. Left IR sensor Right IR sensor Arduino Uno L298N Motor driver Left motor Right motor Battery 7-12 V + switch VCCGNDOUT VCCGNDOUT 5VGNDD2D3 D5D6D9D10 GNDVIN IN1IN2IN3IN4 GND12V OUT1OUT2OUT3OUT4 Red: power (+) Black: ground (-) Blue/green: sensor signals Purple: motor control

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 pinArduino pin
Left sensor VCC5V
Left sensor GNDGND
Left sensor OUTD2
Right sensor VCC5V
Right sensor GNDGND
Right sensor OUTD3

L298N to Arduino

L298N pinArduino pin
IN1D5
IN2D6
IN3D9
IN4D10
GNDGND (common ground)

Motors and battery

ConnectionGoes to
Left motorL298N OUT1 and OUT2
Right motorL298N 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 sensorRight sensorWhere is the line?Robot does
WhiteWhiteIn the middle, between the sensorsDrive forward
BlackWhiteUnder the left sensorTurn left
WhiteBlackUnder the right sensorTurn right
BlackBlackBoth on the line, for example a junction or the endStop

Step-by-step assembly

Step 1

Build the base

Fix the two motors and wheels to the chassis and add the caster wheel at the back or front.

Step 2

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.

Step 3

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.

Step 4

Wire the motors

Connect the left motor to OUT1 and OUT2 and the right motor to OUT3 and OUT4.

Step 5

Wire the sensors and control pins

Follow the tables above for the IR sensors and the IN1 to IN4 pins.

Step 6

Make the track

Stick a black line, 2 to 3 cm wide, on a white surface. Start with a straight line and gentle curves.

Step 7

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

🎚️ Lower the speed first. A slow robot follows better. Start with driveSpeed 120 and increase it step by step.
↔️ Check the sensor gap. If the robot leaves the line, move the sensors a little closer together or farther apart.
💡 Avoid direct sunlight. Strong light confuses IR sensors. Test indoors on a plain white surface.
🔁 If it turns the wrong way, swap the two sensor OUT wires, or change lineIsHigh in the code.

Troubleshooting

ProblemWhat to check
The robot does not moveCheck the battery, the ENA and ENB jumper caps and the common ground.
It turns away from the lineSwap the left and right sensor wires, or set lineIsHigh to false.
It spins in circlesSwap the wires of one motor on the L298N.
It drives off the line on curvesLower driveSpeed, and make sure the line is 2 to 3 cm wide with gentle curves.
The sensors give wrong readingsAdjust the potentiometer on each module, keep them about 1 cm from the floor and avoid sunlight.
Arduino resets when the motors startThe 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 us

Conclusion

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.

Keep reading

Leave a Reply

Your email address will not be published. Required fields are marked *