Jesse RedfordKeaton Cooper
Published

Smart Relay MEGR-3171

A motion controlled/light sensing activated relay board to control your lights, fans, or any other household appliance.

IntermediateFull instructions provided2 hours732
Smart Relay MEGR-3171

Things used in this project

Hardware components

Photon
Particle Photon
Parts List & Prices • 3 Particle Photons ($20.00 per photon) • PIR sensor module ($2.00) • Particle Relay Board ($30.00) • 12V Wall Adapter Power Supply ($7.99) • Photo Resistor ($0.20) • 14-20 Jumper wires ($2.00) Total Project Cost = $102.19 Links to materials Relay Shield https://www.amazon.com/Particle-RLYSHLD-Other-Development-Shield/dp/B01GVI2RWI PIR motion sensor https://www.amazon.com/DIYmall-HC-SR501-Motion-Infrared- 12V 2Amp Wall Adapter power Supply https://www.amazon.com/Amamax%C2%AE-Wall-Adapter-Power- Particle Photon https://www.amazon.com/Particle-Reprogrammable-Development-Prototyping-Electronics/dp/B016YNU1A0/ref=sr_1_1_sspa?s=electronics&ie=UTF8&qid=1541910559&sr=1-1-spons&keywords=photon&psc=1 Photoresistor https://www.amazon.com/eBoot-Photoresistor-Sensitive-Resistor-Dependent/dp/B01N7V536K/ref=sr_1_1_sspa?s=electronics&ie=UTF8&qid=1541910734&sr=1-1-spons&keywords=photoresistor&psc=1 Jumper Wires https://www.amazon.com/McIgIcM-Solderless-Flexible-Breadboard-Arduino/dp/B071NVN87X/ref=sr_1_17?s=electronics&ie=UTF8&qid=1541910279&sr=1-17&keywords=jumper+wires
×3
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Male/Male Jumper Wires
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
Photo resistor
Photo resistor
×1
PIR Sensor (generic)
×1

Software apps and online services

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

Story

Read more

Schematics

Motion Detection vs Time plot

the following channel was created using a webhook and thingSpeak.io
This channel provides a continuous plot throughout the day of how many times motion has been detected. The following graph shows that were was no motion during the time period recorded.

How The System Works

What it looks like

This configuration was used so that the relay board could provide power to run all 3 photons and the system could act as a singular standalone unit and only occupy 1 power outlet. However, the system works through wifi so the photons connected to the photoresistor and the PIR sensor could be placed anywhere in your house without making any changes to the coding for this project.

Simple Wiring Diagram

Please seek advise from a qualified or certified professional before attempting to wire appliances to the relay board. High Voltage is a dangerous thing!!

picture_of_components_1_ey5hbkG8PM.jpg

Code

Photon 1 - Light Detection (Secondary)

C/C++
Photon 1 is a photoresistor that is constantly checking if the room is dark or light. The results of the photoresistor are published online for photon 3 to subscribe to.
int photoresistor = A1;
int power = A5;
int analogvalue;     

//This code set the inital values of the code and assigns each of them to a pin //on the particle photon

void setup() {
    pinMode(power,OUTPUT);
    pinMode(photoresistor, INPUT);
    Particle.variable("analogvalue", &analogvalue, INT);
    
}

//This code sets up which pins supply power, input and how the particle receives //the photoresistor input

void loop() {
    
    analogvalue = analogRead(photoresistor);
    delay(100);
    
    if (analogvalue > 55){
        Particle.publish("LightStatus_KphotonUNCC","Bright", PUBLIC);
        digitalWrite(led,HIGH);
        delay(4000);
      
    }
    else {
        Particle.publish("LightStatus_KphotonUNCC","Dark", PUBLIC);
        digitalWrite(led,LOW);
        delay(4000);
        
    }
    
}
    
//This void loop reads the analog values recieved from the photoresistor and if //the value is above 55, it publishes that the ambient light is bright. If it is //below 55, it publishes that the amblient light is dark. It then delays 4 //seconds to prevent the photon from publishing too often. The value of 55 has to //be fine tuned for each individual situation. Also, if the published values are //backwards, you can flip the greater than and less symbols in the loop.

Photon 2 - Motion Detection (Secondary)

C/C++
This Photon is constantly checking whether or not there is activity going on in the room using a PIR Sensor.
Based on the activity in the room this photon publishes a message to the particle cloud stating "The Room is Occupied" or " The Room is Unoccupied"
int ledPin = D7;                 // choose the pin for the LED
int inputPin = D0;               // choose the PIR sensor pin
//VIN & GRN & D0 are the only wire required to plug in, D7 turns on when motion is detected
bool available;                  // status of room
int motionCounter = 0;           // variable to count motion events

Timer timer(4000, determineMotion); // software timer to check every 10s


void setup() {
  pinMode(ledPin, OUTPUT);       // set LED as output
  pinMode(inputPin, INPUT);      // set sensor as input
  timer.start(); // start the determineMotion timer
}




void determineMotion() {    // this function determines if there's motion
    if(motionCounter < 2) { // if very little motion was detected
        if(available == false) { // only publish if the status changed
            Particle.publish("MotionStatus_MasterPhotonUNCC","Roomisunoccupied",PUBLIC); //publish to the relay photon
            }
        available = true; // set the status to available
    } else if (motionCounter >= 2) {
        if(available == true) { // only publish if the status changed
            Particle.publish("MotionStatus_MasterPhotonUNCC","Roomisoccupied",PUBLIC); //publish to the relay photon
            }
        available = false; // set the status to in use
    }
    motionCounter = 0; // reset motion counter
}

void loop() {
  if (digitalRead(inputPin) == HIGH) {  // check if the input is HIGH
    digitalWrite(ledPin, HIGH);         // turn LED ON if high
    motionCounter++;                    // increment motion counter
  } else {
    digitalWrite(ledPin, LOW);          // turn LED OFF if no input
  }
  delay(500);                           // wait 0.5s
}

Photon 3 - Relay Board (Primary)

C/C++
Photon 3 is connected to the relay shield and is subscribed to the published events from photon 1 and 2.
Based on the events published by each photon the code is set up such that if the room is "Dark" and there is "Motion" detected in the room. The photon will turn a relay on which will supply power to the LED strip wired to it. However, if no motion is detected for a period of time or if the room is sufficient bright. Then the particle will turn the relay off.
int ledPin = D5;                 // choose the pin for the LED
 
bool x ; //sets a boolean value of x for photon 1
bool y ; //sets a boolean value of y for photon 2


void setup() {
pinMode(ledPin, OUTPUT);       // set LED as output
Particle.subscribe("LightStatus_KphotonUNCC",data,"280030000d47343438323536"); // subscribes to Photon 1 for light detection
Particle.subscribe("MotionStatus_MasterPhotonUNCC",anything,"500026000b51353432383931"); // subscribes to Photon 2 for motion detection
}

void data(const char *event, const char *data)          // light Publish
 {
     if (strcmp(data,"Bright"))
     x = true;
     else 
     x = false;
 }
 //reads published data from photon 1
 
void anything(const char *event, const char *data)    // motion sensor
{
    
   if (strcmp(data,"Roomisoccupied"))
    y = false;
    else 
    y = true;
}
//reads published data from photon 2
    

void loop() {
  if (x && y)
//if photon 1 is Dark, and photon 2 Roomisoccupied, then the relay is switched on
digitalWrite(D5, HIGH);
else
digitalWrite(D5,LOW);
}
//if not, the relay stays closed

Credits

Jesse Redford

Jesse Redford

1 project • 1 follower
Keaton Cooper

Keaton Cooper

0 projects • 1 follower

Comments