# 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.

Canonical: https://roboticsengineering.in/projects/servo-sweep-ultrasonic-radar-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**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

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| HC-SR04 ultrasonic sensor | 1 | ₹110 | ₹110 |
| SG90 micro servo | 1 | ₹120 | ₹120 |
| Small bracket or card mount for the sensor | 1 | ₹40 | ₹40 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹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

| Angle | Distance | x | y |
| --- | --- | --- | --- |
| 30° | 100 cm | 86.6 | 50.0 |
| 60° | 100 cm | 50.0 | 86.6 |
| 90° | 50 cm | 0.0 | 50.0 |
| 120° | 80 cm | -40.0 | 69.3 |
| 150° | 120 cm | -103.9 | 60.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

| Part | Connects to | Note |
| --- | --- | --- |
| HC-SR04 VCC and GND | 5V and GND |  |
| HC-SR04 TRIG / ECHO | D9 / D10 |  |
| Servo signal | D6 |  |
| Servo power | 5V and GND, or a separate 5 V supply | Join 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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Distances all -1 | Sensor wiring | Check TRIG on D9 and ECHO on D10 |
| Picture smeared | Wide beam and slow sweep | Accept the limits or slow the sweep |
| Servo jitters | Supply noise | Separate 5 V supply |
| Readings jump at edges | Echoes from side objects | Narrow the sweep or shield the sensor |
| Mirror image | Angle direction reversed | Flip 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](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**Why is the picture coarse?**

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

**Why wait 60 ms per step?**

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

**How do I draw the output?**

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

**Can it map a room?**

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.
