roshan-baig
Published © GPL3+

Security System Using PIR Sensor

This is a Security System Using PIR

IntermediateShowcase (no instructions)2,266
Security System Using PIR Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Any Arduino will work, but you may have to change the code
×1
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
Any LCD will work
×1
Trimmer Potentiometer, 250 kohm
Trimmer Potentiometer, 250 kohm
For Adjusting contrast
×1
Through Hole Resistor, 1 kohm
Through Hole Resistor, 1 kohm
×2
PIR Sensor, 7 m
PIR Sensor, 7 m
×1
Buzzer
Buzzer
×1
Photo resistor
Photo resistor
optional
×1
Resistor 10k ohm
Resistor 10k ohm
×1
5 mm LED: Red
5 mm LED: Red
Any LED will work
×1
Resistor 220 ohm
Resistor 220 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE
Tinkercad
Autodesk Tinkercad

Story

Read more

Schematics

The Circuit

Code

The code

C/C++
#include <LiquidCrystal.h>

// C++ code
/*
Security system using PIR sensor
Made by Roshan Baig
*/
LiquidCrystal lcd(2,3,4,5,6,7);
int ledPin = 12;
const int PIRPin = 8;
int pirState = LOW;
int val = 0;
int photoCellPin = A0;
int photoCellReading;
int speakerPin = 10;
void setup()
{
  lcd.begin(16,2);
  pinMode(ledPin, OUTPUT);
  pinMode(PIRPin, INPUT);
  pinMode(photoCellPin, INPUT);
  pinMode(speakerPin, INPUT);
  Serial.begin(9600);
  lcd.setCursor(0,0);
  lcd.print("P.I.R Motion And");
  lcd.setCursor(0,1);
  lcd.print("light sensors");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Processing Data.");
  playTone(300,300);
  delay(150);
  playTone(0,0);
  delay(3000);
  lcd.clear();
  lcd.setCursor(3,0);
  lcd.print("Waiting For");
  lcd.setCursor(3,1);
  lcd.print("Motion...");
}

void loop()
{
  val = digitalRead(PIRPin);
  photoCellReading = analogRead(photoCellPin);
  if(val == HIGH)
  {
    digitalWrite(ledPin, HIGH);
    if(pirState == LOW)
    {
      Serial.println("Motion Detected");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Motion Detected");
      lcd.setCursor(0,1);
      lcd.print(photoCellReading);
      playTone(300,300);
      delay(150);
      playTone(0,0);
      pirState = HIGH;
    }
  } else
  {
    digitalWrite(ledPin, LOW);
    delay(300);
    scrollScreenSaver();
    if(pirState == HIGH)
    {
      Serial.println("Motion Ended");
      pirState = LOW;
    }
  }
}
void playTone(long duration, int freq)
{
  duration *= 1000;
  int period = (1.0/freq)*100000;
  long elapsed_time = 0;
  while(elapsed_time < duration)
  {
    digitalWrite(13, HIGH);
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(period/2);
    digitalWrite(13, LOW);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(period/2);
    elapsed_time += (period);
  }
}
void scrollScreenSaver()
{
  lcd.clear();
  lcd.setCursor(15,0);
  lcd.print("No Motion");
  lcd.setCursor(15,1);
  lcd.print("Waiting");
  for(int i = 0; i < 22; i++)
  {
    lcd.scrollDisplayLeft();
    delay(150);
  }
}

Credits

roshan-baig

roshan-baig

5 projects • 7 followers

Comments