AI Valley Logo
THE AI VALLEYK12 Coding & Robotics
Back to Blog
Build a Line-Following Robot: Arduino Tutorial for Kids | AI Valley Panchkula Tricity
Bhavesh Bansal
March 10, 2025
10 min read

Build a Line-Following Robot: Arduino Tutorial for Kids

Ever wanted to build a robot that can drive itself? In this exciting hands-on tutorial from AI Valley, the top robotics institute in Zirakpur, you will build a real line-following robot that uses infrared sensors to follow a black line on the floor — just like the robots used in factories and warehouses!

🎯 What You'll Build

A small robot car that uses two IR sensors to detect a black line on a white surface and automatically steers itself to follow it. When the line curves left, the robot turns left. When it curves right, the robot turns right. It's like giving your robot its own pair of eyes!

📋 Prerequisites & Materials

  • Arduino Uno board
  • L298N Motor Driver module
  • 2x DC Motors with wheels
  • 2x IR Sensor Modules (like TCRT5000)
  • Chassis/frame (any small robot car chassis kit)
  • 9V Battery with connector
  • Jumper Wires (male-to-female)
  • Arduino IDE software installed on your computer
  • Black electrical tape on a white surface (your track!)
  • Step 1: Assemble the Chassis and Mount the Motors

    Start by attaching the two DC motors to your robot chassis. Most kits come with mounting holes. Secure the motors with screws, attach the wheels to the motor shafts, and add a caster wheel (or ball wheel) at the front for balance.
    A top-down photo of a robot car chassis with two DC motors mounted

    A top-down photo of a robot car chassis with two DC motors mounted

    Mount the Arduino Uno board on top of the chassis using screws or double-sided tape. Place the L298N motor driver next to it.

    Tip: Label your motors as "Left Motor" and "Right Motor" — this will help when writing the code!

    Step 2: Wire the Motor Driver to Arduino

    The L298N motor driver acts as a bridge between your Arduino and the motors. Arduino sends small signals, and the motor driver amplifies them to spin the motors.
    A wiring diagram showing connections between Arduino Uno and L298N motor driver

    A wiring diagram showing connections between Arduino Uno and L298N motor driver

    cpp
    // Motor Driver Pin Connections:
    // Left Motor:  IN1 -> Pin 5, IN2 -> Pin 6
    // Right Motor: IN3 -> Pin 9, IN4 -> Pin 10
    // ENA -> Pin 3 (PWM speed), ENB -> Pin 11 (PWM speed)
    // Motor Driver GND -> Arduino GND
    // Motor Driver 12V -> 9V Battery +
    

    Important: Double-check every wire before powering on. A wrong connection could damage your Arduino!

    Step 3: Connect the IR Sensors

    IR sensors work by emitting infrared light and measuring how much bounces back. A white surface reflects lots of light (HIGH), while a black line absorbs the light (LOW). Mount the sensors at the front of your chassis, pointing down, about 1-2 cm above the ground.
    IR sensor modules mounted under the robot front, pointing at the floor

    IR sensor modules mounted under the robot front, pointing at the floor

    cpp
    // IR Sensor Connections:
    // Left IR Sensor OUT  -> Arduino Pin 2
    // Right IR Sensor OUT -> Arduino Pin 4
    // Both sensors VCC    -> Arduino 5V
    // Both sensors GND    -> Arduino GND
    

    Step 4: Write the Brain — The Arduino Code

    Open the Arduino IDE, create a new sketch, and paste this complete code:
    The Arduino IDE showing the complete line-following robot code

    The Arduino IDE showing the complete line-following robot code

    cpp
    // Line Following Robot - AI Valley Zirakpur
    const int IN1 = 5, IN2 = 6;   // Left motor
    const int IN3 = 9, IN4 = 10;  // Right motor
    const int ENA = 3, ENB = 11;  // Speed control
    const int LEFT_SENSOR = 2, RIGHT_SENSOR = 4;
    const int SPEED = 150;
    
    void setup() {
      pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
      pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
      pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);
      pinMode(LEFT_SENSOR, INPUT);
      pinMode(RIGHT_SENSOR, INPUT);
      analogWrite(ENA, SPEED);
      analogWrite(ENB, SPEED);
      Serial.begin(9600);
      Serial.println("AI Valley Line Follower Ready!");
    }
    
    void moveForward() {
      digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
    }
    
    void turnLeft() {
      digitalWrite(IN1, LOW);  digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
    }
    
    void turnRight() {
      digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);  digitalWrite(IN4, LOW);
    }
    
    void stopMotors() {
      digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
    }
    
    void loop() {
      int L = digitalRead(LEFT_SENSOR);
      int R = digitalRead(RIGHT_SENSOR);
    
      if (L == HIGH && R == HIGH) { moveForward(); }
      else if (L == LOW && R == HIGH) { turnLeft(); }
      else if (L == HIGH && R == LOW) { turnRight(); }
      else { stopMotors(); }
    
      delay(100);
    }
    

    What this code does: The loop() reads both sensors continuously. Based on which sensor detects the black line, it decides to go forward, turn left, turn right, or stop.

    Step 5: Create the Track and Test!

    Use black electrical tape on a white floor to create an oval track. Start simple, then add curves.
    A robot car following a black tape track on a white floor

    A robot car following a black tape track on a white floor

    • Connect the 9V battery to the motor driver
    • Upload the code to Arduino via USB cable
    • Place the robot so sensors straddle the black line
    • Open Serial Monitor (Ctrl+Shift+M) to debug
    • Watch your robot drive itself!

    Troubleshooting:

  • Robot goes backward? Swap IN1/IN2 wire connections
  • Sensors not detecting? Adjust height to 1-2cm from ground
  • Too fast on turns? Lower SPEED from 150 to 100
  • 🎉 Final Result

    Congratulations! You've built a real autonomous robot that navigates a path using infrared sensors. You've learned motor drivers, IR sensors, digital I/O, and control logic — the same fundamentals used in industrial automation!

    🚀 Challenge: Take It Further

  • Add a third sensor in the center for smoother proportional control
  • Add an ultrasonic sensor (HC-SR04) to stop when detecting obstacles
  • Variable speed turns using analogWrite() for smoother steering
  • 🏫 Learn More at AI Valley, Panchkula & Tricity

    This is the kind of project our students build every week at AI Valley in Zirakpur! Our robotics for kids program covers Arduino, sensors, motor control, 3D printing, and AI. We serve students from Chandigarh, Mohali, and Panchkula. Come visit our lab and start building!

    Tags

    robotics classes for kids PanchkulaArduino tutorial Tricityline following robot projectbest robotics institute Chandigarhcoding classes ZirakpurSTEM education Mohalirobot building for beginnersArduino classes Panchkulakids robotics TricityAI Valley roboticssensor robot project for kids