Arduino Radar Using HC-SR04 Ultrasonic Sensor

An Arduino radar project is a simple object-detection system that scans an area and measures the distance of nearby objects. It uses an HC-SR04 ultrasonic sensor mounted on a servo motor. The servo rotates the sensor through a range of angles, while the Arduino measures the distance at each position.

Illustration

The measured angle and distance can be sent to a computer through the Serial Monitor or used with a compatible visualization program to display a radar-style sweep.

How Arduino Radar Works

The HC-SR04 measures distance by sending an ultrasonic pulse and listening for its echo.

  1. The Arduino commands the servo motor to move the sensor to a particular angle.
  2. The HC-SR04 sends an ultrasonic burst when its TRIG pin receives a short HIGH pulse.
  3. The sound wave reflects from an object and returns to the sensor.
  4. The ECHO pin stays HIGH for the time taken by the sound wave to travel to the object and back.
  5. The Arduino calculates the distance and sends the angle and distance over serial.
  6. The servo repeats the scan across the selected angle range.

The approximate distance formula is:

\[ \text{Distance (cm)}{=tex} = \frac{\text{Echo time in microseconds} \times 0.0343}{2}{=tex} \]

The division by two accounts for the sound travelling to the object and returning.

Hardware and Settings

----------------------------------------------------------------------- Item Choice Reason ----------------------- ----------------------- ----------------------- Microcontroller Arduino Uno Reads the sensor and controls the servo

Ultrasonic Sensor HC-SR04 Measures the distance to nearby objects

Servo Motor SG90 micro servo Rotates the ultrasonic sensor

Jumper Wires Male-to-male Makes the circuit connections

Breadboard Standard breadboard Helps organize the wiring

USB Cable USB Type-A to Type-B Powers/programs the Arduino Uno

Wiring Connections

Connect the components as shown below.

HC-SR04 Ultrasonic Sensor to Arduino

HC-SR04 Pin Arduino Pin Function

VCC 5V Sensor power GND GND Common ground TRIG D9 Starts the ultrasonic measurement ECHO D10 Returns the echo pulse to the Arduino

Servo Motor to Arduino

Servo Wire Arduino Pin Function Brown or Black GND Ground Red 5V supply Positive supply Orange, Yellow or White D3 Servo control signal

Wire colours can vary between servo manufacturers. Check the labels or datasheet for your servo before connecting it.

Complete Code

Arduino radar scanner: rotate the HC-SR04 with a servo, measure distance, and print angle-distance values to the Serial Monitor.
#include <Servo.h>

const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 3;

const int minAngle = 15;
const int maxAngle = 165;

Servo radarServo;

float measureDistanceCm() {
  // Ensure the trigger starts LOW
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Send a 10-microsecond trigger pulse
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo pulse; time out after 30 ms
  unsigned long duration = pulseIn(echoPin, HIGH, 30000UL);

  // No echo received within the timeout
  if (duration == 0) {
    return -1.0;
  }

  return (duration * 0.0343) / 2.0;
}

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.begin(9600);

  radarServo.attach(servoPin);
  radarServo.write(minAngle);
  delay(500);

  Serial.println("angle,distance_cm");
}

void loop() {
  // Sweep from left to right
  for (int angle = minAngle; angle <= maxAngle; angle++) {
    radarServo.write(angle);
    delay(20);

    float distance = measureDistanceCm();

    Serial.print(angle);
    Serial.print(",");
    if (distance < 0) {
      Serial.println("0");
    } else {
      Serial.println(distance, 1);
    }
  }

  // Sweep from right to left
  for (int angle = maxAngle; angle >= minAngle; angle--) {
    radarServo.write(angle);
    delay(20);

    float distance = measureDistanceCm();

    Serial.print(angle);
    Serial.print(",");
    if (distance < 0) {
      Serial.println("0");
    } else {
      Serial.println(distance, 1);
    }
  }
}

Uploading and Testing

  1. Connect the Arduino Uno to your computer using a USB cable.
  2. Open the Arduino IDE and select Arduino Uno under Tools \> Board.
  3. Select the correct serial port under Tools \> Port.
  4. Copy the code into a new sketch and upload it.
  5. Open the Serial Monitor and set the baud rate to 9600.
  6. Place an object in front of the sensor. The output will show the servo angle and measured distance in centimetres.
  7. Move the object to different positions and observe how the distance values change as the sensor scans.

The serial output uses comma-separated values in the format angle,distance_cm. A distance of 0 indicates that no echo was received before the timeout.

Radar Visualization

The Arduino sketch sends one angle-distance pair per line. A computer-side visualization can read these values over the serial port and draw a sweep line and detected objects on a radar-style display.

For a visualization program:

  • Read serial data at 9600 baud.
  • Split each line at the comma.
  • Treat the first value as the servo angle in degrees.
  • Treat the second value as the measured distance in centimetres.
  • Ignore zero-distance readings when plotting detected objects.
  • Set a maximum display range that matches the useful range of your sensor and setup.

The sketch in this tutorial outputs serial data only; it does not include a separate Processing visualization program.

Common Problems

  • The sensor always reports zero distance: Check the TRIG and ECHO wiring, confirm that the sensor has 5V power, and make sure an object is within the sensor's detection range.
  • The distance readings are unstable: Keep the sensor steady, avoid soft or angled target surfaces, and allow a short interval between measurements. Ultrasonic readings can be affected by nearby objects and reflections.
  • The servo does not rotate: Check the signal wire on D3, verify the servo's power and ground connections, and confirm that the servo is not mechanically blocked.
  • The Arduino resets when the servo moves: The servo may be drawing more current than the Arduino's 5V supply can provide. Use a suitable external regulated 5V supply for the servo and connect its ground to Arduino GND.
  • The servo moves but the scan is not smooth: Check for loose wires, reduce mechanical friction, and adjust the delay(20) value if your servo needs more settling time.
  • The Serial Monitor displays unreadable characters: Set the Serial Monitor baud rate to 9600, matching Serial.begin(9600) in the sketch.

Summary

The Arduino radar project combines an HC-SR04 ultrasonic sensor with a servo motor to scan an area and measure object distances at different angles. The Arduino calculates distance from the echo pulse duration and sends angle-distance readings to the computer through serial communication. These readings can be used to build a radar-style visualization and provide a practical introduction to ultrasonic sensing, servo control, and serial data communication.