Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Based Ultrasonic Distance Sensor with Buzzer Alert

Image of Arduino UNO Based Ultrasonic Distance Sensor with Buzzer Alert

Circuit Documentation

Summary

This circuit integrates an HC-SR04 Ultrasonic Sensor with an Arduino UNO microcontroller and a buzzer, powered by a 5V battery. The primary function of this circuit is to measure distance using the ultrasonic sensor and provide an audible alert through the buzzer when an object is detected within a certain range.

Component List

HC-SR04 Ultrasonic Sensor

  • Description: A sensor that measures distance by emitting ultrasonic waves and detecting the time taken for the echo to return.
  • Pins: VCC, TRIG, ECHO, GND

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P, widely used for building digital devices and interactive objects.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13

5V Battery

  • Description: A power source providing a stable 5V supply to the circuit.
  • Pins: +, -

Buzzer

  • Description: An electromechanical component that produces sound when an electrical signal is applied.
  • Pins: PIN, GND

Wiring Details

HC-SR04 Ultrasonic Sensor

  • VCC connected to 5V of Arduino UNO and + of 5V Battery
  • TRIG connected to D9 of Arduino UNO
  • ECHO connected to D8 of Arduino UNO
  • GND connected to GND of Arduino UNO, - of 5V Battery, and GND of Buzzer

Arduino UNO

  • 5V connected to VCC of HC-SR04 Ultrasonic Sensor
  • GND connected to GND of HC-SR04 Ultrasonic Sensor, - of 5V Battery, and GND of Buzzer
  • D9 connected to TRIG of HC-SR04 Ultrasonic Sensor
  • D8 connected to ECHO of HC-SR04 Ultrasonic Sensor
  • D13 connected to PIN of Buzzer

5V Battery

    • connected to VCC of HC-SR04 Ultrasonic Sensor
    • connected to GND of HC-SR04 Ultrasonic Sensor, GND of Arduino UNO, and GND of Buzzer

Buzzer

  • PIN connected to D13 of Arduino UNO
  • GND connected to GND of Arduino UNO, - of 5V Battery, and GND of HC-SR04 Ultrasonic Sensor

Documented Code

Arduino UNO Code

#define trigPin 9           // Trig Pin Of HC-SR04
#define echoPin 8           // Echo Pin Of HC-SR04
#define BUZ 13              // BUZZER
long duration, distance;

void setup() 
{
   pinMode(BUZ, OUTPUT);
   pinMode(trigPin, OUTPUT);       // Set Trig Pin As O/P To Transmit Waves
   pinMode(echoPin, INPUT);        // Set Echo Pin As I/P To Receive Reflected Waves
}

void loop() 
{
  Serial.begin(9600);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);       // Transmit Waves For 10us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH);        // Receive Reflected Waves
  distance = duration / 58.2;               // Calculate Distance
  Serial.println(distance);
  delay(10);
  if (distance < 15)                        // Condition For Object Detection
  {
    // Turn BUZZER ON & OFF 5 times:
    for (int i = 0; i < 5; i++)
    {
      digitalWrite(BUZ, HIGH);
      delay(50);
      digitalWrite(BUZ, LOW);
      delay(50);                                                 
    }
  }
}

This code configures the Arduino UNO to control the HC-SR04 Ultrasonic Sensor and the buzzer. It measures the distance to an object using the ultrasonic sensor and activates the buzzer if the object is closer than 15 cm. The buzzer beeps five times to alert the presence of the object.