Home › DIY Projects › Ultrasonic Radar

Ultrasonic Radar with Arduino, HC-SR04 and Servo

Build a mini radar that sweeps the room like in the movies and spots anything closer than 40 cm. Great for science fairs and robot projects.

  • Level: Beginner
  • Time: 45 minutes
  • Board: Arduino Uno
  • No soldering
▶ Copy code + open simulatorGet the code

Watch it in action

Subscribe to ZEENAIX on YouTube for more projects

What you will build

An HC-SR04 ultrasonic sensor sits on top of an SG90 servo. The servo turns the sensor from 15° to 165° and back, three degrees at a time, about once every 4 seconds. At every step the Arduino measures the distance and sends it to your computer as angle,distance. When something is closer than 40 cm, it also prints OBJECT at 87 deg, 24 cm, telling you where the object is.

You will learn how to combine a sensor and a motor, how to step a servo through angles with a for loop, and how angle plus distance gives a position on a map (the same idea real radar and robot vacuum cleaners use). Below you can play with a live radar screen that uses the same rules.

Components required

Arduino Uno R3 compatible boardArduino Uno R3 × 1The brain. Any Uno or Nano works.
View in store →
HC-SR04 ultrasonic distance sensorHC-SR04 ultrasonic sensor × 1Measures about 2 cm to 4 m.
View in store →
SG90 micro servo motorSG90 micro servo × 1Turns the sensor from side to side.
View in store →
Breadboard × 1Half or full size.
View in store →
Jumper wires (male to male) × 10About 10 wires.
View in store →

Circuit diagram and connections

Tap any part in the diagram to see what it does and where it connects.

Arduino Uno9 1065V GND HC-SR04 SG90 sensor sits on the servo horn Red = 5V · Black/Brown = GND · Orange = signal / Trig · Blue = Echo

Tap a part to learn more.

Part pinArduino pinWire colour (suggested)
HC-SR04 VCC5VRed
HC-SR04 TrigD9Orange
HC-SR04 EchoD10Blue
HC-SR04 GNDGNDBlack
SG90 signal (orange)D6Orange
SG90 power (red)5VRed
SG90 ground (brown)GNDBrown or black

Arduino code

Copy this sketch into the simulator or the Arduino IDE. We wrote and tested it ourselves; you are free to use and change it.

/*
  ZEENAIX DIY Project: Ultrasonic Radar with Arduino
  https://zeenaix.com/diy-projects/ultrasonic-radar-arduino/

  Parts: Arduino Uno, HC-SR04 ultrasonic sensor, SG90 micro servo, jumper wires.

  Wiring:
    HC-SR04  VCC -> 5V   GND -> GND   TRIG -> pin 9   ECHO -> pin 10
    SG90     red -> 5V   brown -> GND orange (signal) -> pin 6
    Sit the HC-SR04 on top of the servo horn so it turns with it.

  What it does:
    The servo sweeps the sensor from 15 to 165 degrees and back, in 3 degree
    steps. At every step the Arduino measures the distance and prints
    "angle,distance" so a radar screen can draw it. Anything closer than
    ALERT_CM is reported as an object.
*/

#include <Servo.h>

const int TRIG_PIN  = 9;
const int ECHO_PIN  = 10;
const int SERVO_PIN = 6;

const int MIN_ANGLE = 15;
const int MAX_ANGLE = 165;
const int STEP      = 3;     // degrees per step
const int ALERT_CM  = 40;    // report objects closer than this

Servo turret;

long readCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);  // 30 ms = about 5 m
  if (us == 0) return -1;                                // nothing in range
  return (long)(us / 58);                                // microseconds to cm
}

