Ultrasonic Radar Scanner Using Arduino Uno, HC-SR04, and SG90 Servo
An ultrasonic radar is a classic robotics and sensor-fusion project that combines mechanical actuation with acoustic distance measurement. By mounting an HC-SR04 ultrasonic transceiver onto an SG90 micro servo, your Arduino Uno can sweep a arc, map nearby obstacles in real time, and trigger visual or audible proximity warnings.

In this tutorial you will wire the servo and ultrasonic sensor, write non-blocking sweep and pulse-timing code, output comma-delimited telemetry over USB serial, and optionally render a classic green-screen radar sweep on your computer.
How Ultrasonic Radar Works
The HC-SR04 acts like miniature sonar by emitting an inaudible $40\text{ kHz}$ ultrasonic burst and measuring how long the echo takes to bounce back from a solid surface.
- The Arduino rotates the SG90 servo in increments from and back.
- At each angle, the Arduino sends a HIGH pulse to the HC-SR04
TRIGpin. - The sensor transmits eight acoustic pulses and pulls its
ECHOpin HIGH until the reflection returns. - Using the speed of sound in air , or , the Arduino calculates distance as:
$$\text{Distance (cm)} = \frac{\text{Echo Duration (}\mu\text{s)} \times 0.0343}{2}$$
- The Arduino prints
angle,distance.over Serial so a PC visualizer or the Serial Monitor can plot obstacles, while lighting an onboard warning LED whenever an object enters a $30\text{ cm}$ threshold.
Hardware and Settings
| Item | Choice | Reason |
|---|---|---|
| Microcontroller | Arduino Uno or Nano | Built-in 16-bit timer support for the Servo library and reliable $5\text{ V}$ logic |
| Ultrasonic Sensor | HC-SR04 | Measures $2\text{ cm}$ to $400\text{ cm}$ with $\sim 3\text{ mm}$ resolution |
| Servo Motor | SG90 9g Micro Servo | Lightweight, low stall current ($\sim 360\text{ mA}$), and includes mounting horns |
| Indicator LED & Resistor | $5\text{ mm}$ Red LED + $220\text{ }\Omega$ resistor | Visual proximity alert when an object is closer than $30\text{ cm}$ |
| Mounting Bracket | Pan-tilt bracket or hot-glue + cardboard | Holds the HC-SR04 perpendicular to the servo rotation axis |
| Capacitor (Recommended) | $100\text{ }\mu\text{F}$ electrolytic capacitor | Smooths current spikes across the $5\text{ V}$ rail when the servo reverses direction |
Wiring Connections
Connect the components using a small breadboard or female-to-male jumper wires. If you use the $100\text{ }\mu\text{F}$ decoupling capacitor, place it across the breadboard's 5V and GND rails with the striped negative lead connected to GND.
HC-SR04 Sensor to Arduino
| HC-SR04 Pin | Arduino Pin | Notes |
|---|---|---|
| VCC | 5V | Sensor operates on $5\text{ V}$ DC |
| TRIG | D10 | Digital output trigger pulse |
| ECHO | D11 | Digital input echo duration pulse |
| GND | GND | Common ground |
SG90 Servo & Alert LED to Arduino
| Component Pin | Arduino Pin | Function |
|---|---|---|
| Servo Signal (Orange/Yellow) | D9 (PWM) | Servo angle control signal |
| Servo VCC (Red) | 5V | Power supply (keep load light; no mechanical resistance) |
| Servo GND (Brown/Black) | GND | Shared ground reference |
| Alert LED Anode ($+$) | D13 via $220\text{ }\Omega$ resistor | Lights up when target distance is under $30\text{ cm}$ |
| Alert LED Cathode ($-$) | GND | Ground return |
Complete Code
#include <Servo.h>
// Pin definitions
#define TRIG_PIN 10
#define ECHO_PIN 11
#define SERVO_PIN 9
#define ALERT_LED 13
// Radar configuration
#define MIN_ANGLE 15
#define MAX_ANGLE 165
#define STEP_DELAY_MS 25 // Time per degree (controls sweep speed)
#define ALERT_DISTANCE_CM 30 // Proximity alert threshold
#define MAX_RANGE_CM 200 // Ignore echoes beyond 2 meters
Servo radarServo;
int measureDistanceCm() {
// Ensure a clean LOW pulse before triggering
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Send 10 microsecond ultrasonic burst trigger
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Timeout after 15,000 us (~250 cm) so missing echoes don't freeze the sweep
unsigned long duration = pulseIn(ECHO_PIN, HIGH, 15000UL);
if (duration == 0) {
return MAX_RANGE_CM; // No obstacle detected within range
}
int distance = (duration * 0.0343) / 2.0;
return constrain(distance, 2, MAX_RANGE_CM);
}
void scanAtAngle(int angle) {
radarServo.write(angle);
delay(STEP_DELAY_MS);
int distance = measureDistanceCm();
// Trigger proximity LED if an object is close
if (distance <= ALERT_DISTANCE_CM) {
digitalWrite(ALERT_LED, HIGH);
} else {
digitalWrite(ALERT_LED, LOW);
}
// Output format compatible with Processing radar visualizers ("angle,distance.")
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(ALERT_LED, OUTPUT);
radarServo.attach(SERVO_PIN);
radarServo.write(MIN_ANGLE);
delay(500); // Allow servo to reach starting position
Serial.begin(9600);
}
void loop() {
// Sweep left to right (15° to 165°)
for (int angle = MIN_ANGLE; angle <= MAX_ANGLE; angle++) {
scanAtAngle(angle);
}
// Sweep right to left (165° to 15°)
for (int angle = MAX_ANGLE; angle >= MIN_ANGLE; angle--) {
scanAtAngle(angle);
}
}
Testing and Visualization Setup
- Upload the sketch to your Arduino Uno via the Arduino IDE. The servo should immediately sweep smoothly back and forth across a $150^\circ$ arc.
- Open the Serial Monitor (
Ctrl + Shift + M) at $9600\text{ baud}$. You will see a continuous stream formatted as15,142.16,140.17,138.representing each degree and its measured distance in centimeters. - Place your hand or a flat book $15\text{ cm}$ in front of the sensor. As the servo sweeps past the object, the reported distance will drop below
30and the alert LED on pin13will illuminate. - (Optional) To view a live graphical green radar sweep on your PC, install the free Processing IDE, load a standard Arduino Radar Processing sketch, and set the COM port string to match your Arduino's port (make sure the Arduino Serial Monitor is closed first so the port isn't locked).
Common Problems
- Arduino resets or USB disconnects when the servo reverses direction: The SG90 draws a brief current spike when changing direction. Place a $100\text{ }\mu\text{F}$ to $470\text{ }\mu\text{F}$ electrolytic capacitor across
5VandGND, or power the servo from an external $5\text{ V}$ supply sharing a common ground with the Arduino. - Readings randomly jump to
200(max range) even with an object nearby: Sound waves bounce away from angled or soft surfaces (like fabric or angled cardboard) instead of reflecting back to the receiver. Test with a flat, hard surface perpendicular to the sensor, or take the median of three quick pings per angle. - Servo jitters violently at the end stops ($0^\circ$ or $180^\circ$): Low-cost SG90 servos often hit their internal mechanical gear stops before reaching a true $0^\circ$ or $180^\circ$. Keep
MIN_ANGLEat15andMAX_ANGLEat165to prevent gear stripping and excessive current draw. - Processing visualizer shows "Port Busy" error: Only one program can read a USB COM port at a time. Close the Arduino IDE Serial Monitor before clicking Run in Processing.
Summary
Pairing an HC-SR04 ultrasonic sensor with an SG90 micro servo turns a single point-distance sensor into a $180^\circ$ spatial scanner. Adding a 15000UL timeout to pulseIn() prevents open-air readings from stalling the sweep, while comma-and-period serial formatting makes the telemetry ready for both debugging and desktop radar graphics.