Line follower robot using Arduino uno

Line Follower Robot Project for Kids | Arduino Step-by-Step Guide
UNO
Arduino Robotics Project

Build a Robot That
Follows a Line 🤖

Two tiny IR sensors and an Arduino UNO are all it takes to build a robot that drives itself along a black track — no remote control needed!

🎓 Ages 11–16
⏱️ ~3 Hours
🔧 Easy–Medium
💰 ₹500–₹800
The Big Idea

How does a line follower robot work? 🧠

The robot has two IR (infrared) sensors mounted on its belly, pointing down at the floor. Each sensor sends out invisible infrared light and checks if it bounces back.

White surface → light bounces back strongly → sensor reads LOW (0).
Black line → light is absorbed, barely bounces back → sensor reads HIGH (1).

The Arduino reads both sensors 100+ times per second and decides which way to steer the motors — forward, turn left, turn right, or stop. That's it! The robot "thinks" about 100 times every second to stay on the line.

Sensor over white

WHITE SURFACE

IR light bounces back

OFF (reads 0)

Sensor over black

BLACK LINE

IR light is absorbed

ON (reads 1)
Decision Brain

What the robot decides — the logic table 📊

This is the "brain" of the robot. The Arduino checks these four possible combinations of sensor readings and tells the motors what to do:

Left Sensor Right Sensor What it means Motor action
0 (white) 0 (white) Both on white — robot is on the line! ⬆ GO FORWARD
1 (black) 0 (white) Left is on black — drifted left of line ↪ TURN RIGHT to correct
0 (white) 1 (black) Right is on black — drifted right of line ↩ TURN LEFT to correct
1 (black) 1 (black) Both on black — reached junction or end ⬛ STOP
💡 The logic above assumes the sensors output HIGH (1) when over the black line. Some sensors are inverted — output LOW over black. If your robot turns the wrong way, just flip the sensor readings in the code!
Shopping List

Components you need 🛒

All parts available at your local electronics shop or online. Total cost: roughly ₹500–₹800.

01
Arduino UNO
The brain — reads sensors, controls motors
02
L298N Motor Driver
Controls speed & direction of two motors
03
IR Sensor Modules × 2
Detects black line vs white surface
04
BO DC Motors × 2
Geared plastic motors, ~200 RPM
05
Wheels × 2 + Tyre
Fit onto BO motor shafts
06
Castor/Ball Wheel × 1
Front support wheel — lets robot turn freely
07
Robot Chassis (Acrylic)
The body — 2-layer acrylic plate works great
08
9V Battery + Clip
Powers Arduino and motors
09
Breadboard + Jumper Wires
For connecting everything neatly
10
USB Cable (Type-B)
To upload code to Arduino from computer
11
Black Electrical Tape / Chart
Make your track on white cardboard!
12
Screws, Standoffs, Nut Set
To mount motors and boards on chassis
💡 Track tip: Use 2-inch wide black electrical tape on white A3 chart paper. Make curves gradual — sharp turns confuse the robot at high speed!
Circuit Design

How to connect everything 🔌

Study this circuit carefully before wiring. One wrong wire can make the motors go backward or damage the board!

Line Follower Robot Circuit Diagram ARDUINO UNO D2 → D3 → D4 → D5 → D6 → D7 → ← A0 (IR-L) ← A1 (IR-R) 5V GND USB-B L298N MOTOR DRIVER IN1 IN2 IN3 IN4 ENA(D5) ENB(D6) OUT-A → Left Motor OUT-B → Right Motor VCC=9V GND 5V-out IR SENSOR LEFT VCC GND OUT IR SENSOR RIGHT VCC GND OUT LEFT MOTOR BO DC motor RIGHT MOTOR BO DC motor 9V BATTERY VCC → L298N 12V pin + Arduino Vin WIRE LEGEND: IR Left signal IR Right signal Control signals Motor power Battery supply Mount 5-8mm above ground

Complete Pin Connection Table 📌

