Build a line-following robot with Arduino

A two-wheeled robot that follows a black line on the floor, using two infrared sensors, a motor driver and about thirty lines of Arduino code.

Age 10 and up. Three sessions of about an hour. Builder level.

What you need

Step by step

  1. Build the base

    Fix the two motors at the back of the chassis, one on each side, and the caster wheel at the front. The robot should sit level and roll straight when you push it. Leave room at the front for the two sensors.

  2. Mount the sensors

    Fix the two IR sensors at the front, facing down, about 1 cm above the floor and about 2 cm apart, so the black line fits between them. Closer to the floor reads more reliably than further away.

  3. Wire the motors to the driver

    Connect the left motor to OUT1 and OUT2 on the L298N, and the right motor to OUT3 and OUT4. Leave the jumpers on ENA and ENB, so both motors are always enabled.

  4. Wire the driver and sensors to the Arduino

    Connect IN1, IN2, IN3 and IN4 on the driver to Arduino pins 5, 6, 9 and 10. Connect each sensor's VCC to 5V, GND to GND, and OUT to pin 2 (left) and pin 3 (right). Connect the battery pack to the driver's 12V and GND terminals, and join the driver's GND to the Arduino's GND: without a shared ground nothing works.

  5. Upload the program

    The idea is simple: if both sensors see the white floor, drive forward; if the left sensor sees the line, turn left; if the right one does, turn right. Most sensor modules read LOW over white and HIGH over black; if yours is the other way round, swap LOW and HIGH in the program.

    const int L1 = 5, L2 = 6, R1 = 9, R2 = 10;
    const int LEFT = 2, RIGHT = 3;
    
    void drive(int l1, int l2, int r1, int r2) {
      digitalWrite(L1, l1); digitalWrite(L2, l2);
      digitalWrite(R1, r1); digitalWrite(R2, r2);
    }
    
    void setup() {
      const int motors[] = {L1, L2, R1, R2};
      for (int p : motors) pinMode(p, OUTPUT);
      pinMode(LEFT, INPUT);
      pinMode(RIGHT, INPUT);
    }
    
    void loop() {
      bool leftOnLine = digitalRead(LEFT) == HIGH;
      bool rightOnLine = digitalRead(RIGHT) == HIGH;
    
      if (!leftOnLine && !rightOnLine) drive(HIGH, LOW, HIGH, LOW);  // forward
      else if (leftOnLine)             drive(LOW, LOW, HIGH, LOW);   // turn left
      else if (rightOnLine)            drive(HIGH, LOW, LOW, LOW);   // turn right
    }
  6. Tune the sensors

    Each sensor has a small screw (a potentiometer) and an LED. Hold the robot over the white floor and turn the screw until the LED just changes, then check it changes back over the tape. Do this for both sensors.

  7. Make a track and test it

    Lay a loop of black tape on a light floor, with gentle curves to start. Put the robot on it with the line between the sensors and switch on. When it follows the gentle track, add a tighter curve and see where it fails.

Safety

When it doesn't work

What it teaches

Go further:

Robotics courses at Robocon.in