BeginnerArduinoArduino UnoHC-SR04Ultrasonic

Ultrasonic Parking Sensor with Arduino Uno

Wire an HC-SR04 ultrasonic module to an Arduino Uno R3 and drive a buzzer that beeps faster as obstacles get closer. Ideal first project — no libraries needed, pure digitalRead/tone logic.

Circuit Hub14 min read1 views

What You'll Build

A proximity alarm that mimics the parking sensors built into modern cars. The HC-SR04 emits an ultrasonic pulse and measures the echo time to calculate distance. Your Arduino maps that distance to a buzzer tone — slow beeps at 50 cm, rapid beeps at 15 cm, and a solid tone inside 5 cm.

The whole circuit needs no libraries and uses just six wires. It's a reliable first project that teaches pulse timing, pulseIn(), and analog-to-digital mapping in about 40 lines of C++.

💡 Tip

The HC-SR04 runs on 5V and its ECHO pin outputs 5V logic. The Arduino Uno's digital pins are 5V-tolerant, so you can connect ECHO directly. On a 3.3V board (Nano 33, Pico) you'd need a voltage divider.

Components required

Arduino Uno R3
×1Buy
HC-SR04 Ultrasonic Sensor
×1Buy
5V Active Buzzer
×1
Red LED (3 mm or 5 mm)
×1
220 Ω Resistor
×1
Full-size Breadboard
×1
Jumper Wires (M-M)
×8
USB Type-A to B Cable
×1

Wiring

Keep the connections simple:

| HC-SR04 Pin | Arduino Pin | |-------------|-------------| | VCC | 5V | | TRIG | D9 | | ECHO | D10 | | GND | GND |

Buzzer + → D8. LED anode → D7 through a 220 Ω resistor to GND.

C++
// ── Ultrasonic Parking Sensor ─────────────────────────────────────────────────
// Arduino Uno R3  |  HC-SR04  |  Active Buzzer  |  Red LED
// ─────────────────────────────────────────────────────────────────────────────

#define TRIG_PIN   9
#define ECHO_PIN  10
#define BUZZER     8
#define LED        7

// Distance thresholds in centimetres
#define DANGER_CM   5
#define CLOSE_CM   15
#define WARN_CM    30
#define MAX_CM     50

// Returns distance in cm (returns 999 on timeout)
float readDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 25000UL); // 25 ms timeout ≈ 430 cm max
  if (duration == 0) return 999;
  return duration * 0.0343f / 2.0f;
}

void beep(int onMs, int offMs) {
  tone(BUZZER, 2400, onMs);
  delay(onMs + offMs);
}

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER,   OUTPUT);
  pinMode(LED,      OUTPUT);
  Serial.begin(9600);
}

void loop() {
  float dist = readDistance();
  Serial.print("Distance: ");
  Serial.print(dist, 1);
  Serial.println(" cm");

  if (dist >= MAX_CM) {
    // Clear — silent
    noTone(BUZZER);
    digitalWrite(LED, LOW);

  } else if (dist >= WARN_CM) {
    // 50–30 cm: slow beep every 700 ms
    beep(80, 620);
    digitalWrite(LED, LOW);

  } else if (dist >= CLOSE_CM) {
    // 30–15 cm: medium beep every 350 ms
    beep(100, 250);
    digitalWrite(LED, HIGH);

  } else if (dist >= DANGER_CM) {
    // 15–5 cm: rapid beep every 120 ms
    beep(80, 40);
    digitalWrite(LED, HIGH);

  } else {
    // < 5 cm: solid alarm
    tone(BUZZER, 3200);
    digitalWrite(LED, HIGH);
    delay(50);
  }
}

Live simulation

Steps

  1. 1Assemble the circuit on a breadboard following the wiring table above
  2. 2Open Arduino IDE 2, select board: Arduino Uno, and the correct COM port
  3. 3Paste the sketch and click Upload
  4. 4Open Serial Monitor at 9600 baud to watch live distance readings
  5. 5Move your hand toward the sensor — the buzzer pattern should change at each threshold
  6. 6Tweak WARN_CM / CLOSE_CM defines to match your use case

Going Further

  • Add a 16×2 I2C LCD to display the numeric distance instead of (or alongside) the Serial Monitor
  • Replace the active buzzer with a passive one and use the tone() frequency to create a musical scale — lower pitch for far, higher for near
  • Mount the sensor in a 3D-printed bracket and power from a 9V battery for a portable device
  • Combine two HC-SR04s (front and rear) on D9/D10 and D6/D7 for a dual-axis parking assistant

Related projects