0% found this document useful (0 votes)
99 views

Full Code

This document contains code for an Arduino-based home security system that uses a PIR motion sensor. It defines pin connections and functions for controlling motors to move in different directions, reading sensor values, triggering an alarm with buzzer and SMS notification, and analyzing sensor data to detect motion. When motion is detected, it will send an SMS alert message to a phone number.

Uploaded by

ayan saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Full Code

This document contains code for an Arduino-based home security system that uses a PIR motion sensor. It defines pin connections and functions for controlling motors to move in different directions, reading sensor values, triggering an alarm with buzzer and SMS notification, and analyzing sensor data to detect motion. When motion is detected, it will send an SMS alert message to a phone number.

Uploaded by

ayan saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

int pin = 13; //Piezo Buzzer Connected

volatile int state = LOW;

#define capPin A5

#define buz 9

#define pulsePin A4

#define led 10

long sumExpect=0; //running sum of 64 sums

long ignor=0; //number of ignored sums

long diff=0; //difference between sum and avgsum

long pTime=0;

long buzPeriod=0;

#define m11 3

#define m12 4

#define m21 5

#define m22 6

#define D0 19

#define D1 18

#define D2 17

#define D3 16

void forward()

digitalWrite(m11, HIGH);

digitalWrite(m12, LOW);

digitalWrite(m21, HIGH);

digitalWrite(m22, LOW);

void backward()
{

digitalWrite(m11, LOW);

digitalWrite(m12, HIGH);

digitalWrite(m21, LOW);

digitalWrite(m22, HIGH);

void left()

digitalWrite(m11, HIGH);

digitalWrite(m12, LOW);

digitalWrite(m21, LOW);

digitalWrite(m22, LOW);

void right()

digitalWrite(m11, LOW);

digitalWrite(m12, LOW);

digitalWrite(m21, HIGH);

digitalWrite(m22, LOW);

void Stop()

digitalWrite(m11, LOW);

digitalWrite(m12, LOW);

digitalWrite(m21, LOW);

digitalWrite(m22, LOW);

void setup()

pinMode(D0, INPUT);

pinMode(D1, INPUT);
pinMode(D2, INPUT);

pinMode(D3, INPUT);

pinMode(m11, OUTPUT);

pinMode(m12, OUTPUT);

pinMode(m21, OUTPUT);

pinMode(m22, OUTPUT);

pinMode(pin, OUTPUT); // buzzer as output

digitalWrite(pin, state); //make buzzer OFF

//PIR Motion Sensor connected at digital pin 2 i.e. interrupt 0

attachInterrupt(0, makeSMS_Alarm, RISING);

Serial.begin(9600); //set Baud Rate

Serial.begin(9600);

pinMode(pulsePin, OUTPUT);

digitalWrite(pulsePin, LOW);

pinMode(capPin, INPUT);

pinMode(buz, OUTPUT);

digitalWrite(buz, LOW);

pinMode(led, OUTPUT);

void loop()

int temp1=digitalRead(D0);

int temp2=digitalRead(D1);

int temp3=digitalRead(D2);

int temp4=digitalRead(D3);

if(temp1==0 && temp2==1 && temp3==0 && temp4==0)

forward();

else if(temp1==0 && temp2==0 && temp3==1 && temp4==0)

left();
else if(temp1==0 && temp2==1 && temp3==1 && temp4==0)

right();

else if(temp1==0 && temp2==0 && temp3==0 && temp4==1)

backward();

else if(temp1==1 && temp2==0 && temp3==1 && temp4==0)

Stop();

int minval=1023;

int maxval=0;

long unsigned int sum=0;

for (int i=0; i<256; i++)

//reset the capacitor

pinMode(capPin,OUTPUT);

digitalWrite(capPin,LOW);

delayMicroseconds(20);

pinMode(capPin,INPUT);

applyPulses();

//read the charge of capacitor

int val = analogRead(capPin); //takes 13x8=104 microseconds

minval = min(val,minval);

maxval = max(val,maxval);

sum+=val;

long unsigned int cTime=millis();

char buzState=0;

if (cTime<pTime+10)

if (diff>0)

buzState=1;

else if(diff<0)
buzState=2;

if (cTime>pTime+buzPeriod)

if (diff>0)

buzState=1;

else if (diff<0)

buzState=2;

pTime=cTime;

if (buzPeriod>300)

buzState=0;

if (buzState==0)

digitalWrite(led, LOW);

noTone(buz);

else if (buzState==1)

tone(buz,2000);

digitalWrite(led, HIGH);

else if (buzState==2)

tone(buz,500);

digitalWrite(led, HIGH);

}
//subtract minimum and maximum value to remove spikes

sum-=minval;

sum-=maxval;

if (sumExpect==0)

sumExpect=sum<<6; //set sumExpect to expected value

long int avgsum=(sumExpect+32)>>6;

diff=sum-avgsum;

if (abs(diff)<avgsum>>10)

sumExpect=sumExpect+sum-avgsum;

ignor=0;

else

ignor++;

if (ignor>64)

sumExpect=sum<<6;

ignor=0;

if (diff==0)

buzPeriod=1000000;

else

buzPeriod=avgsum/(2*abs(diff));

void makeSMS_Alarm()

// Piezo Buzzer ON

state = HIGH;

digitalWrite(pin, state);
//Send SMS

Serial.print("AT+CMGF=1\r");

//Phone number you want to send the sms

Serial.print("AT+CMGS=\"+91xxxxxxxxxx\"\r");

//Text to send

Serial.print("Intruder Alert - Someone At Home | www.maxphi.com\r");

//sends ctrl+z, end of message

Serial.write(0x1A);

void applyPulses()

for (int i=0;i<3;i++)

digitalWrite(pulsePin,HIGH); //take 3.5 uS

delayMicroseconds(3);

digitalWrite(pulsePin,LOW); //take 3.5 uS

delayMicroseconds(3);

You might also like