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.
#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.