Ultrasonic sensor alarm system
This is a practical project that you can use as an alarm system. In the images above, the circuit is not clearly visible, but it is the same as in the schematics. The only difference is that I used an Arduino Nano.
When the sensor detects the presence of an object within a range of about 80 cm, the alarm starts to sound. The system also has an ON/OFF button. You can tell when the system is active by the red LED. If the LED is on, the system is running.
Required components:
-
Arduino Nano board (Arduino UNO or any compatible Arduino board also works)
-
HC-SR04 ultrasonic sensor
-
Active buzzer
-
Push-button
-
LED (or you can use the built-in LED on pin 13, but in the code below I used D8)
-
220Ω resistor (only if you use an external LED)
-
Jumper wires
-
Breadboard (optional, recommended for testing and organizing components)
Connections:
Active buzzer
-
Positive pin (+) → D2
-
Negative pin (−) → GND
HC-SR04 sensor
-
VCC → 5V
-
GND → GND
-
TRIG → D4
-
ECHO → D6
ON/OFF button
-
One pin → D9
-
Other pin → GND
LED
-
Anode (+) → D12
-
Cathode (−) → GND through a 220Ω resistor
Code:
const int buzzerPin = 2;
const int trigPin = 4;
const int echoPin = 6;
const int butonPin = 9;
const int ledPin = 12;
const long alarmDuration = 30000;
const int detectionRange = 80;
// FILTRARE – OPTIMIZAT
const int samples = 3;
const int confirmReads = 2;
const int maxJump = 35;
bool alarmOn = false;
bool systemOn = true;
unsigned long alarmStartTime = 0;
unsigned long lastBeepTime = 0;
bool buzzerState = false;
const int beepOn = 400;
const int beepOff = 300;
int lastButtonState = HIGH;
int detectCount = 0;
long lastDistance = -1;
// ================= DISTANȚĂ =================
long readDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 25000);
if (duration == 0) return -1;
return duration * 0.034 / 2;
}
// ================= FILTRAT =================
long readDistanceFiltered() {
long sum = 0;
int valid = 0;
for (int i = 0; i < samples; i++) {
long d = readDistance();
if (d > 5 && d < 250) {
sum += d;
valid++;
}
delay(3);
}
if (valid == 0) return -1;
return sum / valid;
}
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(butonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(9600);
}
void loop() {
// ===== BUTON =====
int reading = digitalRead(butonPin);
if (reading == LOW && lastButtonState == HIGH) {
systemOn = !systemOn;
alarmOn = false;
detectCount = 0;
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, systemOn ? HIGH : LOW);
delay(200);
}
lastButtonState = reading;
if (!systemOn) return;
// ===== CITIRE =====
long dist = readDistanceFiltered();
Serial.println(dist);
if (dist < 0) {
detectCount = 0;
} else {
if (lastDistance > 0 && abs(dist - lastDistance) > maxJump) {
detectCount = 0;
} else {
if (dist <= detectionRange) {
detectCount++;
if (dist <= 40) detectCount++; // BOOST RAPID
} else {
detectCount = 0;
}
}
lastDistance = dist;
}
// ===== PORNIRE ALARMĂ =====
if (!alarmOn && detectCount >= confirmReads) {
alarmOn = true;
alarmStartTime = millis();
buzzerState = true;
digitalWrite(buzzerPin, HIGH);
lastBeepTime = millis();
}
// ===== ALARMĂ =====
if (alarmOn) {
if (millis() - alarmStartTime >= alarmDuration) {
alarmOn = false;
digitalWrite(buzzerPin, LOW);
} else {
if (buzzerState && millis() - lastBeepTime >= beepOn) {
buzzerState = false;
digitalWrite(buzzerPin, LOW);
lastBeepTime = millis();
}
else if (!buzzerState && millis() - lastBeepTime >= beepOff) {
buzzerState = true;
digitalWrite(buzzerPin, HIGH);
lastBeepTime = millis();
}
}
}
}
