Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
William Louisjohn papanicolasSean O'Brien
Published © GPL3+

DIY Home Motion Sensor Light

This motion light will automatically tell if it is day or night, and will only activate itself if there is motion and the sun is down.

BeginnerFull instructions provided4 hours1,479
DIY Home Motion Sensor Light

Things used in this project

Hardware components

Photon
Particle Photon
×3
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Servos (Tower Pro MG996R)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Photo resistor
Photo resistor
×1

Story

Read more

Schematics

PIR Schematic

Pin out to the PIR Sensor

Photoresistor Schematic

Pin out to the photoresistor and the other resistor.
Note that each component has to be placed into two different points

Servo Schematic

Pin out to the Servo

Code

PIR Sensor Code

Arduino
Used to see if the PIR sensor is being triggered by motion or not
/*                          +-----+
 *               +----------| USB |----------+
 *               |          +-----+       *  |
 *               | [ ] VIN           3V3 [*] |<-------Power To PIR Sensor
 *               | [ ] GND           RST [ ] |
 *               | [ ] TX           VBAT [ ] |
 *               | [ ] RX  [S]   [R] GND [*] |<-------Ground for PIR Sensor
 *               | [ ] WKP            D7 [ ] |       
 *               | [ ] DAC +-------+  D6 [ ] |       
 *               | [ ] A5  |   *   |  D5 [ ] |       
 *               | [ ] A4  |Photon |  D4 [ ] |     
 *               | [ ] A3  |       |  D3 [ ] |
 *               | [ ] A2  +-------+  D2 [ ] |
 *               | [ ] A1             D1 [ ] |
 *               | [ ] A0             D0 [*] |<-------Signal from PIR Sensor
 *               |                           |
 *                \    []         [______]  /
 *                 \_______________________/
 */
int PIR = D0; //sets PIR equal to pin D0
void setup() {
    pinMode(PIR, INPUT);
    //sets pin D0 as an input
}

void loop() {
    if (digitalRead(PIR) == HIGH) {
        Particle.publish("office-motion");
    delay(10000);
    //if the PIR pin reads a High signal then it will trigger the publish command
    }
}

Photoresistor read and publish control

Arduino
Read the analog value on the photoresistor and from that determine whether or not the sun is out based on that value
/*                                            +-----+
 *                                 +----------| USB |----------+
 *                                 |          +-----+       *  |
 *                                 | [ ] VIN           3V3 [ ] |
 *         Resistor Lead---------->| [*] GND           RST [ ] |
 *                                 | [ ] TX           VBAT [ ] |
 *                                 | [ ] RX  [S]   [R] GND [ ] |
 *                                 | [ ] WKP            D7 [ ] |       
 *                                 | [ ] DAC +-------+  D6 [ ] |       
 *         Photoresistor Lead----->| [*] A5  |   *   |  D5 [ ] |       
 *                                 | [ ] A4  |Photon |  D4 [ ] |     
 *                                 | [ ] A3  |       |  D3 [ ] |
 *                                 | [ ] A2  +-------+  D2 [ ] |
 * One end of the                  | [ ] A1             D1 [ ] |
 * Photorisistor and Resistor ---->| [*] A0             D0 [ ] |
 *                                 |                           |
 *                                  \    []         [______]  /
 *                                   \_______________________/
 */
 
int photoresistor = A0; //Sets pin A0 as the photoresistor
int power = A5; //Sets pis A5 as power
int analogvalue; //declares the variable analogvalue
void setup() {
    pinMode(photoresistor, INPUT); //makes pin A0 an input to receive a signal
    pinMode(power, OUTPUT); //sets pin A5 as an output to provide power to the photoresistor
    digitalWrite(power,HIGH); //sets the output of pin A5 to go High
    Particle.variable("analogvalue", &analogvalue, INT);
    Particle.subscribe("office-motion", photoresist, "260046001047353138383138");
    //subscribes this particle to the PIR sensor particle
}
void photoresist(const char *event, const char *data) 
//triggered when the event this particle is subscribed to is triggered
{
  analogvalue = analogRead(photoresistor);
  //reads the analogvalue of the photoresistor
    delay(100);
    if (analogvalue <= 250){
        Particle.publish("nosun1234567");
        delay(10000);
        //if the value is less than 250 the particle publishes that the sun is not out
    }
    else if (analogvalue > 250){
        Particle.publish("sun1234567");
        delay(10000);
        //if the value is greated than 250 the particle publishes that the sun is out
    }
}
void loop() {
}

Particle Servo Control

Arduino
Load onto particle and connect servo to control the flipping of the light.
/*                          +-----+
 *               +----------| USB |----------+
 *               |          +-----+       *  |
 *               | [ ] VIN           3V3 [*] |<-------Power To Servo
 *               | [ ] GND           RST [ ] |
 *               | [ ] TX           VBAT [ ] |
 *               | [ ] RX  [S]   [R] GND [*] |<-------Servo Ground
 *               | [ ] WKP            D7 [ ] |       
 *               | [ ] DAC +-------+  D6 [ ] |       
 *               | [ ] A5  |   *   |  D5 [ ] |       
 *               | [ ] A4  |Photon |  D4 [ ] |     
 *               | [ ] A3  |       |  D3 [ ] |
 *               | [ ] A2  +-------+  D2 [ ] |
 *               | [ ] A1             D1 [ ] |
 *               | [ ] A0             D0 [*] |<-------Signal To Servo
 *               |                           |
 *                \    []         [______]  /
 *                 \_______________________/
 */
Servo myservo;
int pos = 0;
int moveArm;
int arnPosition;
void setup() {
//attaches the servo PWM control to pin D0
myservo.attach(D0);
//declares a function "moveArm" which will be used to set the position on the servo arm
Particle.function("moveArm",setPos);
//Subsctibing to the Particle which sends whether the sun is out or not
Particle.subscribe("nosun1234567", nosun, "34001d000f47343432313031");
}
void nosun(const char *event, const char *data)
{
    myservo.write(125); //writes the position to the servo to turn the light on
    delay(5000);
    myservo.write(0); //after the delay the servo goes back to its original position and turns off the light
    delay(100);
}
void loop() {
}
//declares the function "setPos" which relied on the string command from the user
int setPos(String command) {
    //if "on" is submitted the servo will rotate to 180 degrees
    if (command == "on") {
        myservo.write(125);
        delay(100);
    }
    //if "off" is submitted the servo will rotate back to 0 degrees
    else if (command == "off") {
        myservo.write(0);
        delay(100);
    }
}

Credits

William Louis

William Louis

1 project • 0 followers
john papanicolas

john papanicolas

1 project • 0 followers
Sean O'Brien

Sean O'Brien

1 project • 0 followers

Comments