Kyle YoungJoseph PoundDom Medel
Published

Alarm Activated Light

This project uses the contribution of three Particle Photon 2 devices which can detect noise and motion to control a light.

BeginnerFull instructions provided173
Alarm Activated Light

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×3
ELEGOO Upgraded 37 in 1 Sensor Modules Kit V2.0
ELEGOO Upgraded 37 in 1 Sensor Modules Kit V2.0
1 Button Switch, 1 Laser Emitter, 1 Big Sound Sensor
×1
Breadboard (generic)
Breadboard (generic)
×3
Jumper wires (generic)
Jumper wires (generic)
×10
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×3
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
High Brightness LED, White
High Brightness LED, White
×1
Through Hole Resistor, 10 ohm
Through Hole Resistor, 10 ohm
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Button Circuit Diagram

Sound Sensor Diagram

Motion Sensor and LED Diagram

Code

Button Code

C/C++
Checks for signal that the light is on. If the light is on and the button is pressed send signal to turn off button.
//Pins Used
int button = D0; //Button input
int Off = 0;
int On = 1;
bool light_status = false; //False = Off True = On
int status = D7;
int light;

void setup() {

//Initializing the the pin as input 
    pinMode(button, INPUT);
    Particle.subscribe("Light", light_stat, MY_DEVICES);   
}
    

void loop() {
// makes sure particle is on
    if (Particle.connected() == false) {
        Particle.connect();}
        
// val == 0 means that the button is pushed
// ONLY if button is pushed and light is on then turn off the light

if (digitalRead(button) == 0 && light_status == true) {
    Particle.publish("Light",String("Off")); // Tell the other photon to turn off the light
    digitalWrite(status, LOW);
}
}

void light_stat(const char *event, const char *data){                                         //Subscribed event, listens for the button to publish and then trunes the circuit on from there.
    if(strcmp(data, "On")==0){
        light_status = true;
    digitalWrite(status, HIGH);
    
    }
}

Sound Sensor Code

C/C++
Listens for a loud noise and publishes "Light On" to other devices.
int soundSensor = D0;
int alarmState = 0;

void setup() {
    pinMode(soundSensor, INPUT);
    RGB.control(true);
    RGB.color(0, 50, 0);
}

void loop() {
    int noiseLevel = digitalRead(soundSensor);
    
    if (Particle.connected() == false) {
        Particle.connect();
    }
    
    if (noiseLevel == HIGH && alarmState == 0) {
        Particle.publish("Light", String("On"));
        String soundon = String(1);
        Particle.publish("soundon", soundon, PRIVATE);
        RGB.color(255, 0, 0);
        alarmState = 1;
        delay(500);
    }
    
    else if (noiseLevel == LOW && alarmState == 1) {
        RGB.color(0, 50, 0);
        alarmState = 0;
        String soundoff = String(1);
        Particle.publish("soundoff", soundoff, PRIVATE);
    }
    delay(50);
}

Motion Sensor and LED Code

C/C++
Turns on LED from receiving signals. Also checks for motion to complete test.
#include "Particle.h"

// Define variables
int ledPin = D7;  // Digital pin for the LED
int motionSensorPin = D2;  // Digital pin for the motion sensor
bool isLEDon = false; //False = off, True = on

// Subscribe to the Particle Cloud for LED control and motion sensor
  void handleLED(const char *event, const char *data);
  void handleMotion(const char *event, const char *data);

void setup() {
  // Initialize pins
  pinMode(ledPin, OUTPUT);
  pinMode(motionSensorPin, INPUT);
  
  // Subscribe to the Particle Cloud for LED control and motion sensor
  Particle.subscribe("Light", handleLED);
  Particle.subscribe("Motion", handleMotion);

  // Initialize Serial Communication
  Serial.begin(9600);
}

// Callback function to handle motion events
void handleMotion(const char *event, const char *data) {
    Serial.println("Motion event received");
  // Handle motion events only when the LED is on
  if (isLEDon && digitalRead(motionSensorPin) == HIGH) {
    // Display motion on the Particle Console
    Particle.publish("Motion", "Yes");
    delay(5000);  // Delay for 5 seconds after motion detection
  }
}


// Callback function to handle LED
void handleLED(const char *event, const char *data) {
    // Check the event data
    if (strcmp(data, "On") == 0) {
        // Turn on the LED
        digitalWrite(ledPin, HIGH);
        isLEDon = true;
    } else if (strcmp(data, "Off") == 0) {
        // Turn off the LED
        digitalWrite(ledPin, LOW);
        isLEDon = false;
    }
}

Credits

Kyle Young
1 project • 2 followers
Contact
Joseph Pound
1 project • 1 follower
Contact
Dom Medel
1 project • 3 followers
Contact

Comments

Please log in or sign up to comment.