Ultrasonic radar scanner with Arduino: design worksheet and code

A ₹890 design worksheet for an HC-SR04 on a sweeping servo that prints a distance map over serial, with wiring, a sketch and a test plan that shows where ultrasonic sensors mislead.

About the author · Sources, testing and AI policy

  • Parts₹890
  • Bench time2 h
  • LevelBeginner
  • ReviewedNov 2026

A ₹890 ultrasonic radar scanner that sweeps a room and prints a distance map. An HC-SR04 ultrasonic sensor sits on an SG90 servo. The Arduino sweeps the servo through an arc, takes a distance reading at each angle and prints angle and distance over serial. You can read the numbers, plot them in the Arduino Serial Plotter, or paste them into a spreadsheet to draw the room outline. The page includes the wiring, a complete sketch, a plotting method and a test plan that shows you where an ultrasonic sensor lies.

Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. It is a fixed scanner, not a robot, so it needs no motors or battery pack and runs from USB. It is also the best way to learn what an ultrasonic sensor sees before you trust one to steer a robot.

How it works

The sketch moves the servo one step at a time, waits for it to settle, fires the HC-SR04 and measures the echo time. Distance is the echo time multiplied by the speed of sound and divided by two, because the pulse travels out and back. The result for each angle is printed as one line. A full sweep out and back gives a profile of what is in front of the sensor, with the wide 15 degree beam of the sensor blurring narrow objects.

Bill of materials

PartQtyUnitTotal
Arduino Uno R31₹520₹520
HC-SR04 ultrasonic sensor1₹90₹90
SG90 servo1₹130₹130
Sensor mount bracket1₹40₹40
Small breadboard1₹60₹60
Jumper wire set1₹50₹50
Parts total₹890

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. A small servo and sensor mount, or a bracket you print or cut yourself, keeps the sensor stable on the horn.

Wiring

PartArduino UnoNote
HC-SR04 VCC / GND5V / GNDThe sensor needs 5 V
HC-SR04 TRIGD4Trigger output
HC-SR04 ECHOD8Echo input, 5 V logic
SG90 signalD9Servo signal
SG90 VCC / GND5V / GNDFine for one small servo from USB; use a separate rail if the Uno resets
If the board resets when the servo starts, power the servo from a separate 5 V supply with a common ground.

The sketch

// Ultrasonic radar scanner: Uno + HC-SR04 + SG90
// Prints "angle,distance_cm" lines. Open the Serial Monitor at 115200 baud.

#include <Servo.h>

const int TRIG = 4;
const int ECHO = 8;
const int SERVO_PIN = 9;

const int MIN_ANGLE = 15;
const int MAX_ANGLE = 165;
const int STEP = 5;
const int SETTLE_MS = 60;
const int MAX_CM = 200;

Servo scanner;

void setup() {
  Serial.begin(115200);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  scanner.attach(SERVO_PIN);
  scanner.write(MIN_ANGLE);
  delay(800);
}

void loop() {
  for (int angle = MIN_ANGLE; angle <= MAX_ANGLE; angle += STEP) {
    report(angle);
  }
  for (int angle = MAX_ANGLE; angle >= MIN_ANGLE; angle -= STEP) {
    report(angle);
  }
}

void report(int angle) {
  scanner.write(angle);
  delay(SETTLE_MS);
  int cm = distanceCm();
  Serial.print(angle);
  Serial.print(",");
  Serial.println(cm);
}

int distanceCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  unsigned long duration = pulseIn(ECHO, HIGH, 12000);
  if (duration == 0) {
    return 0;                         // no echo: out of range or soft target
  }
  int cm = duration / 58;             // about 58 microseconds per cm, out and back
  return (cm > MAX_CM) ? 0 : cm;
}

A reading of 0 means “no usable echo”, not “an object at zero distance”. The 12 ms timeout limits the range to roughly two metres and stops one missed echo from stalling the whole sweep. The divisor of 58 assumes the speed of sound at room temperature; it varies a little with temperature, so treat the output as approximate.

Seeing the map

  1. Quick look: read the Serial Monitor output as a table of angle and distance.
  2. Plot: copy a few sweeps into a spreadsheet and draw a scatter chart of X = distance × cos(angle) and Y = distance × sin(angle) to see the room outline from above. Skip the zero readings.
  3. Code plot: a short Python script with a plotting library can read the serial port and draw a live polar plot; keep the baud rate the same on both sides.

Test plan

  1. Tape measure: aim at a flat wall at 20, 50 and 100 cm and compare the output with the tape. Fix any large offset before anything else.
  2. Sweep limits: check the servo reaches both ends without straining. Narrow MIN_ANGLE and MAX_ANGLE if it hums.
  3. Narrow object: put a bottle in front of the scanner. Note how many angles it appears in; the wide beam smears it.
  4. Angled wall: point at a wall at a sharp angle. The echo may bounce away and read 0 or far.
  5. Soft surface: try a curtain or a cushion. Soft materials absorb sound and may not read.
  6. Corner: place the scanner in a room corner and see whether the corner distances look right or double-bounce.

What usually goes wrong

SymptomLikely causeFix
All zerosMissing ground, swapped TRIG/ECHO or no 5 VCheck grounds, then swap the signal wires
Noisy, jumping valuesServo noise or a moving sensor mid-readingIncrease SETTLE_MS, average two readings
Board resets at startServo current from USBSeparate 5 V rail with a common ground
Objects look wider than they areWide sensor beamThis is the sensor; use a narrower beam sensor for detail
Sweep is slowSettle time and sensor timeoutUse a larger step or shorter timeout
Distances drift with the weatherSpeed of sound changes with temperatureAdd a temperature sensor and correct the divisor

Upgrades

  • Mount it on a robot: use the scan to choose a direction, as the obstacle avoiding robot does.
  • Display: draw the sweep on a small OLED or TFT as a radar screen.
  • Better sensor: try a time-of-flight sensor for a narrower beam and compare the two on the same scene.
  • Second axis: add a tilt servo for a rough 3D scan, remembering the sensor’s wide beam.

Cost and parts notes

The ₹890 total is an illustration for the parts above and does not include shipping. The HC-SR04 guide explains the beam cone and the surfaces that give false readings. Use the cost calculator to compare sellers.

Common questions

Is this accurate enough to map a room?

It gives a rough outline. Beam width, soft surfaces and angled walls all cause errors, so treat it as a sketch, not a survey.

Why does the sketch print 0 for some angles?

Zero means no echo came back in time. It can be an out-of-range wall, a soft object or a surface at a steep angle.

Do I need a battery?

No. A single small servo and sensor can run from USB. Add a separate supply only if the board resets.

Can I use an ESP32?

Yes, but the HC-SR04 echo pin outputs 5 V, which can damage a 3.3 V input. Use a divider or level shifter.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; angles, timing and the distance divisor are starting values, 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.