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
| Part | Qty | Unit | Total |
|---|---|---|---|
| Arduino Uno R3 | 1 | ₹520 | ₹520 |
| HC-SR04 ultrasonic sensor | 1 | ₹90 | ₹90 |
| SG90 servo | 1 | ₹130 | ₹130 |
| Sensor mount bracket | 1 | ₹40 | ₹40 |
| Small breadboard | 1 | ₹60 | ₹60 |
| Jumper wire set | 1 | ₹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
| Part | Arduino Uno | Note |
|---|---|---|
| HC-SR04 VCC / GND | 5V / GND | The sensor needs 5 V |
| HC-SR04 TRIG | D4 | Trigger output |
| HC-SR04 ECHO | D8 | Echo input, 5 V logic |
| SG90 signal | D9 | Servo signal |
| SG90 VCC / GND | 5V / GND | Fine for one small servo from USB; use a separate rail if the Uno resets |
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
- Quick look: read the Serial Monitor output as a table of angle and distance.
- 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.
- 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
- 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.
- Sweep limits: check the servo reaches both ends without straining. Narrow
MIN_ANGLEandMAX_ANGLEif it hums. - Narrow object: put a bottle in front of the scanner. Note how many angles it appears in; the wide beam smears it.
- Angled wall: point at a wall at a sharp angle. The echo may bounce away and read 0 or far.
- Soft surface: try a curtain or a cushion. Soft materials absorb sound and may not read.
- Corner: place the scanner in a room corner and see whether the corner distances look right or double-bounce.
What usually goes wrong
| Symptom | Likely cause | Fix |
|---|---|---|
| All zeros | Missing ground, swapped TRIG/ECHO or no 5 V | Check grounds, then swap the signal wires |
| Noisy, jumping values | Servo noise or a moving sensor mid-reading | Increase SETTLE_MS, average two readings |
| Board resets at start | Servo current from USB | Separate 5 V rail with a common ground |
| Objects look wider than they are | Wide sensor beam | This is the sensor; use a narrower beam sensor for detail |
| Sweep is slow | Settle time and sensor timeout | Use a larger step or shorter timeout |
| Distances drift with the weather | Speed of sound changes with temperature | Add 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.