Ultrasonic Distance Sensor Alarm
Create a proximity alarm system using an ultrasonic sensor. Features adjustable sensitivity and both visual and audio alerts.

Project Overview
Build a sophisticated proximity alarm system using ultrasonic technology! This project teaches you how to work with sensors, process analog data, and create responsive alert systems.
The HC-SR04 ultrasonic sensor works like sonar, sending out sound waves and measuring the time it takes for them to bounce back. You'll learn how to convert this timing data into distance measurements.
This project includes both visual (LED) and audio (buzzer) alerts, with adjustable sensitivity using a potentiometer. It's perfect for security applications, parking sensors, or robotics projects.
What You'll Learn
- Understanding digital pin outputs
- Using delay() functions for timing
- Creating loops and sequences
- Basic circuit building
- Arduino IDE fundamentals
- Troubleshooting common issues
- Analog sensor reading and processing
- Sensor calibration techniques
- Data validation and error handling
- Serial monitor debugging
Key Features
Step-by-Step Instructions
Wire the Ultrasonic Sensor
Connect VCC to 5V, GND to ground, Trig to pin 7, and Echo to pin 6. The sensor needs clean power connections.

Add Alarm Components
Connect the buzzer to pin 8, LED to pin 13 with a resistor, and potentiometer to A0 for sensitivity adjustment.
const int trigPin = 7; // Trig pin of the ultrasonic sensor
const int echoPin = 8; // Echo pin of the ultrasonic sensor
const int ledPin = 13; // Pin for the LED indicator
const int delayTime = 100;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}
Write Distance Measurement Code
Create a function to measure distance using the ultrasonic sensor timing method.
void loop() {
float duration;
float distance;
// Trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo signal
duration = pulseIn(echoPin, HIGH);
// Convert duration to distance
distance = (duration * 0.0343 ) / 2;
// LED indicator for close objects
if (distance < 5) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Print results to the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Delay for stability
delay(100);
}