Colin GlasgowThomas SpiveyDylan Staples
Published

MEGR 3171 Bathroom Occupation Detection

A combination of sound, light, and motion sensors to detect if you've left your lights on when they shouldn't be.

BeginnerFull instructions provided58
MEGR 3171 Bathroom Occupation Detection

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×3
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Photo resistor
Photo resistor
×1
SparkFun Sound Detector (with Headers)
SparkFun Sound Detector (with Headers)
×1
Jumper wires (generic)
Jumper wires (generic)
×9
Breadboard (generic)
Breadboard (generic)
×3

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service
Google Sheets
Google Sheets

Story

Read more

Schematics

Sound Sensor Particle

Motion and Light Sensor Particle

Code

Light and Motion code

C/C++
int photoresistor = A0;
int analogvalue;   
int inputPin = D0;               //  input pin (for PIR sensor)
int pirState = LOW;             // assuming no motion detected
int val = 0;                    // variable for reading the pin status

//This section of code simply assigns the pins on the Particle Photon, and names 
//variables that will be used later in the code.

void setup() {
    pinMode(photoresistor, INPUT);
    Particle.variable("analogvalue", &analogvalue, INT);
    pinMode(inputPin, INPUT);     // declare sensor as input
}

//This section of the code is the setup for the rest of the code.  It applies power
//to the pins requiring power, or names the pin to recieve an input; in this case
//from our photo resistor.

void loop() {
    val = digitalRead(inputPin);  // read input value
    analogvalue = analogRead(photoresistor);
    if (val == HIGH) {            // check if the input is HIGH
        if (pirState == LOW) {
          // turned on
          Particle.publish("MotionOn", PUBLIC);
          //  print on the output change, not state
          pirState = HIGH;delay(10);
        }
      } else {
        if (pirState == HIGH){
          // turned off
          Particle.publish("MotionOff", PUBLIC);
          // We only want to print on the output change, not state
          pirState = LOW;
        }
      }
    if (analogvalue < 3600){
        Particle.publish("LightOn", PUBLIC);
        delay(1000);
    }
    else {
        Particle.publish("LightOff", PUBLIC);
        delay(1000);
    }
    
}

Sound Code

C/C++
//Sound Sensor Code
const int soundSensorPin = D0;
const int ledPin = D7; // D7 LED pin on the Particle Photon

void setup() {
  pinMode(soundSensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  Particle.subscribe("sound_detected", soundHandler, MY_DEVICES);
}

void loop() {
  // Check for sound detection
  if (digitalRead(soundSensorPin) == HIGH) {
    // Publish an event when sound is detected
    Particle.publish("sound_detected", "on",  PUBLIC);
    delay(250); // Add a delay to avoid multiple triggers for the same sound
  }
}

void soundHandler(const char *event, const char *data) {
  // Handle the sound detection event
  digitalWrite(ledPin, HIGH); // Turn on the D7 LED
  delay(1000); // Keep the LED on for 1 second (adjust as needed)
  digitalWrite(ledPin, LOW); // Turn off the D7 LED
}

LED Code

C/C++
int led=D7;
int x=0;
int y=0;
int z=0;

void setup () {
    pinMode(led, OUTPUT);
  Particle.subscribe("sound_detected", sound, "0a10aced202194944a04ab6c");
  Particle.subscribe("LightOn", light, "0a10aced202194944a04ad10");
  Particle.subscribe("MotionOn", motion, "0a10aced202194944a04ad10");
}

void sound(const char *event, const char *data) {
    x=1;
}
void light(const char *event, const char *data) {
    y=1;
}
void motion(const char *event, const char *data) {
    z=1;
}

void loop () {
    if (x==1 && y==1 && z==1) {
    digitalWrite(led,HIGH);
    delay(5000);
    digitalWrite(led,LOW);
    x=0;
    y=0;
    z=0;
    }
    
}

Credits

Colin Glasgow
1 project • 0 followers
Thomas Spivey
1 project • 0 followers
Dylan Staples
1 project • 0 followers

Comments