Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Based Ultrasonic Distance Measurement with Buzzer Alert

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Ultrasonic Distance Measurement with Buzzer Alert

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller with two HC-SR04 Ultrasonic Sensors and a Buzzer. The purpose of the circuit is to measure distances using the ultrasonic sensors and provide an audible alert through the buzzer when a certain distance threshold is met. The Arduino UNO is programmed to control the sensors and the buzzer, and it outputs the measured distance to the serial monitor.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor (x2)

Buzzer

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Buzzer

Documented Code

// Define the pins for the buzzer and ultrasonic sensors
const int buzzer = 8;
const int trig_pin = 9;
const int echo_pin = 10;

// Variables to store the timing and distance
float timing = 10;
float distance = 20;

void setup() {
  // Initialize the sensor pins
  pinMode(echo_pin, INPUT);
  pinMode(trig_pin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  // Ensure the trigger pin is set low
  digitalWrite(trig_pin, LOW);
  digitalWrite(buzzer, LOW);
    
  // Start the serial communication
  Serial.begin(9600);
}

void loop() {
  // Trigger the ultrasonic pulse
  digitalWrite(trig_pin, LOW);
  delay(2);
  
  digitalWrite(trig_pin, HIGH);
  delay(10);
  digitalWrite(trig_pin, LOW);
  
  // Read the echo pin and calculate the distance
  timing = pulseIn(echo_pin, HIGH);
  distance = (timing * 0.034) / 2;
  
  // Print the distance in cm and inches
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print("cm | ");
  Serial.print(distance / 2.54);
  Serial.println("in");
  
  // If the distance is less than or equal to 50 cm, sound the buzzer
  if (distance <= 50) {
    tone(buzzer, 600);
  } else {
    noTone(buzzer);
  }
  
  // Wait before the next loop iteration
  delay(100);
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It sets up the ultrasonic sensors and the buzzer, then continuously measures the distance and sounds the buzzer if the distance is less than or equal to 50 cm. The measured distance is also printed to the serial monitor in both centimeters and inches.