Raya Tabassum: Unusual Switch Project

Concept & Usage: My idea was to build a distance-sensitive security system. The system is designed to be sensitive to distance, using the HC-SR04 ultrasonic sensor paired with an Arduino board to trigger an audible alarm. The closer the object is to the sensor, the faster the alarm—or buzzer—will beep, which serves as an intuitive indicator of proximity.

The ultrasonic sensor-based alarm acts not only as a simple security device but can also be an interactive element for various applications. By emitting ultrasonic waves, the sensor can determine the distance to an object based on the time it takes for the waves to return. In the context of security, this setup can detect potential intruders or unauthorized access by sensing motion within a predefined range.

The versatility of the alarm allows it to be adapted for different purposes, such as:

  • Intruder detection for personal properties, alerting homeowners to possible trespassers
  • Industrial safety, where restricted areas can be monitored to ensure personnel safety
  • Public events, to manage crowd control and prevent attendees from accessing sensitive areas

Implementation:

When the system senses an object within a specified distance (less than 50 cm in your code), it activates the buzzer. The alarm’s operation is controlled by the code which defines the pins for the sensor and the buzzer, measures the distance by calculating the time interval of the echo’s return, and dynamically adjusts the beeping speed based on this distance.

Code:

//Security Alarm with Ultrasonic Sensor//

#define trigPin 6  
#define echoPin 5
#define buzzer 2
float new_delay; 


void setup() 
{
  Serial.begin (9600); 
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
  pinMode(buzzer,OUTPUT);
  
}


void loop() 
{
  long duration, distance;
  digitalWrite(trigPin, LOW);        
  delayMicroseconds(2);              
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);           
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  new_delay= (distance *3) +30;
  Serial.print(distance);
  Serial.println("  cm");
  if (distance < 50)
  {
   digitalWrite(buzzer,HIGH);
   delay(new_delay);
   digitalWrite(buzzer,LOW);
 
  }
  else
  {
    digitalWrite(buzzer,LOW);

  }
  
 delay(200);
}
  • trigPin and echoPin are defined for sending and receiving the ultrasonic signals.
  • buzzer is defined for the alarm sound.
  • In the loop function, the program triggers the ultrasonic sensor and listens for the echo. The distance is calculated based on the time it takes for the echo to return.
  • The new_delay is calculated based on the measured distance. The closer the object, the smaller the delay, resulting in a faster beep.
  • The buzzer is activated if the object is within 50 cm of the sensor, with the beeping speed proportional to the object’s proximity.

References:

I used several Youtube tutorials for executing the project using the tools.

-https://youtu.be/HynLoCtUVtU?si=zHnjgYTF8wYnlOKp

-https://youtu.be/0Lhgd8PQmn0?si=OXr1fvTP4b9X0pPW

Leave a Reply