FromPin / TerminalToPin / TerminalNotes
IR Sensor LeftVCCArduino5VPowers the sensor
IR Sensor LeftGNDArduinoGNDCommon ground
IR Sensor LeftOUTArduinoA0Reads HIGH over black line
IR Sensor RightVCCArduino5VPowers the sensor
IR Sensor RightGNDArduinoGNDCommon ground
IR Sensor RightOUTArduinoA1Reads HIGH over black line
ArduinoD2L298NIN1Left motor direction bit A
ArduinoD3L298NIN2Left motor direction bit B
ArduinoD4L298NIN3Right motor direction bit A
ArduinoD7L298NIN4Right motor direction bit B
ArduinoD5 (PWM)L298NENALeft motor speed (PWM)
ArduinoD6 (PWM)L298NENBRight motor speed (PWM)
L298NOUT1, OUT2Left Motor+ –Drive left wheel
L298NOUT3, OUT4Right Motor+ –Drive right wheel
9V Battery+L298N12VMotor power supply
L298N5V outArduinoVINL298N's onboard regulator powers Arduino!
L298NGNDArduinoGNDCommon ground — very important!
⚠️ Motor direction: If a motor spins the wrong way when you test, just swap its two wires (OUT1↔OUT2 or OUT3↔OUT4). No need to change the code!
Build Guide

Step-by-step instructions 🔧

Follow these steps in order. Don't skip the testing steps — they save a lot of frustration!

01

Assemble the chassis

Attach both BO motors to the bottom plate of the acrylic chassis using the motor brackets and screws. Fit the wheels onto the motor shafts. Attach the castor (ball wheel) at the front — this keeps the robot balanced and lets it turn smoothly.

💡 The castor wheel goes at the front, the two driven wheels at the back. This is called a differential drive setup!
02

Mount the boards on the second layer

Use standoffs (small brass spacers) to mount the Arduino UNO and L298N Motor Driver on the top layer of the chassis. Keep them apart so their pins don't touch. Use M3 screws to secure them firmly — a board that wobbles causes bad connections!

💡 Place the L298N near the back edge — its motor output terminals (OUT1-OUT4) should be easy to reach with wires going down to the motors.
03

Mount the IR sensors under the chassis

Attach both IR sensor modules to the underside of the bottom plate using small screws or cable ties. Position them at the very front, about 5–8 mm above the ground, and space them so they are about 2 cm apart — one on the left, one on the right of centre.

⚠️ Height matters a lot! Too high (more than 12 mm) and the sensors can't tell black from white. Too low (touching ground) and they'll scratch. Adjust after mounting.
04

Connect the motors to L298N

Connect the left motor's two wires to L298N's OUT1 and OUT2 terminals. Connect the right motor's two wires to OUT3 and OUT4. Tighten the screw terminals firmly — a loose motor wire causes stuttering and strange behaviour!

💡 Don't worry about which wire is + or – yet. If the motor spins backward, you'll swap the wires after your first test.
05

Wire the L298N to Arduino

Connect IN1→D2, IN2→D3, IN3→D4, IN4→D7, ENA→D5, ENB→D6 using jumper wires. Then connect L298N's GND to Arduino GND, and L298N's 5V output pin to Arduino's VIN — the L298N's built-in voltage regulator powers your Arduino from the 9V battery!

⚠️ Make sure the ENA and ENB jumpers on the L298N are REMOVED — you'll be controlling speed from the Arduino's D5 and D6 PWM pins instead.
06

Connect the IR sensors to Arduino

Left sensor: VCC→5V, GND→GND, OUT→A0. Right sensor: VCC→5V, GND→GND, OUT→A1. Use the breadboard to share the 5V and GND connections neatly between both sensors.

💡 IR sensors have a small potentiometer (blue trimmer screw) on them. You'll adjust this in the calibration step to set the detection sensitivity.
07

Connect the battery

Connect the 9V battery clip's red wire (+) to L298N's 12V terminal, and the black wire (–) to L298N's GND terminal. Do NOT connect the battery yet — wait until after uploading the code!

