CI

Reverse Parking system using Ultrasonic sensor and Arduino Uno

Description

In this project, we will build an Arduino-based Reverse Parking System using an HC-SR04 ultrasonic sensor and a buzzer. The system detects the distance of an object and generates sound alerts based on how close the object is.

The ultrasonic sensor continuously measures the distance of nearby objects. Arduino processes the distance readings and controls the buzzer using different alert patterns. When an object comes closer, the buzzer beeps faster, and when the object is very close, it produces a continuous sound. This project is useful for understanding ultrasonic sensors, distance measurement, and buzzer control using Arduino UNO.

Components Required

ComponentQuantity
Arduino UNO1
HC-SR04 Ultrasonic Sensor1
Buzzer1
Breadboard1
Jumper WiresAs required

Circuit Diagram

Connect the HC-SR04 ultrasonic sensor and buzzer to the Arduino UNO according to the circuit diagram and connection tables below.

Arduino Ultrasonic Distance Based Buzzer Alert System Circuit Diagram
Download Circuit Diagram

Pin Connection Table

HC-SR04 Ultrasonic Sensor → Arduino UNO

Ultrasonic Sensor PinArduino UNO Pin
VCC5V
TRIGD9
ECHOD10
GNDGND

Buzzer → Arduino UNO

Buzzer PinArduino UNO Pin
Positive (+)D8
Negative (-)GND

Working Principle

The HC-SR04 ultrasonic sensor sends ultrasonic waves towards an object and measures the time taken for the echo to return. Arduino calculates the distance using this time and controls the buzzer according to four different distance zones.

When the object is within 10 cm, the buzzer produces a continuous sound. Between 10 and 20 cm, it beeps rapidly at 100 ms intervals. Between 20 and 30 cm, it beeps at 250 ms intervals, while between 30 and 40 cm, it produces slower beeps at 500 ms intervals. When the object is more than 40 cm away, the buzzer remains OFF.

Distance-Based Alert Zones

Distance Buzzer Response
0–10 cm Continuous Sound
10–20 cm Fast Beeping (100 ms)
20–30 cm Medium Beeping (250 ms)
30–40 cm Slow Beeping (500 ms)
More than 40 cm Buzzer OFF

Arduino Code

// Creative Inventions
// Subscribe to my YouTube channel

// Ultrasonic Sensor Pins
const int trigPin = 9;
const int echoPin = 10;

// Buzzer Pin
const int buzzerPin = 8;

float distance;
unsigned long duration;

// Timing Variables
unsigned long previousMeasureTime = 0;
unsigned long previousBeepTime = 0;

const unsigned long measureInterval = 60;

bool buzzerState = false;

void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  Serial.begin(9600);
}

void loop() {

  // Measure distance at regular intervals
  if (millis() - previousMeasureTime >= measureInterval) {

    previousMeasureTime = millis();

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);

    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);

    digitalWrite(trigPin, LOW);

    // Read ultrasonic echo duration
    duration = pulseIn(echoPin, HIGH, 30000UL);

    if (duration == 0) {
      distance = 999;
    } else {
      distance = duration * 0.0343 / 2.0;
    }

    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
  }

  unsigned long currentTime = millis();

  // Zone 1: Very close (0-10 cm)
  if (distance <= 10) {

    tone(buzzerPin, 2000);
    buzzerState = true;
  }

  // Zone 2: 10-20 cm (Fast Beeping)
  else if (distance <= 20) {

    if (currentTime - previousBeepTime >= 100) {

      previousBeepTime = currentTime;
      buzzerState = !buzzerState;

      if (buzzerState) {
        tone(buzzerPin, 2000);
      } else {
        noTone(buzzerPin);
      }
    }
  }

  // Zone 3: 20-30 cm (Medium Beeping)
  else if (distance <= 30) {

    if (currentTime - previousBeepTime >= 250) {

      previousBeepTime = currentTime;
      buzzerState = !buzzerState;

      if (buzzerState) {
        tone(buzzerPin, 2000);
      } else {
        noTone(buzzerPin);
      }
    }
  }

  // Zone 4: 30-40 cm (Slow Beeping)
  else if (distance <= 40) {

    if (currentTime - previousBeepTime >= 500) {

      previousBeepTime = currentTime;
      buzzerState = !buzzerState;

      if (buzzerState) {
        tone(buzzerPin, 2000);
      } else {
        noTone(buzzerPin);
      }
    }
  }

  // More than 40 cm: Buzzer OFF
  else {

    noTone(buzzerPin);
    buzzerState = false;
  }
}