Wiring diagramTB6612FNG driver · AO1 → Left motor · 1; TB6612FNG driver · AO2 → Left motor · 2; TB6612FNG driver · BO1 → Right motor · 1; TB6612FNG driver · BO2 → Right motor · 2; TB6612FNG driver · PWMA → Arduino Nano · D5; TB6612FNG driver · AIN1 → Arduino Nano · D7; TB6612FNG driver · AIN2 → Arduino Nano · D8; TB6612FNG driver · PWMB → Arduino Nano · D6; TB6612FNG driver · BIN1 → Arduino Nano · D4; TB6612FNG driver · BIN2 → Arduino Nano · D2; TB6612FNG driver · STBY → Arduino Nano · D9; TB6612FNG driver · VM → Holder, 2 × 18650 · +; Holder, 2 × 18650 · + → Arduino Nano · VIN; TB6612FNG driver · GND → Holder, 2 × 18650 · −; Holder, 2 × 18650 · − → Arduino Nano · GND; TB6612FNG driver · VCC → Arduino Nano · 5V; Sensor 1 (far left) · DO → Arduino Nano · A0; Sensor 2 · DO → Arduino Nano · A1; Sensor 3 (middle) · DO → Arduino Nano · A2; Sensor 4 · DO → Arduino Nano · A3; Sensor 5 (far right) · DO → Arduino Nano · A4; Sensor 1 (far left) · VCC → Arduino Nano · 5V; Sensor 2 · VCC → Arduino Nano · 5V; Sensor 3 (middle) · VCC → Arduino Nano · 5V; Sensor 4 · VCC → Arduino Nano · 5V; Sensor 5 (far right) · VCC → Arduino Nano · 5V; Sensor 1 (far left) · GND → Arduino Nano · GND; Sensor 2 · GND → Arduino Nano · GND; Sensor 3 (middle) · GND → Arduino Nano · GND; Sensor 4 · GND → Arduino Nano · GND; Sensor 5 (far right) · GND → Arduino Nano · GND; Start button · 1 → Arduino Nano · D3; Start button · 2 → Arduino Nano · GNDArduino NanoD5D7D8D6D4D2D9VINA05VA1GNDA2A3A4D3TB6612FNG driverPWMAAIN1AIN2PWMBBIN1BIN2STBYVCCVMGNDAO1AO2BO1BO2Left motor12Right motor12Holder, 2 × 18650+−Sensor 1 (far left)VCCGNDDOSensor 2VCCGNDDOSensor 3 (middle)VCCGNDDOSensor 4VCCGNDDOSensor 5 (far right)VCCGNDDOStart button12

Drawn with a TB6612. An L298N takes the same pins: ENA, IN1, IN2, ENB, IN3, IN4, and its 12V terminal instead of VM. 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.

NewRobotics

We haven't added a photo or video of the finished project yet. Meanwhile, here is the wiring: every wire on the pin the instructions use.

Drawn with a TB6612. An L298N takes the same pins: ENA, IN1, IN2, ENB, IN3, IN4, and its 12V terminal instead of VM.

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.

Difficulty
Advanced
Time
4 h
Age
12+
Parts
8, 8 on the site

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

Variations

Choose a variation and the parts list updates.

