Anton
Published © CC BY-NC-SA

Particle Photon PIR Sensor and Event Reporting

Sneaky kids stealing that Halloween candy? Use a Particle Photon to detect and report motion events.

BeginnerFull instructions provided1 hour4,021
Particle Photon PIR Sensor and Event Reporting

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Photon
Particle Photon
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Circuit Diagram

Wiring diagram for the Photon, PIR Sensor, and LED

PIR Sensor Image

Image of the wiring diagram

Code

PhotonPIR Sketch

Arduino
Sketch to upload to Particle Photon
// Sketch for Particle Photon - PIR Sensor / Motion Detection
// By Anton

int inputPin = D0;              // choose the input pin (for PIR sensor)
int ledPin = D1;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

int calibrateTime = 5000;      // wait for the thingy to calibrate

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(inputPin, INPUT);     // declare sensor as input
}

void loop() {

  // if the sensor is calibrated
  if (calibrated()) {
  // get the data from the sensor
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
    }
}

void readTheSensor() {
    val = digitalRead(inputPin);
}

bool calibrated() {
    return millis() - calibrateTime > 0;
}

void setLED(int state) {
    digitalWrite(ledPin, state);
}

void reportTheData() {
    if (val == HIGH) {
        // the current state is no motion
        // i.e. it's just changed
        // announce this change by publishing an event
        if (pirState == LOW) {
          // we have just turned on
          Particle.publish("PhotonMotion", "Motion Detected", PRIVATE);
          // Update the current state
          pirState = HIGH;
          setLED(pirState);
        }
    } else {
        if (pirState == HIGH) {
          // we have just turned of
          // Update the current state
          Particle.publish("PhotonMotion", "Off", PRIVATE);
          pirState = LOW;
          setLED(pirState);
        }
    }
}

Credits

Anton

Anton

5 projects • 38 followers
Time traveler, developer; special interest in black holes and bending space time

Comments