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.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.
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.
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.
TB6612FNG driverAO1→ → Left motor1
TB6612FNG driverAO2→ → Left motor2
TB6612FNG driverBO1→ → Right motor1
TB6612FNG driverBO2→ → Right motor2
TB6612FNG driverPWMA→ → Arduino NanoD5
TB6612FNG driverAIN1→ → Arduino NanoD7
TB6612FNG driverAIN2→ → Arduino NanoD8
TB6612FNG driverPWMB→ → Arduino NanoD6
TB6612FNG driverBIN1→ → Arduino NanoD4
TB6612FNG driverBIN2→ → Arduino NanoD2
TB6612FNG driverSTBY→ → Arduino NanoD9
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.
TB6612FNG driverVM→ → Holder, 2 × 18650+
Holder, 2 × 18650+→ → Arduino NanoVIN
TB6612FNG driverGND→ → Holder, 2 × 18650−
Holder, 2 × 18650−→ → Arduino NanoGND
TB6612FNG driverVCC→ → Arduino Nano5V
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.
Sensor 1 (far left)DO→ → Arduino NanoA0
Sensor 2DO→ → Arduino NanoA1
Sensor 3 (middle)DO→ → Arduino NanoA2
Sensor 4DO→ → Arduino NanoA3
Sensor 5 (far right)DO→ → Arduino NanoA4
Sensor 1 (far left)VCC→ → Arduino Nano5V
Sensor 2VCC→ → Arduino Nano5V
Sensor 3 (middle)VCC→ → Arduino Nano5V
Sensor 4VCC→ → Arduino Nano5V
Sensor 5 (far right)VCC→ → Arduino Nano5V
Sensor 1 (far left)GND→ → Arduino NanoGND
Sensor 2GND→ → Arduino NanoGND
Sensor 3 (middle)GND→ → Arduino NanoGND
Sensor 4GND→ → Arduino NanoGND
Sensor 5 (far right)GND→ → Arduino NanoGND
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.
Start button1→ → Arduino NanoD3
Start button2→ → Arduino NanoGND
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.
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)constint SENSORS[5] = {A0, A1, A2, A3, A4}; // left .. rightconstint BLACK = HIGH; // most modules read HIGH over black; use LOW if yours read the other wayvoid 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)constint PWMA = 5; // left motor speed (PWM)constint AIN1 = 7;
constint AIN2 = 8;
constint PWMB = 6; // right motor speed (PWM)constint BIN1 = 4;
constint BIN2 = 2;
constint STBY = 9; // TB6612 only; leave it unconnected on an L298Nconstint BUTTON = 3; // start/stop, to GNDconstint LED = 13; // on while runningconstint SENSORS[5] = {A0, A1, A2, A3, A4}; // left .. rightconstlong WEIGHTS[5] = {-2000, -1000, 0, 1000, 2000}; // where each sensor sitsconstint 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 middlefloat Ki = 0.0; // fixes a slow drift; leave it at 0 at firstfloat Kd = 0.4; // calms the wobbleint BASE_SPEED = 120; // speed on a straight, 0..255int MAX_PWM = 200; // 6 V motors on a 7.4 V pack: keep the average near 6 Vconstunsignedlong TICK_MS = 5; // run the PID 200 times a secondlong lastError = 0;
long integral = 0;
bool running = false;
unsignedlong 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 itvoid checkButton() {
if (digitalRead(BUTTON) == HIGH) return;
drive(0, 0);
delay(30); // let the contact settlewhile (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.
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.
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.