void scanAt(int angle) {
  turret.write(angle);
  delay(30);                     // give the servo time to get there
  long cm = readCm();
  Serial.print(angle);           // radar format: angle,distance
  Serial.print(',');
  Serial.println(cm);
  if (cm > 0 && cm < ALERT_CM) {
    Serial.print("OBJECT at ");
    Serial.print(angle);
    Serial.print(" deg, ");
    Serial.print(cm);
    Serial.println(" cm");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  turret.attach(SERVO_PIN);
  Serial.println("ZEENAIX Ultrasonic Radar ready");
}

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

Step-by-step instructions

A. Try it in the free simulator (5 minutes)

  1. Open the ZEENAIX Arduino Simulator and choose Blank editor with the Arduino Uno board.
  2. Delete the starter code, then paste the sketch from the code section.
  3. Click Libraries, search Servo and click + Add to project (the one by Michael Margolis). Without it you get Servo.h: No such file.
  4. Tap + Add and place an HC-SR04 ultrasonic sensor and a servo.
  5. Draw the wires exactly as in the connection table above.
  6. Press the green ▶ run button. The servo arm starts sweeping from side to side.
  7. Open Serial to see a stream of angle,distance lines. Tap the sensor and set its distance below 40 cm: OBJECT at … deg lines appear.

B. Build it for real (40 minutes)

  1. Stick the HC-SR04 on the servo horn with double-sided tape or a small 3D-printed or cardboard bracket, so the sensor turns with the servo.
  2. Fix the servo to a flat base (a piece of cardboard or a small box) so it does not tip over while sweeping.
  3. Connect the HC-SR04 and the servo to the Uno as in the connection table. Use the breadboard to share 5V and GND between them.
  4. Plug the Uno into your computer, open the Arduino IDE, select Tools › Board › Arduino Uno and the right COM port, paste the code and click Upload. The Servo library comes with the Arduino IDE.
  5. Open Tools › Serial Monitor at 9600 baud and watch the readings. Move a book or your hand in front of the radar to see OBJECT alerts.
  6. For a radar screen on your computer, open Tools › Serial Plotter, or feed the angle,distance lines into a Processing or Python sketch.

Try it here: live radar screen

ZEENAIX Ultrasonic Radar ready

Same rules as the sketch: 15° to 165° in 3° steps, objects closer than 40 cm are reported. The radar shows up to 100 cm.

How it works

The servo gets a new angle from turret.write(angle), waits 30 ms to arrive, and then the HC-SR04 takes one reading. The echo time divided by 58 gives the distance in centimetres. Each pair of numbers, the angle and the distance, is one dot on the radar.

To draw the dot, the screen turns those two numbers into x and y with a little trigonometry: x = distance × cos(angle) and y = distance × sin(angle). An object at 90° and 25 cm is straight ahead, 25 cm away. One full sweep from 15° to 165° and back is 100 steps, about 4 seconds.

Tested: we compiled this sketch for the Arduino Uno (Servo library, zero warnings) and ran it in an AVR simulation with an object placed straight ahead at 25 cm. The servo stepped in 3° steps (pulse widths matched each angle), the open room read 148 cm, and as the sweep passed 87° to 99° it printed OBJECT at 87 deg, 24 cm and the following angles, then went back to the far readings.

Real-life uses of a scanning distance sensor

Science fair radar

A sweeping radar with a live screen is a crowd favourite at school and college exhibitions.

Obstacle-avoiding robot

Mount it on a robot car: the robot looks left and right, then turns towards the side with the most free space.

Room mapping

Collect the angle and distance pairs to draw a rough map of a room, like a very simple LiDAR.

Security alert

Leave it sweeping a doorway or a shelf. When anything appears closer than usual, sound a buzzer or send a message with an ESP32.

Parking guide with direction

Unlike a fixed sensor, the radar tells you whether an obstacle is on the left, centre or right.

Troubleshooting

  • Servo jitters or the board resets: power the servo from a separate 5V supply or a good 1A USB charger, and keep the GNDs connected.
  • Readings at every angle look the same: the sensor is probably not turning. Check it is fixed firmly to the servo horn.
  • Random far or zero readings: soft or angled surfaces scatter sound. Test with a flat book or a box.
  • Sweep is too fast to read: increase the delay(30) in scanAt() to 60 or raise STEP.

Frequently asked questions

How far can this radar see?

The HC-SR04 measures about 2 cm to 4 m. For a clear radar screen, 5 cm to 2 m works best.

How do I show a radar screen on my computer?

The sketch prints angle,distance lines. Open the Arduino Serial Plotter, or read the serial port from a Processing or Python sketch and draw a dot for each line.

Is this real radar?

Real radar uses radio waves. This project uses ultrasound (sound too high for people to hear), which works the same way for short distances. It is often called "sonar".

Do I need to buy parts to try this?

No. You can run the whole project in the free ZEENAIX simulator first, then buy only the parts you need when you are ready to build it.

Build it for real

Get the Arduino Uno, HC-SR04 sensor, SG90 servo, breadboard and jumper wires from the ZEENAIX store, with delivery across India.

▶ Copy code + open simulatorMore DIY projects