RADAR SYSTEM FULLY WORKING WITH CIRCUIT CODE AND SIMULATION

RADAR System Using Arduino UNO & HC-SR04 Ultrasonic Sensor – MakeMindz
RADAR System Arduino UNO HC-SR04 SG90 Servo Intermediate Tinkercad Simulation

RADAR System Using Arduino UNO & Ultrasonic Sensor

Build a fully functional 180° scanning RADAR that sweeps an HC-SR04 ultrasonic sensor on a servo motor, measures distances at every angle, and streams live data to the Serial Monitor — a perfect STEM and science fair project.

What You'll Build

The servo rotates the HC-SR04 sensor in 1° steps from 0° to 180°. At every angle the sensor fires an ultrasonic pulse, times the echo, and calculates the distance. The Arduino prints both the angle and distance to the Serial Monitor — simulating the sweep of a real RADAR system. Optional: pipe the Serial output into the Processing IDE to render a graphical RADAR display.

📡

Scan Range

0° – 180°

📏

Max Distance

~400 cm

Difficulty

Intermediate

⏱️

Build Time

1 – 2 Hours

How It Works

90° 180°

5-Step Scan Cycle

  • Step 1 — Servo writes angle (0–180°) and waits 15ms to settle
  • Step 2 — Arduino pulses TRIG pin HIGH for 10µs
  • Step 3 — Ultrasound travels out, hits object, returns
  • Step 4pulseIn(ECHO) captures echo duration in µs
  • Step 5 — Distance calculated and printed with angle to Serial
01

Servo sweeps 0° → 180°

The for loop increments the servo angle by 1° each iteration, then waits 15ms for the servo to physically reach the position before taking a reading.

02

Ultrasonic ping fired

TRIG is pulled LOW (2µs), then HIGH (10µs), then LOW again. This generates a precise 40kHz ultrasonic burst from the HC-SR04's transmitter.

03

Echo timed with pulseIn()

pulseIn(echoPin, HIGH) returns the microsecond duration of the echo pulse — the time taken for the sound wave to reach an object and return.

04

Distance calculated

Distance (cm) = duration × 0.0343 ÷ 2. The ÷2 accounts for the round trip — the pulse travels to the object and back, so only half the total time equals the one-way distance.

05

Angle + Distance printed to Serial Monitor

The Arduino streams "Angle: X | Distance: Y cm" at 9600 baud for every degree, creating a complete map of the scanned environment.

Distance Formula

distance (cm) = duration (µs) × 0.0343 ÷ 2
Speed of sound ≈ 0.0343 cm/µs at 20°C  |  ÷2 because sound travels TO object AND back

Components Required

ComponentQtyPurpose
Arduino UNO (R3)×1Main controller — runs servo sweep and distance logic
HC-SR04 Ultrasonic Sensor×1Measures distance via sound echo (2–400 cm range)
SG90 Servo Motor×1Rotates the HC-SR04 through 0–180°
Breadboard×1Prototyping power rails for clean wiring
Jumper Wires (M-M, M-F)×15+All signal and power connections
USB Cable / 5V Power Supply×1Powers Arduino and servo
📦
Only one library needed: Servo.h is built into the Arduino IDE — no extra installation required. The HC-SR04 is read using built-in pulseIn() — no library needed.


Wiring Diagram

radar_arduino.json — Circuit Overview
RADAR System — Wiring Overview (Arduino UNO + HC-SR04 + SG90) ARDUINO UNO 5V → HC-SR04 VCC & Servo Red GND → HC-SR04 GND & Servo Brown D9 → HC-SR04 TRIG D10 ← HC-SR04 ECHO D6 → SG90 Orange (Signal) HC-SR04 Ultrasonic Sensor TRIG ECHO VCC → Arduino 5V GND → Arduino GND TRIG ← Arduino D9 ECHO → Arduino D10 SG90 SERVO MOTOR Mounts HC-SR04 for 0–180° scan SG90 Red → Arduino 5V Brown → Arduino GND Orange → Arduino D6 SERIAL MONITOR 9600 baud Angle: 0 | Distance: 45 cm Angle: 15 | Distance: 38 cm Angle: 30 | Distance: 25 cm Angle: 45 | Distance: 8 cm Angle: 60 | Distance: 18 cm Angle: 90 | Distance: 10 cm Angle:120 | Distance: 33 cm Angle:180 | Distance: 52 cm USB PROCESSING IDE (optional graphical display) 5V GND TRIG (D9) ECHO (D10) Servo Signal (D6)

