All projects
📡🟡 Intermediate 1.5 hrs

Ultrasonic Radar

A sweeping radar that measures distance while a servo turns the sensor left and right.

What You'll Build

You will build a miniature radar station. A servo slowly sweeps an ultrasonic sensor from 15° to 165° and back, and at every angle the sensor measures how far away the nearest object is. The angle and distance pair gets printed to the Serial Monitor so you can watch objects appear and disappear as the beam passes over them.

What You Need

  • Arduino Uno
  • HC-SR04 ultrasonic sensor
  • SG90 servo
  • Breadboard
  • Jumper wires

How It Works

The HC-SR04 works like a bat. It sends out a burst of sound too high for humans to hear, then measures how long the echo takes to come back. Because sound travels about 0.034 cm per microsecond, distance equals time divided by two, multiplied by that speed. The servo simply rotates the sensor so you can scan a whole area instead of one direction.

Wiring

  1. 1Servo signal → pin 11, red → 5V, brown/black → GND.
  2. 2HC-SR04 VCC → 5V, GND → GND.
  3. 3HC-SR04 Trig → pin 10, Echo → pin 9.

Step-by-Step Instructions

  1. 1

    Mount the sensor

    Tape or glue the ultrasonic sensor onto the servo horn so the two 'eyes' face forward. Keep the wires loose enough to turn.

  2. 2

    Power carefully

    A single small servo is fine on the Arduino's 5V pin, but if it jitters, feed it from a separate battery pack and join the grounds together.

  3. 3

    Test distance alone

    Before adding movement, upload a short sketch that just prints the distance. Wave your hand and check the numbers make sense.

  4. 4

    Add the sweep

    Now let the servo step one degree at a time. A short delay after each step gives the servo time to actually arrive before you measure.

  5. 5

    Read the output

    Open the Serial Monitor and watch pairs like 90,42 — angle 90 degrees, 42 centimetres away.

Arduino Code

ultrasonic-radar.ino
#include <Servo.h>

Servo radar;
const int TRIG = 10;
const int ECHO = 9;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  radar.attach(11);
  Serial.begin(9600);
}

int readDistance() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long duration = pulseIn(ECHO, HIGH);
  return duration * 0.034 / 2;
}

void loop() {
  for (int angle = 15; angle <= 165; angle++) {
    radar.write(angle);
    delay(30);
    Serial.print(angle);
    Serial.print(",");
    Serial.println(readDistance());
  }
  for (int angle = 165; angle > 15; angle--) {
    radar.write(angle);
    delay(30);
    Serial.print(angle);
    Serial.print(",");
    Serial.println(readDistance());
  }
}

Understanding the Code

#include <Servo.h>

Loads Arduino's servo library so you can command angles instead of writing tricky pulse code yourself.

delayMicroseconds(10)

The HC-SR04 needs a 10 microsecond pulse on Trig to fire its sound burst.

pulseIn(ECHO, HIGH)

Waits and measures how long the Echo pin stays HIGH — that is the round trip time of the sound.

duration * 0.034 / 2

Turns microseconds into centimetres, halved because the sound travelled there and back.

for (int angle = 15; ...)

A loop that counts the angle up one step at a time, sweeping the servo.

Challenges & Upgrades

Trigger a buzzer when something comes closer than 20 cm.
Draw the radar on your computer using Processing or p5.js.
Speed up the sweep and see how it affects accuracy.

Troubleshooting

😖 Distance reads 0 or huge numbers

Fix: Trig and Echo may be swapped, or the object is closer than 2 cm — the sensor's minimum range.

😖 Servo buzzes and resets the board

Fix: It is pulling too much current. Power it separately and connect the grounds.

Up next

🌡️ Mini Weather Station

Build This