Christopher WilliamsKyle Kalish
Published © GPL3+

Motion Activated Light Switch

Two Particle Photons communicate and switch on the lights when a PIR sensor is triggered. Created for MEGR 3171 Fall 2018 at UNCC

IntermediateFull instructions provided5 hours1,073
Motion Activated Light Switch

Things used in this project

Hardware components

Photon
Particle Photon
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Relay (generic)
Sainsmart model SKU: 101-70-101
×1
Extension Cord
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×6
Male/Male Jumper Wires
×6
Lamp
×1

Software apps and online services

Microsoft Excel

Hand tools and fabrication machines

Wire cutter/stripper

Story

Read more

Schematics

PIR Circuit Diagram

Relay Circuit Diagram

Code

Relay

C/C++
 /*
     * Living Rooom Lights
     */
     
    int ledPin = D7;                // pin for the LED
    int inputPin = D0;               //  input pin (for PIR sensor)
    int pirState = LOW;             // assuming no motion detected
    int val = 0;                    // variable for reading the pin status
    void setup() {
      pinMode(ledPin, OUTPUT);      // declare LED as output
      pinMode(inputPin, INPUT);     // declare sensor as input
      Particle.subscribe("Kalish_Apartment", myHandler, "DeviceID");
      Serial.begin(9600);
    }

    void myHandler(const char *event, const char *data)
{

  if (strcmp(data,"intact")==0) {
    // if beam is intact, then turn your board LED on
    digitalWrite(ledPin,HIGH);
  }
  else if (strcmp(data,"broken")==0) {
    // if beam is broken, turn your board LED off
    digitalWrite(ledPin,LOW);
  }
  else {
    // if the data is something else, don't do anything.
  }
}

Motion Sensor

C/C++
 /*
     * Living Room Lights
     */
    
    int ledPin = D7;                // pin for the LED
    int inputPin = D0;               //  input pin (for PIR sensor)
    int pirState = LOW;             // assuming no motion detected
    int val = 0;                    // variable for reading the pin status
    void setup() {
      pinMode(ledPin, OUTPUT);      // declare LED as output
      pinMode(inputPin, INPUT);     // declare sensor as input
      Particle.subscribe("Williams_Apartment", myHandler);
      Serial.begin(9600);
    }
 void loop(){
      val = digitalRead(inputPin);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
        digitalWrite(ledPin, HIGH);  // turn LED ON
        if (pirState == LOW) {
          // turned on
          Serial.println("Motion detected!");
          Particle.publish("Kalish_Apartment","intact");
          //  print on the output change, not state
          pirState = HIGH;delay(100000);
        }
      } else {
        digitalWrite(ledPin, LOW); // turn LED OFF
        if (pirState == HIGH){
          // turned off
          Serial.println("Motion ended!");
          Particle.publish("Kalish_Apartment","broken");
          // We only want to print on the output change, not state
          pirState = LOW;
        }
      }
    }
    
    void myHandler(const char *event, const char *data)
{

  if (strcmp(data,"intact")==0) {
    // if beam is intact, then turn your board LED on
    digitalWrite(ledPin,HIGH);
  }
  else if (strcmp(data,"broken")==0) {
    // if beam is broken, turn your board LED off
    digitalWrite(ledPin,LOW);
  }
  else {
    // if the data is something else, don't do anything.
  }
}

Credits

Christopher Williams

Christopher Williams

1 project • 0 followers
Mechanical Engineering student at UNC Charlotte
Kyle Kalish

Kyle Kalish

1 project • 0 followers
Mechanical Engineering Student at the University of North Carolina at Charlotte

Comments