⚠️ Always connect the battery last and disconnect it first when making changes to wiring. Connecting a battery to the wrong pins can fry your Arduino instantly!
08

Upload the code

Connect the Arduino to your computer via USB cable. Open Arduino IDE, paste the code from the next section, and click the Upload button (→ arrow). Wait for "Done uploading" message. Then disconnect the USB cable.

💡 If you get a "port not found" error, you need to install the CH340 USB driver for your Arduino UNO clone. Search "CH340 driver" online and install it.
09

Calibrate the IR sensors

Connect the battery. Place the robot on white paper. Use a small screwdriver to turn the blue potentiometer on each IR sensor module. The red indicator LED on the sensor should be ON when over white and OFF when you place black tape under it. Adjust until the switch is crisp and reliable.

💡 Test calibration by slowly sliding a piece of black tape under each sensor. The sensor LED should turn OFF exactly when the tape is underneath it. If it's sluggish, keep adjusting!
10

Test on your track!

Make a track on white A3 chart paper with 2-inch black electrical tape. Keep curves smooth and gradual. Place the robot on the line, make sure sensors straddle the line (one on each side), turn it on and let go! Fine-tune motor speed values in the code if the robot overshoots corners.

🎉 If the robot goes straight but misses turns, try reducing the SPEED_TURN value to make turns sharper. If it wiggles too much on straight sections, increase BASE_SPEED slightly!
Arduino Code

The code that makes it think 💻

Every section is explained with comments. Adjust the speed values at the top to tune your robot's performance on different track shapes.

📦 No extra libraries needed! This code uses only the built-in Arduino functions. Just open Arduino IDE, paste, and upload.
line_follower_robot.ino
// =========================================================
//  LINE FOLLOWER ROBOT — Arduino UNO
//  Two IR sensors + L298N Motor Driver
//  Written in a kids-friendly style with full comments!
// =========================================================

// ─── MOTOR DRIVER PINS ───────────────────────────────────
#define IN1  2    // Left motor direction A
#define IN2  3    // Left motor direction B
#define IN3  4    // Right motor direction A
#define IN4  7    // Right motor direction B
#define ENA  5    // Left motor speed  (PWM pin!)
#define ENB  6    // Right motor speed (PWM pin!)

// ─── SENSOR PINS ─────────────────────────────────────────
#define IR_LEFT  A0   // Left IR sensor output
#define IR_RIGHT A1   // Right IR sensor output

// ─── SPEED SETTINGS (0-255) ──────────────────────────────
// ⚡ Tune these if your robot goes too fast/slow
const int BASE_SPEED  = 180;  // Forward speed
const int TURN_SPEED  = 160;  // Outer wheel in a turn
const int SLOW_SPEED  = 80;   // Inner wheel in a turn (braking)

// ─── SENSOR THRESHOLD ────────────────────────────────────
// Analog value above this = sensor detects BLACK LINE
const int THRESHOLD = 500;   // Range 0-1023; adjust if needed

// =========================================================
//  SETUP — runs once when Arduino turns on
// =========================================================
void setup() {
  // Set motor control pins as outputs
  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);

  // Sensor pins are inputs (default, but good to be explicit)
  pinMode(IR_LEFT,  INPUT);
  pinMode(IR_RIGHT, INPUT);

  Serial.begin(9600);
  Serial.println("Line Follower Robot Ready!");

  // Small delay before starting — gives you time to place the robot
  delay(2000);
}

