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

Arduino UNO-Based Distance Detection with Buzzer Alert

Image of Arduino UNO-Based Distance Detection with Buzzer Alert

Circuit Documentation

Summary of the Circuit

This circuit integrates an HC-SR04 Ultrasonic Sensor with an Arduino UNO microcontroller and a buzzer. The primary function of this circuit is to measure the distance to an object using the ultrasonic sensor and to provide an audible alert when the object is within a certain distance threshold. The Arduino UNO is programmed to trigger the HC-SR04 to send out an ultrasonic pulse and then listen for the echo. The time taken for the echo to return is used to calculate the distance to the object. If this distance is less than 10 centimeters, the buzzer is activated.

Component List

HC-SR04 Ultrasonic Sensor

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

Buzzer

  • Description: An electromechanical component that emits a tone when an electrical signal is applied.
  • Pins: PIN, GND

Arduino UNO

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

Wiring Details

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to the 5V output of the Arduino UNO.
  • TRIG: Connected to digital pin D7 of the Arduino UNO.
  • ECHO: Connected to digital pin D6 of the Arduino UNO.
  • GND: Connected to one of the GND pins of the Arduino UNO.

Buzzer

  • PIN: Connected to digital pin D12 of the Arduino UNO.
  • GND: Connected to one of the GND pins of the Arduino UNO.

Arduino UNO

  • 5V: Provides power to the HC-SR04 Ultrasonic Sensor.
  • GND: Common ground used by both the HC-SR04 Ultrasonic Sensor and the buzzer.
  • D7: Sends trigger pulses to the HC-SR04 Ultrasonic Sensor.
  • D6: Receives echo pulses from the HC-SR04 Ultrasonic Sensor.
  • D12: Controls the buzzer.

Documented Code

/*
 * Arduino UNO R3 with HC-SR04 Ultrasonic Sensor and Buzzer
 * This code measures the distance using the HC-SR04 sensor and activates the
 * buzzer if the distance is below a certain threshold.
 */

#define TRIG_PIN 7
#define ECHO_PIN 6
#define BUZZER_PIN 12

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

void loop() {
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance < 10) {
    digitalWrite(BUZZER_PIN, HIGH);
  } else {
    digitalWrite(BUZZER_PIN, LOW);
  }
  delay(500);
}

The code is written for the Arduino UNO and is responsible for controlling the HC-SR04 Ultrasonic Sensor and the buzzer. The TRIG_PIN and ECHO_PIN constants are defined to correspond to the digital pins D7 and D6 on the Arduino, respectively, which are connected to the TRIG and ECHO pins of the HC-SR04. The BUZZER_PIN is set to D12, which is connected to the buzzer. The setup() function initializes these pins and starts serial communication. The loop() function triggers the ultrasonic sensor, calculates the distance, and activates the buzzer if the distance is below 10 centimeters.