SID_2004
Published © GPL3+

Burglar Alarm

I made this on my own.

IntermediateShowcase (no instructions)2,936
Burglar Alarm

Things used in this project

Story

Read more

Schematics

The circuit

screenshot_(24)_1hq46ai1kk.png

screenshot_(25)_PZi9RXqzbT.png

Code

The code:

Arduino
#include <Servo.h>
// create servo object to control a servo
Servo myservo;
// variable to store the servo position
int pos = 0;
// Define pins for ultrasonic, buzzer, led and relay
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
int const ledPin = 13;
i
void setup()
{
  myservo.attach(11);
  // attach servo on pin 11
  pinMode(trigPin, OUTPUT);
  // trig pin will have pulses output
  pinMode(echoPin, INPUT);
  // echo pin should be input to get pulse width
  pinMode(buzzPin, OUTPUT);
  // buzz pin is output to control buzzering
  pinMode(ledPin, OUTPUT);
  // led pin is output to glow light

  
}
void loop()
{
  // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  int duration, distance;
  // Output pulse with 1ms width on trigPin
  digitalWrite(trigPin, HIGH);
  delay(1);
  digitalWrite(trigPin, LOW);
  // Measure the pulse input in echo pin
  duration = pulseIn(echoPin, HIGH);
  // Distance is half the duration devided by 29.1 (from datasheet)
  distance = (duration / 2) / 29.1;
  // if distance less than 0.15 meter and more than 0 (0 or less means over range)
  if (distance <= 15 && distance >= 0)
  {
    // Buzz
    digitalWrite(buzzPin, HIGH);
    // Led on
    digitalWrite(ledPin, HIGH);
    
   
    {
      myservo.write(0);// tell servo to go to position in variable 'pos'
      delay(15);// servo 15ms to reach that position
    }
  }
  else
  {
    // Don't buzz
    digitalWrite(buzzPin, LOW);
    // Led off
    digitalWrite(ledPin, LOW);
    
    {
      myservo.write(90);// tell servo to go to position in variable 'pos'
      delay(15);// servo 15ms to reach that position
    }
  }
  // Waiting 60 ms won't hurt any one
  delay(60);
}

Credits

SID_2004

SID_2004

3 projects • 5 followers

Comments