# Wall-following robot with an ultrasonic sensor and PD steering: design worksheet and code

> A ₹1,550 design worksheet for a robot that drives alongside a wall at a set distance, using one side-facing ultrasonic sensor and PD steering, with the angle-error geometry worked out.

Canonical: https://roboticsengineering.in/projects/wall-following-robot-ultrasonic-pd-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**A ₹1,550 robot that follows a wall at about 15 cm. One HC-SR04 faces the wall, the sketch compares the distance with a target, and PD steering nudges the wheels to correct it. The page includes the geometry of what the sensor actually reads when the robot is not parallel, because that is where most wall-followers go wrong.**

**Scope:** intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The geometry table was computed by script. The gains are starting guesses. A single side sensor cannot see a wall ahead, so the version here is for a long straight wall with gentle corners; the upgrades list the fix.

## How it works

The sensor measures along its own beam, not along the shortest line to the wall. If the robot is turned by an angle θ away from parallel, the reading is the true perpendicular distance divided by cos θ. At 15 cm and 20 degrees that is 15 / 0.940 = 15.96 cm. The error is small for small angles, which is why proportional steering works, but at 30 degrees the reading is 17.3 cm and it looks like the robot is drifting away when it is just turned.

The sketch computes error = distance − target. A positive error means too far from the wall, so the wheel on the wall side slows and the other speeds up. Correction = Kp × error + Kd × (error − last error); the derivative term acts against swinging.

## Bill of materials

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| HC-SR04 ultrasonic sensor | 1 | ₹110 | ₹110 |
| 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 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Parts total | ₹1,550 |  |  |

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.

## The numbers

| Robot angle to wall | Reading for 15 cm perpendicular | Apparent error |
| --- | --- | --- |
| 0° | 15.0 cm | 0.0 cm |
| 10° | 15.2 cm | +0.2 cm |
| 20° | 16.0 cm | +1.0 cm |
| 30° | 17.3 cm | +2.3 cm |
| 45° | 21.2 cm | +6.2 cm |

*Reading = perpendicular distance / cos(angle). At 45° the beam also tends to glance off a smooth wall and may return no echo at all.*

With Kp = 6 and a wheel base speed of 130, an error of +2 cm adds 12 PWM steps to one wheel and subtracts 12 from the other. If the reading is lost (no echo), the sketch treats it as 10 cm too far, which makes the robot turn toward the wall rather than away.

## Power and wiring

| Part | Connects to | Note |
| --- | --- | --- |
| HC-SR04 VCC and GND | 5V and GND |  |
| HC-SR04 TRIG / ECHO | D12 / D13 | Sensor faces the wall, mounted level |
| L298N ENA / IN1 / IN2 | D5 / D7 / D8 | Left motor |
| L298N ENB / IN3 / IN4 | D6 / D9 / D4 | Right motor |
| Battery | L298N 12V input and Uno VIN | Check your pack voltage against the board limits |

*This version follows a wall on the robot’s left. For a right-hand wall, mount the sensor on the right and swap the sign of the steering.*

## The sketch

```
// Wall-following robot: Uno + HC-SR04 (left side) + L298N
// Design worksheet: gains and target are starting values.

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

const float TARGET = 15.0;      // cm from the wall
const int BASE = 130;
float Kp = 6.0, Kd = 12.0;
float lastError = 0;

float readCm() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long t = pulseIn(ECHO, HIGH, 25000UL);
  return t == 0 ? 999 : t / 58.3;
}

void drive(int left, int right) {
  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(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  pinMode(L_EN, OUTPUT); pinMode(L_IN1, OUTPUT); pinMode(L_IN2, OUTPUT);
  pinMode(R_EN, OUTPUT); pinMode(R_IN3, OUTPUT); pinMode(R_IN4, OUTPUT);
}

void loop() {
  float d = readCm();
  float error = (d > 60) ? 10.0 : d - TARGET;   // lost wall: turn toward it
  error = constrain(error, -10.0, 10.0);

  float steer = Kp * error + Kd * (error - lastError);
  lastError = error;
  // positive error = too far from the left wall: slow the left wheel
  drive(BASE - steer, BASE + steer);
  delay(60);                                     // one ping per 60 ms
}
```

The loop time is fixed at about 60 ms, so retune Kd if you change it. If the robot turns away from the wall, the sign of steer is reversed for your wiring. The sketch has been proofread but not compiled on this site.

## Test plan

1. **Distance accuracy:** print readings with the robot held at 10, 15 and 20 cm from the wall.
2. **Angle check:** hold it at 20 and 30 degrees and compare the readings with the table.
3. **Straight wall:** start it parallel at 15 cm and watch for weaving; lower Kp if it oscillates.
4. **Offset start:** start it at 25 cm and 8 cm and see how it settles.
5. **Corner test:** try a gentle outside corner and note where it loses the wall.
6. **Surface test:** try painted wood, tile, cardboard and fabric.

## What usually goes wrong

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Oscillates along the wall | Kp too high or Kd too low | Lower Kp, raise Kd |
| Drifts away slowly | Kp too low | Raise Kp |
| Veers into the wall at a gap | No echo treated badly | Check the lost-wall rule |
| Readings jump | Specular reflection from smooth walls | Use a rougher wall or angle the sensor slightly |
| Hits a wall ahead | No front sensor | Add one at the front |

## Upgrades

- **Front sensor:** add one at the front so it turns at inside corners.
- **Servo scan:** mount the sensor on a servo to look ahead and sideways.
- **Filtering:** take the median of three readings.
- **Speed by error:** slow down when the error is large.

## Cost and parts notes

The ₹1,550 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 does the angle matter?**

The sensor measures along its beam. When the robot is turned, that length is longer than the perpendicular distance to the wall.

**Can one sensor follow any wall?**

It can follow a long straight wall. Corners and gaps need a second sensor.

**What does the derivative term do?**

It reacts to how fast the error is changing, which damps the swinging from side to side.

**What if the wall is on the right?**

Mount the sensor on the right and swap the signs of the steering.

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.
