Servo sweep ultrasonic radar with serial output on Arduino: design worksheet and code

A ₹930 design worksheet for a sweeping ultrasonic scanner: an HC-SR04 on a servo, serial angle-and-distance output you can plot on a PC, with the sweep timing, beam-width limits and polar-to-Cartesian maths worked out.

About the author · Editorial & verification standards

  • Parts₹930
  • Bench time3–5 h
  • LevelIntermediate
  • ReviewedNov 2026

A ₹930 scanner that sweeps an ultrasonic sensor back and forth and reports an angle and a distance for each step. The numbers can be plotted on a PC as a radar-style map. The page works out how long a sweep takes, how coarse the picture really is because of the sensor’s wide beam, and how to turn angle and distance into x and y.

Scope: intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The sweep timing, beam figure and coordinate table below were computed by script. The beam width of the HC-SR04 is commonly given as about 15 degrees; treat that as a typical listing value, not a measurement. The result is a coarse picture, good for spotting walls and large objects, not for mapping fine detail.

How it works

The servo steps from 15 to 165 degrees in 2 degree steps, which is 76 positions. At each step the sketch waits 60 ms, which is enough for the small servo to move and for the previous echo to die away, then takes one distance reading and prints it as angle,distance. A full sweep takes about 76 × 60 ms = 4.6 seconds.

Because the beam is wide, nearby steps see overlapping areas. At a range of 100 cm a 15 degree cone is about 2 × 100 × tan 7.5° = 26 cm wide, so a thin object looks wider than it is, and the 2 degree step oversamples the picture. Converting to x and y uses x = d × cos θ and y = d × sin θ, with the sensor at the origin.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
HC-SR04 ultrasonic sensor1₹110₹110
SG90 micro servo1₹120₹120
Small bracket or card mount for the sensor1₹40₹40
Breadboard1₹60₹60
Jumper wires1₹50₹50
Parts total₹930

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

AngleDistancexy
30°100 cm86.650.0
60°100 cm50.086.6
90°50 cm0.050.0
120°80 cm-40.069.3
150°120 cm-103.960.0
x = d × cos θ, y = d × sin θ, in centimetres, with 0° pointing right and 90° straight ahead.

A 60 ms step time keeps the readings from echoing into each other; sensor guides commonly suggest leaving roughly 60 ms between measurements. It means each sweep is slow, so a moving object will smear.

Power and wiring

PartConnects toNote
HC-SR04 VCC and GND5V and GND
HC-SR04 TRIG / ECHOD9 / D10
Servo signalD6
Servo power5V and GND, or a separate 5 V supplyJoin grounds if separate
Fix the sensor to the servo horn so it looks straight ahead at 90 degrees, and keep the cable slack so it does not pull as the sensor turns.

The sketch

// Servo sweep ultrasonic radar: Uno + HC-SR04 + SG90
// Design worksheet: prints "angle,distance_cm" lines for plotting.

#include <Servo.h>

const int TRIG = 9, ECHO = 10, SERVO_PIN = 6;
const int MIN_A = 15, MAX_A = 165, STEP_A = 2;
const int SETTLE_MS = 60;

Servo head;

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 ? -1 : t / 58.3;            // -1 means no echo
}

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  head.attach(SERVO_PIN);
  Serial.begin(115200);
}

void sweepTo(int a) {
  head.write(a);
  delay(SETTLE_MS);
  float d = readCm();
  Serial.print(a);
  Serial.print(',');
  Serial.println(d, 1);
}

void loop() {
  for (int a = MIN_A; a <= MAX_A; a += STEP_A) sweepTo(a);
  for (int a = MAX_A; a >= MIN_A; a -= STEP_A) sweepTo(a);
}

The output is plain text, one line per step, so it can be pasted into a spreadsheet, plotted with a short script, or drawn with a program such as Processing. Distances of -1 mean no echo, usually because the surface is far away, soft or angled away from the sensor. The sketch has been proofread but not compiled on this site.

Test plan

  1. Straight-ahead check: confirm 90 degrees points straight forward, and adjust the horn if not.
  2. Known scene: place a box at a measured angle and distance and compare the output.
  3. Beam width: move a thin pole across the beam and see over how many angles it shows up.
  4. Wall test: scan a flat wall and see how the distance changes with angle; it should follow d = d0 / cos θ.
  5. Soft surfaces: try cloth and foam and note the -1 readings.
  6. Speed: time one sweep and compare it with the 4.6 second estimate.

What usually goes wrong

SymptomLikely causeFix
Distances all -1Sensor wiringCheck TRIG on D9 and ECHO on D10
Picture smearedWide beam and slow sweepAccept the limits or slow the sweep
Servo jittersSupply noiseSeparate 5 V supply
Readings jump at edgesEchoes from side objectsNarrow the sweep or shield the sensor
Mirror imageAngle direction reversedFlip the sign in your plot

Upgrades

  • Live plot: draw the points on a PC as they arrive.
  • Navigation: put the scanner on a robot and steer toward the largest gap.
  • Faster sensor: try an infrared time-of-flight sensor with a narrower beam.
  • Two-axis scan: add a tilt servo for a rough 3D map.

Cost and parts notes

The ₹930 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

The sensor’s beam is wide, so each reading covers a broad area and thin objects look wider than they are.

The servo needs time to move and the previous echo needs time to fade.

Convert each line with x = d cos θ and y = d sin θ and plot the points.

Roughly, for walls and large furniture. It is not precise enough for detail.

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.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.