Pin Connection Map

HC-SR04 Ultrasonic

VCC→ Arduino 5V
GND→ Arduino GND
TRIG→ Arduino D9
ECHO→ Arduino D10

SG90 Servo Motor

Red→ Arduino 5V
Brown / Black→ Arduino GND
Orange / Yellow→ Arduino D6 (Signal)

diagram.json (Wokwi)

📁 How to use: Go to wokwi.com, click the diagram.json tab and replace all contents with the JSON below. Create a sketch.ino file and paste the code from the next section. Press ▶ to run.
diagram.json
{
  "version": 1,
  "author": "MakeMindz",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-arduino-uno",
      "id": "uno",
      "top": 130,
      "left": 295,
      "attrs": {}
    },
    {
      "type": "wokwi-hc-sr04",
      "id": "sonar",
      "top": 80,
      "left": 30,
      "attrs": { "distance": "25" }
    },
    {
      "type": "wokwi-servo",
      "id": "servo1",
      "top": 280,
      "left": 30,
      "attrs": { "angle": "90", "horn": "single" }
    }
  ],
  "connections": [
    ["sonar:VCC",  "uno:5V",   "red",    []],
    ["sonar:GND",  "uno:GND",  "black",  []],
    ["sonar:TRIG", "uno:9",    "orange", []],
    ["sonar:ECHO", "uno:10",   "purple", []],
    ["servo1:V+",  "uno:5V",   "red",    []],
    ["servo1:GND", "uno:GND",  "black",  []],
    ["servo1:PWM", "uno:6",    "orange", []]
  ]
}
💡
Wokwi tip: Click the HC-SR04 in the simulation and drag its Distance slider to simulate objects at different ranges. Watch the Serial Monitor update in real time as the servo sweeps and distance changes.

Step-by-Step Instructions

1

Mount HC-SR04 on Servo Horn

Fix the HC-SR04 sensor onto the SG90 servo horn using double-sided tape or a cable tie. Ensure both transmitter (T) and receiver (R) cylinders face forward and the sensor is centred on the horn so it rotates evenly.

2

Wire the HC-SR04 Sensor

  • VCC → Arduino 5V
  • GND → Arduino GND
  • TRIG → Arduino D9
  • ECHO → Arduino D10

Use male-to-female jumper wires from the sensor to the Arduino headers directly, or via a breadboard power rail.

3

Wire the SG90 Servo Motor

  • Red wire → Arduino 5V
  • Brown / Black wire → Arduino GND
  • Orange / Yellow signal wire → Arduino D6
⚠️
Powering the servo directly from the Arduino 5V pin works for a single SG90 in simulation. For real hardware with multiple servos or longer sweep times, use an external 5V supply shared with the Arduino GND to avoid voltage drops.
4

Manage the Rotating Wires

Route the HC-SR04 wires loosely alongside the servo so they don't pull tight at 0° or 180°. Coil any extra wire and secure with a small cable tie — leave enough slack for full 180° rotation without snagging.

5

Open Arduino IDE and Include Servo Library

Download the Arduino IDE from arduino.cc. The Servo.h library is built in — no extra installation needed. Select Board → Arduino UNO and your USB port.

6

Upload the Sketch

Copy the full code from the section below, paste it into a new Arduino IDE sketch, and click the Upload arrow. Wait for "Done uploading" before opening the Serial Monitor.

7

Open Serial Monitor & Observe

Go to Tools → Serial Monitor. Set baud rate to 9600. You should see lines like Angle: 45 | Distance: 22 cm streaming as the servo sweeps. Place objects at different distances and angles to see readings change.

8

(Optional) Add Processing IDE Graphical Display

Download the Processing sketch and run it alongside the Arduino sketch. It reads the Serial port and renders a live green-on-black RADAR sweep with object blips — just like a real RADAR display.

Full Source Code

radar_system.ino
// ─────────────────────────────────────────────────────
// RADAR System — Arduino UNO + HC-SR04 + SG90 Servo
// MakeMindz.com | roboticsandcircuits.blogspot.com
// Sweeps 0–180°, measures distance at each angle,
// streams Angle + Distance to Serial Monitor (9600 baud)
// ─────────────────────────────────────────────────────

