# Five-sensor line follower with PD steering on Arduino: design worksheet and code

> A ₹1,640 design worksheet for a line follower that turns five sensor readings into one position number and steers with proportional-derivative control, with a worked table, a calibration sweep and a tuning order.

Canonical: https://roboticsengineering.in/projects/five-sensor-line-follower-pd-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**A ₹1,640 line follower that knows how far off the line it is, not only which side. Five reflectance sensors are combined into one position number, and the sketch steers by that number with proportional-derivative (PD) control. The page shows the position maths with a worked table, a calibration sweep, a tuning order, and what to do when the line is lost.**

**Scope:** intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The position table below was computed by script. The gains in the sketch are starting guesses, not tuned values: the right numbers depend on your motors, wheels, speed and sensor spacing, and finding them is part of the build. For a simpler two-sensor version see the [line follower robot](https://roboticsengineering.in/projects/line-follower-robot-arduino/).

## How it works

Number the sensors from left to right with weights −2, −1, 0, +1 and +2. Convert each reading to a value from 0 (floor) to 1 (line). The position is the weighted average: the sum of weight × value, divided by the sum of values. A line under the centre sensor gives 0. A line between the centre and the next sensor to the right gives +0.5. A line under the outer right sensor gives +2.

The error is that position, in units of sensor spacing. Steering is then correction = Kp × error + Kd × (error − last error). A positive error means the line is to the right, so the left wheel speeds up and the right slows down by the correction. Kp pushes back in proportion to how far off the robot is; Kd pushes against how fast the error is changing, which damps the wobble.

## Bill of materials

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| 5-channel analog IR reflectance sensor array | 1 | ₹250 | ₹250 |
| L298N motor driver board | 1 | ₹180 | ₹180 |
| 2WD chassis kit with two gear motors and wheels | 1 | ₹450 | ₹450 |
| Battery holder with two 18650 cells | 1 | ₹210 | ₹210 |
| Parts total | ₹1,640 |  |  |

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings. Choose a five-channel module that gives an analog output for each sensor; a module with only on/off outputs works but gives a coarser position.

## Worked position values

| Sensors (left to right) | Position | Meaning |
| --- | --- | --- |
| 0 0 1 0 0 | 0 | On the line |
| 0 1 1 0 0 | −0.5 | Line slightly left |
| 0 1 0 0 0 | −1 | Line under the second sensor, left |
| 1 1 0 0 0 | −1.5 | Line far left |
| 1 0 0 0 0 | −2 | Line under the outer left sensor |
| 0 0 0 1 1 | +1.5 | Line far right |
| 0 0 0 0 1 | +2 | Line under the outer right sensor |
| 0 0 0 0 0 | none | Line lost, handled separately |

*Weights −2, −1, 0, +1, +2, on/off readings. With analog readings the values are fractions; for example 0, 0.2, 0.9, 0.4, 0 gives +0.13, a hair to the right of centre.*

The result is a smooth number that changes as the line slides across the array, instead of jumping between a few states. Its resolution depends on your sensor spacing: the error is in units of one sensor spacing, so measure the spacing on your module if you want to turn it into millimetres.

## Power and wiring

| Part | Arduino Uno | Note |
| --- | --- | --- |
| Sensor outputs 1 to 5 | A0 to A4 | Left to right |
| Sensor VCC and GND | 5V and GND |  |
| L298N ENA / IN1 / IN2 | D5 / D7 / D8 | Left motor, ENA is PWM |
| L298N ENB / IN3 / IN4 | D6 / D9 / D4 | Right motor, ENB is PWM |
| Battery | L298N 12V input and Uno VIN | Check your pack voltage against the board’s limits |

*Keep the sensors a few millimetres above the floor, as the module’s sheet advises.*

## The sketch

```
// Five-sensor line follower with PD steering: Uno + L298N + 5-channel IR array
// Design worksheet: Kp, Kd and BASE are starting guesses, not tuned values.

const int N = 5;
const int SENSOR_PINS[N] = {A0, A1, A2, A3, A4};
const float WEIGHT[N] = {-2, -1, 0, 1, 2};
const bool LINE_IS_HIGH = true;     // set false if the line reads LOWER than the floor

const int L_EN = 5, L_IN1 = 7, L_IN2 = 8;
const int R_EN = 6, R_IN3 = 9, R_IN4 = 4;

const int BASE = 130;               // forward PWM
float Kp = 35.0;
float Kd = 60.0;

int lo[N], hi[N];
float lastError = 0;

void calibrate() {                  // sweep the robot over line and floor for 3 s
  for (int i = 0; i < N; i++) { lo[i] = 1023; hi[i] = 0; }
  unsigned long t = millis();
  while (millis() - t < 3000) {
    for (int i = 0; i < N; i++) {
      int v = analogRead(SENSOR_PINS[i]);
      if (v < lo[i]) lo[i] = v;
      if (v > hi[i]) hi[i] = v;
    }
  }
}

void drive(int left, int right) {   // forward only, 0..255
  left = constrain(left, 0, 255);
  right = constrain(right, 0, 255);
  digitalWrite(L_IN1, HIGH); digitalWrite(L_IN2, LOW);
  digitalWrite(R_IN3, HIGH); digitalWrite(R_IN4, LOW);
  analogWrite(L_EN, left);
  analogWrite(R_EN, right);
}

void setup() {
  pinMode(L_EN, OUTPUT); pinMode(L_IN1, OUTPUT); pinMode(L_IN2, OUTPUT);
  pinMode(R_EN, OUTPUT); pinMode(R_IN3, OUTPUT); pinMode(R_IN4, OUTPUT);
  delay(1000);
  calibrate();
  delay(1000);
}

void loop() {
  float sum = 0, wsum = 0;
  for (int i = 0; i < N; i++) {
    int range = max(hi[i] - lo[i], 1);
    float v = (analogRead(SENSOR_PINS[i]) - lo[i]) / (float)range;
    v = constrain(v, 0.0, 1.0);
    if (!LINE_IS_HIGH) v = 1.0 - v;
    sum += v;
    wsum += v * WEIGHT[i];
  }

  float error;
  if (sum < 0.3) {                  // line lost: keep turning the way it was last seen
    error = (lastError >= 0) ? 2.5 : -2.5;
  } else {
    error = wsum / sum;
  }

  float correction = Kp * error + Kd * (error - lastError);
  lastError = error;
  drive(BASE + correction, BASE - correction);
  delay(5);                         // keeps the derivative term steady
}
```

The derivative term is per loop, so the delay at the end keeps the loop time steady. If you change it, retune Kd. The lost-line rule makes the robot keep turning toward where the line was last seen, which usually finds it again at a sharp corner but can also spin forever on a gap, so add a time limit if your track has gaps.

## Tuning order

1. **Calibrate on your track:** sweep the sensors across the line and floor during the three seconds at power-up.
2. **Set Kd to 0 and a slow BASE:** raise Kp until the robot follows gentle curves, then until it starts to weave, and back off a little.
3. **Add Kd:** raise it until the weaving calms. Too much Kd makes it twitchy on noise.
4. **Raise BASE:** in small steps, retuning Kp and Kd as it speeds up.
5. **Test hard cases:** a sharp corner, a crossing, a gap.

## What other builders report

This is general practice described in many tutorials, summarised in our own words, not a result from our own robot: weighted-average position over a sensor array is the usual way to get a smooth error, and tuning proportional first, then derivative, then speed is the usual order. Exact gains are always specific to the robot.

## What usually goes wrong

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Weaves on straight lines | Kp too high or Kd too low | Lower Kp, raise Kd |
| Slow to enter curves | Kp too low | Raise Kp |
| Twitchy, noisy motors | Kd too high or noisy sensors | Lower Kd, shade the sensors |
| Turns the wrong way | Line polarity or sensor order reversed | Flip LINE_IS_HIGH or the weights |
| Loses the line at corners | Speed too high | Lower BASE |
| Behaves differently each day | Calibration or ambient light | Recalibrate at start-up, shade the array |

## Upgrades

- **More sensors:** an eight-sensor array gives a finer position.
- **Speed by curvature:** slow down when the error is large.
- **Integral term:** add a small I term only if there is a steady offset.
- **Junction handling:** detect crossings when all sensors see the line.

## Cost and parts notes

The ₹1,640 total is an illustration and does not include shipping. Use the [cost calculator](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**Why use five sensors instead of two?**

Two sensors only say which side the line is on. Five give a position, so the robot can steer gently when it is a little off and hard when it is far off.

**What does the derivative term do?**

It reacts to how fast the error is changing, which damps the weaving that proportional control alone produces.

**Do I need analog outputs?**

They give a finer position. On/off outputs still work, with steps of half a sensor spacing.

**What are good values for Kp and Kd?**

There are no universal values. Start small, as in the tuning order, and tune on your own track.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.
