Team DabbaWalla (PEvans/Dlumia)
Published © GPL3+

Basic Motion Detection with Fridge Tamper Detection

Receive notification of activity in your home. Know when unauthorized guests are in your home and when important items are tampered with.

BeginnerFull instructions provided10 hours1,215
Basic Motion Detection with Fridge Tamper Detection

Things used in this project

Hardware components

Photon
Particle Photon
×2
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×1
Slide Switch
Slide Switch
×1
SparkFun Keypad
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Qualcomm 3D printed Custom Case
×1

Software apps and online services

Apple particle.io
Maker service
IFTTT Maker service

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Case Concept

Schematics

Circuit

Circuit magnets

Code

Motion Detector with Magnetic Tamper Indicator

C/C++
This code is uploaded to the photon particle and give the particle the program to operate a PIR sensor to detect motion. This sensor, when detecting motion will send a signal notification to a personal phone while activating an LED. The photon also operates a magnet pair that when separated, a signal notification is sent to the phone also. This can be placed on anything that the customer wishes to be notified if it is tampered with.
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 100;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = D6;    //the digital pin connected to the PIR sensor's output
int ledPin = D2;
int MagPos = D0;      //******************************
int MagNeg = D1;        //******************************
//int ledPin = D2;  


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);
  pinMode(MagPos,OUTPUT); //******************************
  pinMode(MagNeg,INPUT);  //******************************
  

  digitalWrite(ledPin,HIGH);   //******************
  delay(100);
  digitalWrite(ledPin,LOW);    //******************
  delay(100);
  digitalWrite(ledPin,HIGH);   //******************
  delay(100);
  digitalWrite(ledPin,LOW);    //******************
  delay(100);
  digitalWrite(ledPin,HIGH);   //******************
  delay(100);
  digitalWrite(ledPin,LOW);    //******************


  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(1);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       Particle.publish("Motion Status","Someone is in the kitchen",60,PRIVATE);
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(1);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(1);
           Particle.publish("Motion Status","No one is in the kitchen",60,PRIVATE);
           }
       }
while (Particle.connected()){

if(digitalRead(MagNeg)==LOW){

Particle.publish("DoorStatus","FRIDGE OPEN",60,PRIVATE);
delay(5000);}
else{
    delay(5000);}}
  }
 

Basic Motion Sensing Code

C/C++
Uploads to the photon particle to give it the program to operate a simple PIR motion detector sending a signal to a personal phone and activating an LED.
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 100;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = D6;    //the digital pin connected to the PIR sensor's output
int ledPin = D2;



/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(1);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       Particle.publish("Motion Status","Someone is in the kitchen",60,PRIVATE);
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(1);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(1);
           Particle.publish("Motion Status","No one is in the kitchen",60,PRIVATE);
           }
       }
  }

Credits

Team DabbaWalla (PEvans/Dlumia)

Team DabbaWalla (PEvans/Dlumia)

1 project • 3 followers

Comments