// =========================================================
//  LOOP — runs forever, making decisions 100x per second
// =========================================================
void loop() {
  // Read both IR sensors (analog 0–1023)
  int leftVal  = analogRead(IR_LEFT);
  int rightVal = analogRead(IR_RIGHT);

  // Convert to simple true/false: is sensor over the black line?
  bool leftOnLine  = (leftVal  > THRESHOLD);
  bool rightOnLine = (rightVal > THRESHOLD);

  // Print sensor values to Serial Monitor for debugging
  Serial.print("L:"); Serial.print(leftVal);
  Serial.print("  R:"); Serial.println(rightVal);

  // ─── DECISION MAKING ─────────────────────────────────

  if (!leftOnLine && !rightOnLine) {
    // Both sensors on WHITE → go straight!
    goForward();
  }
  else if (leftOnLine && !rightOnLine) {
    // LEFT sensor on black → robot drifted left → turn RIGHT
    turnRight();
  }
  else if (!leftOnLine && rightOnLine) {
    // RIGHT sensor on black → robot drifted right → turn LEFT
    turnLeft();
  }
  else {
    // BOTH sensors on black → junction or end → STOP
    stopMotors();
  }

  delay(5);   // Tiny delay to avoid sensor noise
}

// =========================================================
//  MOTOR CONTROL FUNCTIONS
// =========================================================

void goForward() {
  // Left motor FORWARD
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  // Right motor FORWARD
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
  // Set full speed on both wheels
  analogWrite(ENA, BASE_SPEED);
  analogWrite(ENB, BASE_SPEED);
  Serial.println("→ FORWARD");
}

void turnLeft() {
  // Left motor SLOW (inner wheel)
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  // Right motor FAST (outer wheel)
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
  analogWrite(ENA, SLOW_SPEED);   // Left slows down
  analogWrite(ENB, TURN_SPEED);   // Right speeds up
  Serial.println("↩ TURN LEFT");
}

void turnRight() {
  // Left motor FAST (outer wheel)
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  // Right motor SLOW (inner wheel)
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
  analogWrite(ENA, TURN_SPEED);   // Left speeds up
  analogWrite(ENB, SLOW_SPEED);   // Right slows down
  Serial.println("↪ TURN RIGHT");
}

void stopMotors() {
  // Pull both motor directions LOW = brake
  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
  Serial.println("⬛ STOP");
}
Try It!

Interactive Robot Simulator 🎮

Move the robot around and see exactly what both sensors read and what decision the Arduino makes — just like it does in real life!

🤖 Live Robot Brain Simulator
🤖
LEFT SENSOR ── WHITE
RIGHT SENSOR ── WHITE
DECISION ⬆ FORWARD
LEFT MOTOR 180 / 255
RIGHT MOTOR 180 / 255
Level Up!

Cool upgrades to try 🚀

Once your basic robot follows the line smoothly, try these exciting enhancements!

📡

3-Sensor Array

Add a third centre sensor for smoother line tracking — especially good for sharp S-curves

🧮

PID Control

Upgrade from simple on/off logic to a PID algorithm — the robot will glide around curves without wobbling

📟

OLED Display

Show real-time sensor values and current action on a tiny 0.96" OLED screen mounted on the robot

🚧

Obstacle Avoider

Add an HC-SR04 ultrasonic sensor at the front to pause the robot when something blocks its path

📱

Bluetooth Override

Add an HC-05 Bluetooth module to manually control the robot from your phone, then release it back to line-follow mode

Speed Racer Mode

Use an 11.1V LiPo battery and motor encoders to calculate and maximise speed around a competition track

Questions & Answers

Questions kids always ask ❓

