Introduction: Ultrasonic Security System

This project is an ultrasonic sensor-based alarm. It is distance sensitive so the speed of the buzzer depends upon how close or far the object is. If the object is near then the buzzer beeps faster and if it is far then the buzzer beeps slowly. there are many tutorials but I have this one, so anyone can do it with minimal parts

Supplies

Step 1: Princple

An ultrasonic sensor is an active component that measures distance by emitting ultrasonic waves and analyzing their reflections. Operating on the principle of echolocation, it uses a transducer to convert electrical energy into ultrasonic waves. As an object approaches, the sensor calculates distance based on the time it takes for the waves to travel to the object and back. Unlike a photoresistor responding to light, the ultrasonic sensor's output changes with the proximity of the target object, making it a key tool in distance detection.

Step 2: Schematic Diagram

Step 3: Source Code

if you want to add in the leds uncomment it

#define trigPin 6

#define echoPin 5

#define buzzer 2

#define greenLed 9

#define orangeLed 10

#define redLed 11


float new_delay;


void setup()

{

  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  pinMode(buzzer, OUTPUT);

  pinMode(greenLed, OUTPUT);

  pinMode(orangeLed, OUTPUT);

  pinMode(redLed, 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");


  /*

  // Turn off all LEDs

  digitalWrite(greenLed, LOW);

  digitalWrite(orangeLed, LOW);

  digitalWrite(redLed, LOW);


  if (distance > 100)

  {

    // Object is more than 1m away, turn on green LED

    digitalWrite(greenLed, HIGH);

  }

  else if (distance <= 100 && distance >= 50)

  {

    // Object is between 1m and 50cm away, turn on orange LED

    digitalWrite(orangeLed, HIGH);

  }

  else if (distance < 50 && distance >= 10)

  {

    // Object is between 50cm and 10cm away, turn on red LED

    digitalWrite(redLed, HIGH);

  }

  */


  if (distance < 50)

  {

    // Object is less than 50cm away, sound the buzzer

    digitalWrite(buzzer, HIGH);

    delay(new_delay);

    digitalWrite(buzzer, LOW);

  }

  else

  {

    digitalWrite(buzzer, LOW);

  }


  delay(200);

}


Step 4: Credits