sambarker21
Published © GPL3+

Arduino Security Alarm System

Simple alarm system that can be armed and disarmed

BeginnerFull instructions provided2 hours9,962
Arduino Security Alarm System

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Arduino UNO
Arduino UNO
×1
Buzzer
Buzzer
×1
LED (generic)
LED (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Arduino Security System Schematics

Code

Arduino Security System Code

Arduino
const int trigPin = 11;//Pin to send out sound wave
const int echoPin = 10; //Pin to listen for sound wave coming back

int ledPin = 5;//Pin to turn Led on
int buttonApin = 9;//Pin to arm the alarm
int buttonBpin = 8;//Pin to disarm the alarm
int value_A;//Value of pin A
int value_B;//Value of pin B


int buzzer = 6;//the pin of the active buzzer
int delayTime1= 250;
int delayTime2=250;

long duration; // Travel time obtained from sensor
int distance; // calculated distance of how far away the object is 

void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  

  pinMode(buzzer, OUTPUT);
  
  pinMode(12, INPUT);
  
  Serial.begin(9600);

  

}

void loop() {

  // Check to see the state of button A and B
  value_A = digitalRead(buttonApin);
  value_B = digitalRead(buttonBpin);
  
  delay(500);

  
   if (value_A == LOW)
  {
    //If the alarm is armed, turn the LED on
    digitalWrite(ledPin, HIGH);
    while(ledPin, HIGH)
    {
      // Send out a sound wave and calculate how far away the nearest object is to it. 
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
    
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
    
      duration = pulseIn(echoPin, HIGH);
      distance = duration*0.034/2;
    
      Serial.print("Distance:");
      Serial.println(distance);

      while (distance < 98)
      {
        // If the distance is less than 98 cm, then sound the alarm and make the led flash
           
            digitalWrite(buzzer,HIGH);
            digitalWrite(ledPin, HIGH);
            delay(delayTime1);//wait for 1ms
            digitalWrite(buzzer,LOW);
            digitalWrite(ledPin, LOW);
            delay(delayTime1);//wait for 1ms

 
                digitalWrite(buzzer,HIGH);
                digitalWrite(ledPin, HIGH);
                delay(delayTime2);//wait for 2ms
                digitalWrite(buzzer,LOW);
                digitalWrite(ledPin, LOW);
                delay(delayTime2);//wait for 2ms   

             // If the B button is pushed, shut the alarm off and disarm it
             if (digitalRead(buttonBpin) == LOW)
              {
                break;
                }
      
          
      }
    
      delay(100);

      // If b button is pressed, disarm the alarm
      if (digitalRead(buttonBpin) == LOW)
      {
        break;
      }
      

      }

      
  }
  // If the alarm is disarmed, turn the LED off
  if (value_B == LOW)
  {
    digitalWrite(ledPin, LOW);
  }

 
  
  
 
}

 

Credits

sambarker21

sambarker21

1 project • 2 followers

Comments