This content originally appeared on DEV Community and was authored by Elmer Urbina
Introduction
In this hands-on project, we successfully designed and built a smart object detection alarm using an Arduino UNO and an HC-SR04 ultrasonic sensor. The system detects objects within a predefined range and triggers an audible alert via a buzzer. This project not only demonstrates the use of ultrasonic technology for proximity sensing but also validates the functionality of new hardware components after troubleshooting earlier failures.
Project Overview
- Objective: Create a reliable object detection system using ultrasonic waves.
- Core Components: Arduino UNO, HC-SR04 sensor, active/passive buzzer, and jumper wires.
- Applications: Parking assistance, home security, robotics, and industrial measurement.
Methodology & Components
We used an ultrasonic detection methodology combined with real-time audio feedback.
Hardware Components:
Circuit Diagram
Below is the wiring setup used in the project:
Arduino Code
Here’s the complete code uploaded to the Arduino IDE:
` #define trig 7 // Trigger pin for HC-SR04
#define echo 6 // Echo pin for HC-SR04
#define buzzer 12 // Buzzer control pin
void setup() {
// Define pin modes
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long duration, distance;
// Clear the trigger pin
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Send a 10µs pulse to trigger
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Read the echo pin and calculate pulse duration
duration = pulseIn(echo, HIGH);
// Calculate distance in cm
distance = (duration / 2) * 0.0343;
// If an object is detected within 10 cm, activate the buzzer
if (distance < 10) {
tone(buzzer, 4000); // Emit 4000 Hz tone
delay(1000);
} else {
noTone(buzzer); // Turn off buzzer
}
}`
Key Functionalities Explained
Ultrasonic Detection: The HC-SR04 emits an ultrasonic pulse and measures the time taken for the echo to return.
Distance Calculation: Distance is calculated using the formula:
Distance = (Duration / 2) * 0.0343
where 0.0343 is the speed of sound in centimeters per microsecond.
Alarm Trigger: If an object is detected within 10 cm, the buzzer emits a 4000 Hz tone for 1 second.
Continuous Monitoring: The system runs in a loop, providing real-time detection.
Real-World Applications
Parking Sensors: Warn drivers of nearby obstacles.
Home Security: Detect intrusions near doors or windows.
Robotics: Enable obstacle avoidance in autonomous robots.
Industrial Use: Monitor fill levels in tanks or containers.
Conclusion
This smart alarm system effectively demonstrates the use of ultrasonic sensors for object detection. It confirmed that earlier hardware issues were due to faulty components and not coding errors. The system is reliable, scalable, and ready for enhancements like wireless communication or multi-sensor integration.
This content originally appeared on DEV Community and was authored by Elmer Urbina

