Fast PID line follower with 8 sensors
Build it step by step
0 of 7 doneBuild the chassis
Sensor bar low and in front.
Set the mini-360 to 5 V
Before connecting the Nano.
Wire the driver and motors
Test each wheel alone.
Read the sensors on serial
Check black and white values.
Calibrate and test slowly
Low base speed first.
Tune Kp then Kd
One change at a time.
Raise the speed
In small steps.
How do you build a fast PID line follower with 8 sensors?
An 8-sensor IR array gives a line position and a PID loop steers smoothly, fast enough for college robotics competitions. You need Arduino Nano, TCRT5000 IR reflective sensor module × 8, TB6612FNG Motor Driver, N20 micro metal gear motor × 2, Robot car chassis kit 2WD (acrylic), Li-Po 2S 7.4 V 2200 mAh 25C (RC) and Mini-360 buck converter. It takes 7 steps and about 300 minutes.
- Level
- Advanced
- Time
- About 300 min
- Parts
- 7
Wiring
| From | To | Why |
|---|---|---|
| Sensor 1 to 8 analog outputs | Nano A0 to A7 | Line position (left to right) |
| Sensor VCC and GND | Nano 5V and GND | Power |
| TB6612 AIN1, AIN2, PWMA | Nano D4, D5, D3 | Left motor |
| TB6612 BIN1, BIN2, PWMB | Nano D7, D8, D6 | Right motor |
| TB6612 STBY | Nano D9 | High to enable |
| TB6612 VM and GND | LiPo plus and minus | Motor power (7.4 V) |
| TB6612 VCC | Nano 5V | Logic power |
| Mini-360 IN | LiPo plus and minus | Set OUT to 5.0 V before connecting the Nano |
| Mini-360 OUT | Nano 5V pin and GND | Logic and sensor power |
Code
Eight sensors give a position along the array: each sensor adds its reading times its place number, and the average is where the line is, from 0 at the far left to 7000 at the far right, with 3500 meaning dead centre. The robot corrects with a PID loop on that error: the proportional part steers harder the further off the line it is, and the derivative part damps the wobble. Tune Kp alone first with Kd at zero until it follows the line but weaves, then add Kd until it runs smooth, and only then raise the base speed. Calibrate on every start by sweeping the sensor bar over the line and the white for five seconds, since lighting changes the readings. Power: the 7.4 V pack goes to the motor driver and, through a mini-360 set to exactly 5 V with a meter, to the Nano and sensors. LiPo safety: charge on a balance charger on a non-flammable surface, never unattended, never run the pack flat (stop at about 3.3 V per cell), and do not use a puffy or damaged pack. Run only on a track in a clear area. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Fast PID line follower with 8 analog sensors on A0 to A7. For the first 5 seconds sweep the robot over the line to calibrate.
// Board: Arduino Nano. Position runs 0 to 7000 with the line centred at 3500.
const uint8_t S[8] = { A0, A1, A2, A3, A4, A5, A6, A7 };
const uint8_t AIN1 = 4, AIN2 = 5, PWMA = 3, BIN1 = 7, BIN2 = 8, PWMB = 6, STBY = 9;
const bool BLACK_HIGH = true; // true if a dark line gives a higher reading; change if your sensors are the opposite
float KP = 0.06, KD = 0.8; // tune Kp first, then Kd
const int BASE = 130, MAXSPD = 200; // 200 of 255 keeps the motors near their 6 V rating on a 7.4 V pack
int mn[8], mx[8];
long lastPos = 3500;
int lastErr = 0;
void drive(int l, int r) {
l = constrain(l, 0, MAXSPD); r = constrain(r, 0, MAXSPD);
digitalWrite(AIN1, HIGH); digitalWrite(AIN2, LOW); analogWrite(PWMA, l);
digitalWrite(BIN1, HIGH); digitalWrite(BIN2, LOW); analogWrite(PWMB, r);
}
void calibrate() {
for (int i = 0; i < 8; i++) { mn[i] = 1023; mx[i] = 0; }
pinMode(13, OUTPUT); digitalWrite(13, HIGH);
unsigned long t = millis();
while (millis() - t < 5000) {
for (int i = 0; i < 8; i++) {
int v = analogRead(S[i]);
if (v < mn[i]) mn[i] = v;
if (v > mx[i]) mx[i] = v;
}
}
digitalWrite(13, LOW);
}
long readLine() {
long num = 0, den = 0;
for (int i = 0; i < 8; i++) {
int v = constrain(map(analogRead(S[i]), mn[i], mx[i], 0, 1000), 0, 1000);
if (!BLACK_HIGH) v = 1000 - v;
num += (long)v * i * 1000L;
den += v;
}
if (den < 200) return lastPos < 3500 ? 0 : 7000; // line lost: turn towards the side it was last seen
lastPos = num / den;
return lastPos;
}
void setup() {
pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT); pinMode(BIN2, OUTPUT); pinMode(PWMB, OUTPUT);
pinMode(STBY, OUTPUT); digitalWrite(STBY, HIGH);
calibrate();
delay(1000);
}
void loop() {
long pos = readLine();
int err = (int)(pos - 3500);
int corr = (int)(KP * err + KD * (err - lastErr) * 0.1);
lastErr = err;
drive(BASE + corr, BASE - corr); // positive error means the line is to the right, so the left wheel speeds up
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Sensor bar over the line | Position near 3500 |
| Line moves left | Position falls and the robot turns left |
| Gentle curve | Follows without weaving much |
| Line lost | Turns to where it last saw it |
If it does not work
Robot turns away from the line
Swap the sign of the correction or set BLACK_HIGH.
Wobbles
Lower Kp or raise Kd.
Stops at junctions
Check the lost-line rule and sensor spacing.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the fast PID line follower with 8 sensors?
Arduino Nano, TCRT5000 IR reflective sensor module × 8, TB6612FNG Motor Driver, N20 micro metal gear motor × 2, Robot car chassis kit 2WD (acrylic), Li-Po 2S 7.4 V 2200 mAh 25C (RC) and Mini-360 buck converter. TCRT5000 IR reflective sensor module × 8: Or a ready 8-channel QTR-style array.
How long does the fast PID line follower with 8 sensors take to build?
About 300 minutes for a advanced maker, in 7 steps.
How do I know the fast PID line follower with 8 sensors works?
Sensor bar over the line: Position near 3500. Line moves left: Position falls and the robot turns left. Gentle curve: Follows without weaving much.
Robot turns away from the line: what should I check?
Swap the sign of the correction or set BLACK_HIGH.
Wobbles: what should I check?
Lower Kp or raise Kd.
Stops at junctions: what should I check?
Check the lost-line rule and sensor spacing.