Instructions

  1. How PID works, simply

    Five sensors see where the line is: in the middle, a little to the left or far to the right. That offset is called the “error”. PID works out how much to steer from three parts: P (proportional): the further the line is from the middle, the harder it steers. I (integral): if the robot keeps drifting slightly to one side, it slowly corrects that. D (derivative): it watches how fast the error changes and stops the wobble. The result is a robot that follows the line smoothly instead of zigzagging like a simple on-off car. If this is your first robot, build “A Line-Following Car” first.

  2. The track

    The easiest start is a ready-made practice track: unroll it on the floor and go. For a track of your own, stick 19 mm black electrical tape onto a light floor or a large sheet of white paper. Keep the bends gentle (a radius of at least about 15 cm) and smooth out creases and air bubbles: the sensor reads them as a line.

  3. The chassis and motors

    Build the 2WD chassis following its own instructions: two TT motors with wheels and a swivel wheel at the back. If the motors have no wires, solder them on. Fix the board and the driver to the top plate and the battery holder as low as you can. Solder the header pins onto the Nano and the TB6612 first: both come unsoldered.

  4. The motor driver

    Connect the left motor to the TB6612's AO1 and AO2 and the right one to BO1 and BO2. Control: PWMA → D5, AIN1 → D7, AIN2 → D8, PWMB → D6, BIN1 → D4, BIN2 → D2, STBY → D9. An L298N uses the same pins: ENA → D5, IN1 → D7, IN2 → D8, ENB → D6, IN3 → D4, IN4 → D2. Take the jumpers off ENA and ENB. The L298N has no STBY, so D9 stays free.

    Drawn with a TB6612. An L298N takes the same pins: ENA, IN1, IN2, ENB, IN3, IN4, and its 12V terminal instead of VM. 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.

    • TB6612FNG driver AO1 → Left motor 1
    • TB6612FNG driver AO2 → Left motor 2
    • TB6612FNG driver BO1 → Right motor 1
    • TB6612FNG driver BO2 → Right motor 2
    • TB6612FNG driver PWMA → Arduino Nano D5
    • TB6612FNG driver AIN1 → Arduino Nano D7
    • TB6612FNG driver AIN2 → Arduino Nano D8
    • TB6612FNG driver PWMB → Arduino Nano D6
    • TB6612FNG driver BIN1 → Arduino Nano D4
    • TB6612FNG driver BIN2 → Arduino Nano D2
    • TB6612FNG driver STBY → Arduino Nano D9
  5. Power

    Charge the two 18650 cells fully and put them in the holder (7.4 V), with its switch off. Connect “+” to the TB6612's VM (the 12V terminal on an L298N) and to the board's VIN, and “−” to the driver's GND and the board's GND. The TB6612's VCC goes to the board's 5V. Everything must share the same ground. Check the polarity twice before switching on.

    Drawn with a TB6612. An L298N takes the same pins: ENA, IN1, IN2, ENB, IN3, IN4, and its 12V terminal instead of VM. 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.

    • TB6612FNG driver VM → Holder, 2 × 18650 +
    • Holder, 2 × 18650 + → Arduino Nano VIN
    • TB6612FNG driver GND → Holder, 2 × 18650 −
    • Holder, 2 × 18650 − → Arduino Nano GND
    • TB6612FNG driver VCC → Arduino Nano 5V
  6. The sensor bar

    Fix the five TCRT5000 modules in a row under the front, about 15 mm apart centre to centre and 3–5 mm above the floor. The middle sensor must sit exactly on the robot's centre line. Connect the DO outputs, left to right, to A0, A1, A2, A3 and A4, every VCC to 5V and GND to ground. The further ahead of the wheels the sensors are, the earlier the robot sees a bend.

    Drawn with a TB6612. An L298N takes the same pins: ENA, IN1, IN2, ENB, IN3, IN4, and its 12V terminal instead of VM. 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.

    • Sensor 1 (far left) DO → Arduino Nano A0
    • Sensor 2 DO → Arduino Nano A1
    • Sensor 3 (middle) DO → Arduino Nano A2
    • Sensor 4 DO → Arduino Nano A3
    • Sensor 5 (far right) DO → Arduino Nano A4
    • Sensor 1 (far left) VCC → Arduino Nano 5V
    • Sensor 2 VCC → Arduino Nano 5V
    • Sensor 3 (middle) VCC → Arduino Nano 5V
    • Sensor 4 VCC → Arduino Nano 5V
    • Sensor 5 (far right) VCC → Arduino Nano 5V
    • Sensor 1 (far left) GND → Arduino Nano GND
    • Sensor 2 GND → Arduino Nano GND
    • Sensor 3 (middle) GND → Arduino Nano GND
    • Sensor 4 GND → Arduino Nano GND
    • Sensor 5 (far right) GND → Arduino Nano GND
  7. The start button

    Connect one leg of the button to D3 and the other to ground. One press starts the robot, the next one stops it.

    Drawn with a TB6612. An L298N takes the same pins: ENA, IN1, IN2, ENB, IN3, IN4, and its 12V terminal instead of VM. 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.

    • Start button 1 → Arduino Nano D3
    • Start button 2 → Arduino Nano GND
  8. The pin table

    Every connection from the steps above, in one table. Key: S1–S5 are the line sensors from left to right (S1 is the far-left one, S3 the middle), BTN the start button, BAT+ and BAT- the holder's terminals after its switch, and M-L and M-R the left and right motors. The L298N's pins are in brackets; "-" means that pin isn't used on an L298N. Once it is built, check each wire against it.

    Text
    Arduino Nano/Uno
    D5                 ->  TB6612 PWMA   (L298N ENA)
    D7                 ->  TB6612 AIN1   (L298N IN1)
    D8                 ->  TB6612 AIN2   (L298N IN2)
    D6                 ->  TB6612 PWMB   (L298N ENB)
    D4                 ->  TB6612 BIN1   (L298N IN3)
    D2                 ->  TB6612 BIN2   (L298N IN4)
    D9                 ->  TB6612 STBY   (L298N -)
    A0                 ->  S1 DO
    A1                 ->  S2 DO
    A2                 ->  S3 DO
    A3                 ->  S4 DO
    A4                 ->  S5 DO
    D3                 ->  BTN, BTN -> GND
    5V                 ->  TB6612 VCC, S1-S5 VCC
    VIN                ->  BAT+
    GND                ->  BAT-, TB6612 GND, S1-S5 GND
    
    TB6612 AO1, AO2    ->  M-L   (L298N OUT1, OUT2)
    TB6612 BO1, BO2    ->  M-R   (L298N OUT3, OUT4)
    TB6612 VM          ->  BAT+  (L298N 12V)
  9. Check the sensors

    Upload this small sketch and open the Serial Monitor (9600). Slide the robot slowly across the line: a sensor over black should show 1 and one over white 0. If it's the other way round, change BLACK = HIGH to BLACK = LOW in this sketch and in the PID code. A sensor that always shows the same number needs its screw adjusted. Don't move on to PID until all five work.

    Arduino C++
    // Sensor check: prints the five line sensors as 0 and 1 (1 = black)
    const int SENSORS[5] = {A0, A1, A2, A3, A4};   // left .. right
    const int BLACK = HIGH;   // most modules read HIGH over black; use LOW if yours read the other way
    
    void setup() {
      Serial.begin(9600);
      for (int i = 0; i < 5; i++) pinMode(SENSORS[i], INPUT);
    }
    
    void loop() {
      for (int i = 0; i < 5; i++) {
        Serial.print(digitalRead(SENSORS[i]) == BLACK ? 1 : 0);
        Serial.print(' ');
      }
      Serial.println();
      delay(200);
    }
  10. The PID code

    Keep the holder switched off while uploading. For a Nano choose “Arduino Nano” as the board and “ATmega328P” as the processor (“Old Bootloader” if the upload fails). After switching on, the robot waits. Put it on the line and press the button: it sets off a second later, and a second press stops it.

    Arduino C++
    // PID line follower: Arduino Nano (or Uno) + TB6612FNG (or L298N) + five TCRT5000 sensors.
    // The sensors sit in a row across the front, left to right on A0..A4.
    // A black line on a light floor; a button on D3 starts and stops the robot.
    
    // Motor driver (on an L298N: PWMA = ENA, AIN1/AIN2 = IN1/IN2, PWMB = ENB, BIN1/BIN2 = IN3/IN4)
    const int PWMA = 5;   // left motor speed (PWM)
    const int AIN1 = 7;
    const int AIN2 = 8;
    const int PWMB = 6;   // right motor speed (PWM)
    const int BIN1 = 4;
    const int BIN2 = 2;
    const int STBY = 9;   // TB6612 only; leave it unconnected on an L298N
    
    const int BUTTON = 3; // start/stop, to GND
    const int LED = 13;   // on while running
    
    const int SENSORS[5] = {A0, A1, A2, A3, A4};            // left .. right
    const long WEIGHTS[5] = {-2000, -1000, 0, 1000, 2000};  // where each sensor sits
    const int BLACK = HIGH;   // most modules read HIGH over black; use LOW if yours read the other way
    
    // PID: start with these values, then tune them (see the steps)
    float Kp = 0.08;      // steer harder the further the line is from the middle
    float Ki = 0.0;       // fixes a slow drift; leave it at 0 at first
    float Kd = 0.4;       // calms the wobble
    int BASE_SPEED = 120; // speed on a straight, 0..255
    int MAX_PWM = 200;    // 6 V motors on a 7.4 V pack: keep the average near 6 V
    
    const unsigned long TICK_MS = 5;   // run the PID 200 times a second
    
    long lastError = 0;
    long integral = 0;
    bool running = false;
    unsigned long lastTick = 0;
    
    // Both motors at once, -255..255 each (negative = backwards)
    void drive(int left, int right) {
      left = constrain(left, -MAX_PWM, MAX_PWM);
      right = constrain(right, -MAX_PWM, MAX_PWM);
      digitalWrite(AIN1, left > 0 ? HIGH : LOW);
      digitalWrite(AIN2, left < 0 ? HIGH : LOW);
      digitalWrite(BIN1, right > 0 ? HIGH : LOW);
      digitalWrite(BIN2, right < 0 ? HIGH : LOW);
      analogWrite(PWMA, abs(left));
      analogWrite(PWMB, abs(right));
    }
    
    // Where the line is: -2000 (under the far-left sensor) .. 0 (middle) .. 2000 (far right).
    // When no sensor sees it, keep turning towards the side it was last seen on.
    long readPosition() {
      long sum = 0;
      int count = 0;
      for (int i = 0; i < 5; i++) {
        if (digitalRead(SENSORS[i]) == BLACK) {
          sum += WEIGHTS[i];
          count++;
        }
      }
      if (count == 0) return lastError > 0 ? 2500 : -2500;
      return sum / count;
    }
    
    // One press starts the robot, the next one stops it
    void checkButton() {
      if (digitalRead(BUTTON) == HIGH) return;
      drive(0, 0);
      delay(30);                               // let the contact settle
      while (digitalRead(BUTTON) == LOW) {}    // wait until it is released
      running = !running;
      lastError = 0;
      integral = 0;
      digitalWrite(LED, running ? HIGH : LOW);
      digitalWrite(STBY, running ? HIGH : LOW);
      if (running) delay(1000);                // a second to take your hand away
    }
    
    void setup() {
      pinMode(STBY, OUTPUT);
      digitalWrite(STBY, LOW);   // motors off until the button
      pinMode(PWMA, OUTPUT);
      pinMode(AIN1, OUTPUT);
      pinMode(AIN2, OUTPUT);
      pinMode(PWMB, OUTPUT);
      pinMode(BIN1, OUTPUT);
      pinMode(BIN2, OUTPUT);
      for (int i = 0; i < 5; i++) pinMode(SENSORS[i], INPUT);
      pinMode(BUTTON, INPUT_PULLUP);
      pinMode(LED, OUTPUT);
    }
    
    void loop() {
      checkButton();
      if (!running) return;
      if (millis() - lastTick < TICK_MS) return;
      lastTick = millis();
    
      long error = readPosition();   // the target is 0: the line under the middle sensor
      integral = constrain(integral + error, -20000L, 20000L);
      long derivative = error - lastError;
      lastError = error;
    
      int turn = (int)(Kp * error + Ki * integral + Kd * derivative);
      drive(BASE_SPEED + turn, BASE_SPEED - turn);   // line to the right: left wheel faster
    }
  11. Tune the PID

    1. Set Ki and Kd to zero and BASE_SPEED to 90. 2. Raise Kp a little at a time (0.04, 0.06, 0.08…) until the robot gets round the bends, even if it wobbles. 3. Now raise Kd (0.2, 0.4, 0.8…) until the wobble dies down. 4. Raise BASE_SPEED step by step and tune Kp and Kd again each time. 5. Add Ki (starting at 0.0005) only if the robot keeps drifting to one side on the straights. After each change run the whole track and write down which values work. Tune with fully charged cells: as they run down, the robot slows and turns differently.

  12. If something doesn't work

    It runs off the line in the bends: lower BASE_SPEED or raise Kp. It zigzags on the straights: lower Kp or raise Kd. It spins on the spot and can't find the line: one motor turns backwards (swap its two wires), or the sensors are wired in reverse order: A0 must be the far-left one. It steers away from the line instead of towards it: swap + and − in the last line of the code (the drive call), or check the order of the sensors. It always turns better one way: two motors are never exactly the same, that's normal. Add a little Ki.

  13. Safety and competitions

    Charge the 18650 cells only with a charger made for them, and never use a damaged or swollen cell. Stop the robot with its button or switch it off before you pick it up. Read the rules before a competition: robot size, line width, crossings, gaps and the start procedure differ from one event to the next. Test the robot under different lighting: sunlight confuses infrared sensors.

  • NewRobotics
    Intermediate4 h12+

    A Mini-Sumo Robot with Arduino Nano

    A small autonomous robot for the mini-sumo class (10 × 10 cm, up to 500 g): it waits 5 seconds after the start, finds the opponent with an ultrasonic sensor and pushes it out of the ring, while sensors underneath watch the white edge so it never drives out itself.

    9/12 parts on the site
  • 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 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.

    7/7 parts on the site