#include <Servo.h>

// ── Pin Definitions ──────────────────────────────────
#define trigPin   9    // HC-SR04 trigger output
#define echoPin  10   // HC-SR04 echo input
#define servoPin  6   // SG90 PWM signal

// ── Object Declaration ───────────────────────────────
Servo radarServo;

// ── Global Variables ─────────────────────────────────
long duration;    // Echo pulse duration in microseconds
int  distance;    // Calculated distance in cm

// ── setup() ──────────────────────────────────────────
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  radarServo.attach(servoPin);

  Serial.println("RADAR System — MakeMindz.com");
  Serial.println("──────────────────────────────");
}

// ── loop() ───────────────────────────────────────────
// Sweeps servo 0° → 180° taking a distance reading every 1°
void loop() {

  for (int angle = 0; angle <= 180; angle++) {

    // Step 1: Rotate servo to current angle
    radarServo.write(angle);
    delay(15);              // Wait for servo to reach position

    // Step 2: Send ultrasonic pulse
    //   Pull TRIG low first to ensure clean pulse
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);

    // HIGH pulse for exactly 10µs fires the sonic burst
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Step 3: Measure echo duration
    duration = pulseIn(echoPin, HIGH);

    // Step 4: Calculate distance
    //   distance = duration × speed_of_sound ÷ 2
    //   speed of sound = 0.0343 cm/µs at 20°C
    distance = duration * 0.0343 / 2;

    // Step 5: Print angle and distance to Serial Monitor
    Serial.print("Angle: ");
    Serial.print(angle);
    Serial.print(" | Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
  }

  // After full 180° sweep, loop() restarts automatically
  // Add a reverse sweep here if you want a back-and-forth motion:
  // for (int angle = 180; angle >= 0; angle--) { ... }
}

Example Serial Monitor Output

// Serial Monitor — 9600 baud
RADAR System — MakeMindz.com
──────────────────────────────
Angle: 30 | Distance: 25 cm
Angle: 60 | Distance: 18 cm
Angle: 90 | Distance: 10 cm ← close object detected
Angle: 120 | Distance: 33 cm
Angle: 150 | Distance: 47 cm
Angle: 180 | Distance: 52 cm

Test Without Hardware

🧪

Live simulation — pre-built on Tinkercad

The complete RADAR circuit is already set up on Tinkercad. Click the Tinkercad link below to open it, press Simulate, open the Serial Monitor, and watch angle + distance data stream live. Drag the HC-SR04 distance slider to place virtual objects.

Tinkercad advantage: This simulation is pre-wired — just click Simulate and Start Simulation. No setup required. Use it to test your understanding of the code before building the physical circuit.

What You'll Learn

📏

Ultrasonic Distance Measurement

Understand how sound pulses, echo timing, and the pulseIn() function work together.

🔄

Servo Motor Control

Use the Servo library to precisely position the SG90 at any angle between 0° and 180°.

📡

Angle-Based Scanning

Learn how iterating servo angles creates a spatial map of objects around the sensor.

🖥️

Serial Communication

Stream structured data from Arduino to the Serial Monitor for real-time analysis.

🧮

Physics — Speed of Sound

Apply the distance = speed × time formula and understand why we divide by 2.

🤖

Sensor + Actuator Integration

Combine an input sensor and output actuator into a single coordinated embedded system.

Advanced Improvements

🖥️
Processing IDE graphical RADAR display
📺
OLED display for live distance output
🔔
Buzzer alert when object is within 20 cm
💡
LED bar graph for proximity indication
💾
SD card data logging with timestamps
🔄
360° continuous rotation servo scanning

Applications

🤖

Obstacle Detection

Foundation for autonomous robot navigation — detect and map surrounding obstacles.

🚗

Parking Sensor Prototype

Scan a parking area and alert when a vehicle enters the detection zone.

🔭

Surveillance Simulation

Sweep-scan a room boundary and log any object positions detected during the scan.

🏫

STEM Exhibition

Impressive science fair project that demonstrates real engineering radar concepts.

Explore More Projects

© 2026 MakeMindz — Arduino, IoT & Robotics Tutorials

Built with ❤️ for makers, students, and engineers.

Comments

try for free