Why does the robot turn in the OPPOSITE direction to the sensor that sees the black line?
This is the cleverest part! Think of it like walking on a tightrope. If your LEFT foot goes too far left and slides off the rope, you lean RIGHT to get back on. The robot does the same — if the LEFT sensor falls onto the black line, it means the robot has drifted left of centre, so it turns RIGHT to self-correct and get back on track.
What is PWM and why do we use it to control motor speed?
PWM stands for Pulse Width Modulation. Instead of changing voltage (which is hard), the Arduino turns the power to the motor ON and OFF very rapidly — hundreds of times per second! If it's ON 70% of the time, the motor gets 70% of full speed. If it's ON 100%, the motor goes full speed. The analogWrite() function does all of this automatically. The motor's own mass and inertia "smooths out" the pulses into a steady spin!
Why do we need the L298N motor driver? Can't we just connect motors directly to Arduino?
Absolutely not! A DC motor can draw 200–600 mA of current when running, and even more when it starts. Arduino's output pins can only safely provide about 40 mA before they get damaged. The L298N is a power amplifier — it takes the Arduino's small signal (like a whisper) and uses the battery's full power (like a shout) to drive the motors. It also lets you reverse motor direction, which Arduino pins alone cannot do.
My robot wiggles from side to side even on a straight line. How do I fix it?
This is called "oscillation" and it's very common! There are three fixes. First, reduce your BASE_SPEED — a slower robot has more time to react smoothly. Second, increase the SLOW_SPEED during turns so the inner wheel doesn't brake too harshly. Third, add a small delay() between sensor readings — this stops the robot reacting to tiny sensor noise. The best fix for serious wobble is implementing a PID controller, which is an advanced upgrade.
Can I use coloured tape instead of black tape for the track?
Yes and no! IR sensors respond to how much infrared light is reflected, not to visible colour. Dark colours (dark blue, dark green, black) absorb a lot of IR light and work well as the "line". Very light colours (white, yellow, light grey) reflect IR strongly and work as the "background". Avoid shiny surfaces — they can reflect IR unpredictably regardless of colour. Red tape might work, but test it first by holding your sensor over it and checking the LED.
What is the "analog" reading and why does the code use THRESHOLD = 500?
The analogRead() function returns a value from 0 to 1023, representing how much light the sensor sees. Over a shiny white surface, it returns a high number (maybe 800-1023). Over a black line, it returns a low number (maybe 100-300). The THRESHOLD of 500 is the "cut-off point" in the middle — any reading above 500 means black, below 500 means white. You can adjust this value up or down if your sensors behave differently. Open the Serial Monitor to see the actual numbers your sensors return!
Is this similar to how self-driving cars work?
Yes! Our robot is a tiny version of the same concept. Real self-driving cars use cameras and AI instead of IR sensors, but the core idea is the same: sense your surroundings, decide which way to steer, act on that decision, repeat thousands of times per second. Tesla, Waymo, and other self-driving systems are essentially doing a much more complex version of exactly what our little line follower does — just with LIDAR, cameras, GPS, and neural networks instead of two IR sensors!

Line Follower Robot — Arduino Robotics Project for Kids

From IR sensors to motor control — you just built your first autonomous robot 🤖

Perfect for school science fairs, robotics clubs, and STEM competitions

Keywords: line follower robot arduino project kids, IR sensor L298N motor driver tutorial, autonomous robot school project India, STEM robotics line tracking step by step

Comments

Product Cards
Buddy Bot eBook
⭐ New 2026 Release
Build Your
Own Robot!
3D design, wiring &
Arduino coding.
Young inventors love it!
🖨️
3D Print
All parts
Wire it
Circuit guide
💻
Code it
Arduino IDE
🤖
Watch it
Walk & react
📋 Your Details
Enter your name
Valid 10-digit no.
Enter a valid email
Special Website Offer
₹499 300
🌍 International: $5 USD
One-time · Instant digital delivery
🔒 Secured by Razorpay · Your data is safe
📄 Download Free Sample Copy
🔒 Secured by Razorpay · Your data is safe
🍓
Raspberry Pi Pico Mastery
21 Projects
⚡ Launch Price — 80% OFF
Learn Pico
Build 21 Projects!
MicroPython · Wokwi
IoT · Certificate
Perfect for beginners!
🖥️
Wokwi
No hardware
🐍
MicroPy
From zero
🔨
21 Projects
IoT + sensors
📄
Certificate
Verified cert
📋 Your Details
Enter your name
Valid 10-digit no.
Enter a valid email
Special Launch Offer
₹999 200 80% OFF
🌍 International: $5 USD
One-time · Lifetime access · No subscription
🔒 Secured by Razorpay · UPI · Cards · NetBanking
🎉

You're in!

Payment successful! Your Buddy Bot eBook is ready. Time to build!

📖 Access Your eBook Now
🎉

Enrolled!

Payment successful! Lifetime access to all 21 Pico Projects is yours!

🍓 Go to My Course