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

Arduino UNO Based Ultrasonic Distance Sensor with Alert System

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Ultrasonic Distance Sensor with Alert System

Circuit Documentation

Summary

This circuit is designed to utilize an Arduino UNO microcontroller to interface with an HC-SR04 Ultrasonic Sensor, a Piezo Buzzer, and an LED. The primary function of this circuit is to measure distance using the ultrasonic sensor and provide an alert through the buzzer and LED when an object is within a predefined safety distance. The Arduino UNO controls the sensor to emit ultrasonic waves and listens for the echo to calculate the distance to an object. If the measured distance is within the safety threshold, the buzzer is activated, and the LED is turned on to signal proximity.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor

Piezo Buzzer

LED: Two Pin (white)

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Piezo Buzzer

LED: Two Pin (white)

Documented Code

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buzzer, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
  safetyDistance = distance;
  if (safetyDistance <= 5){
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, LOW);
  }
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
}

This code is designed to run on the Arduino UNO microcontroller. It initializes the pins connected to the ultrasonic sensor, buzzer, and LED. In the main loop, it triggers the ultrasonic sensor to send a pulse and listens for the echo to calculate the distance to an object. If the distance is less than or equal to 5 units (presumably centimeters), it activates the buzzer and turns on the LED as an alert. The distance is also printed to the serial monitor for debugging purposes.