Illustration: a robot car following a black tape line on paper.Robotics
Illustration (made with AI): roughly what the finished project looks like.

A Line-Following Car

Two infrared sensors see a black line on the floor and the car follows it on its own, round the bends too. On a wide black strip it stops, as if at a finish line.

Difficulty
Intermediate
Time
1 h 30 min
Age
12+
Parts
7, 7 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. The track

    Lay a closed loop of 2 cm black electrical tape on a light floor or a big sheet of white paper. Keep the bends gentle. For a finish, stick a wide strip of tape across the line.

  2. Build the car

    Wire the chassis, motors, L298N and power the same way as in “A Car That Avoids Obstacles”: ENA 5, IN1 7, IN2 8, ENB 6, IN3 4, IN4 2.

    D2 is the board's digital pin 2, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • L298N driver OUT1 → Left motor 1
    • L298N driver OUT2 → Left motor 2
    • L298N driver OUT3 → Right motor 1
    • L298N driver OUT4 → Right motor 2
    • L298N driver ENA → Arduino D5
    • L298N driver IN1 → Arduino D7
    • L298N driver IN2 → Arduino D8
    • L298N driver ENB → Arduino D6
    • L298N driver IN3 → Arduino D4
    • L298N driver IN4 → Arduino D2
    • L298N driver 12V → Holder, 2 × 18650 +
    • Holder, 2 × 18650 + → Arduino VIN
    • L298N driver GND → Holder, 2 × 18650 −
    • Holder, 2 × 18650 − → Arduino GND
  3. Sensors

    Fix two sensors under the front of the car, 1 cm above the floor and a little further apart than the line is wide. Connect the left sensor's DO pin (OUT on some modules) to A0 and the right one's to A1, VCC to 5V and GND to ground.

    D2 is the board's digital pin 2, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Left sensor VCC → Arduino 5V
    • Left sensor GND → Arduino GND
    • Left sensor DO → Arduino A0
    • Right sensor VCC → Arduino 5V
    • Right sensor GND → Arduino GND
    • Right sensor DO → Arduino A1
  4. Tune the sensors

    Put the sensor over white, then over black, and turn its little screw until the module's LED lights over white and goes out over black.

  5. Upload the code

    Place the car so the line runs between the two sensors. If the car runs away from the line, change BLACK = HIGH to BLACK = LOW in the code.

    Arduino C++
    // L298N: take the jumpers off ENA and ENB and wire them to PWM pins
    const int ENA = 5, IN1 = 7, IN2 = 8;   // left motor
    const int ENB = 6, IN3 = 4, IN4 = 2;   // right motor
    const int LEFT_EYE = A0, RIGHT_EYE = A1;   // the sensors' DO outputs
    const int BLACK = HIGH;                // most modules give HIGH over black, some give LOW
    const int SPEED = 140;                 // on the straight
    const int TURN = 170;                  // in the bends
    
    void drive(int left, int right) {      // -255..255 for each side
      digitalWrite(IN1, left > 0 ? HIGH : LOW);
      digitalWrite(IN2, left < 0 ? HIGH : LOW);
      digitalWrite(IN3, right > 0 ? HIGH : LOW);
      digitalWrite(IN4, right < 0 ? HIGH : LOW);
      analogWrite(ENA, abs(left));
      analogWrite(ENB, abs(right));
    }
    
    void setup() {
      int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
      for (int pin : pins) pinMode(pin, OUTPUT);
      pinMode(LEFT_EYE, INPUT);
      pinMode(RIGHT_EYE, INPUT);
      delay(2000);                         // time to put the car on the track
    }
    
    void loop() {
      bool left = digitalRead(LEFT_EYE) == BLACK;
      bool right = digitalRead(RIGHT_EYE) == BLACK;
    
      if (left && right) drive(0, 0);              // a wide black strip: the finish
      else if (left) drive(-TURN / 2, TURN);       // line under the left sensor: turn left
      else if (right) drive(TURN, -TURN / 2);      // under the right one: turn right
      else drive(SPEED, SPEED);                    // line between the sensors: straight on
    }
  • Robotics
    Intermediate2 h12+

    A Car That Avoids Obstacles

    A two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.

    8/8 parts on the site
  • Robotics
    Intermediate1 h 30 min12+

    A Phone-Controlled Car (Bluetooth)

    An HC-05 Bluetooth module links the car to your phone. Drive it forward and back and turn it left and right with the buttons of an Android app, and change its speed too.

    8/8 parts on the site
  • NewRobotics
    Advanced4 h12+

    A PID Line Follower Robot

    Five infrared sensors measure how far the robot has drifted off the line, and a PID controller corrects the motors smoothly and fast. Built for competitions: the track, the sensor bar, the code and how to tune it.

    8/8 parts on the site