Slade GarraghtyAlex BaltosBahaa Eddine Antar
Published

Smart Motion Light

The Smart Motion Light acts as an easy way to prevent unwanted lights left on in your house.

BeginnerFull instructions provided3 hours219
Smart Motion Light

Things used in this project

Hardware components

Motion Sensor
×1
Particle Photoresistor
×1
Voltage Sensor
×1
Micro Servo Motor
×1
Sparkfun Breadboard
×2
Basic Light Switch
×1
Resistor 220 ohm
Resistor 220 ohm
×2
LED (generic)
LED (generic)
×1
Argon, IoT Starter Kit – Particle Retail
×3
Desk Lamp, LED
Desk Lamp, LED
×1

Software apps and online services

Adafruit IO

Story

Read more

Schematics

Circuit Flow Diagram

This figure outlines the communication between the three argon devices.

Circuit Diagram

This figure details the complete circuit diagrams for the three Argon devices used in our project. This figure can be used to replicate or reproduce our project.

Adafruit Graphed Data for Motion and Voltage

Code

Argon #2 (Motion Sensor)

C/C++
// MOTION SENSOR CODE FOR 2nd ARGON
// THIS CODE IS COMPLETE AND WORKING, READY TO BE FLASHED TO A 2nd ARGON

// WIRE GUIDE:
// -------motion sensor-----
// GND TO GND COLUMN
// VCC TO VUSB
// OUT TO D4
//-----led bulb------------
// + (Longer) LEG INTO A5
// - (shorter) LEG INTO GND COLUMN

int argonled = D7; //d7 pin is the on board led, will blink on and off when motion is detected
int ir = D4; //digital pin d4 is named ir, infrared optical motion sensor
int PinLed = A5; 


void setup() {

pinMode(ir, INPUT); // sets the ir reader pin as the input for d4
pinMode(PinLed, OUTPUT); // sets the output data to pin a5, where the led is
}

void loop() {
  
    delay(5000); // gives loop time to publish events and reset
    if(digitalRead(ir) == LOW) // if the ir sensor is off, 
    {
        digitalWrite(PinLed, LOW); //led will be off if object is close
    
        Particle.publish("ir",String(ir==0), PUBLIC);
        
    }
    
    else
    {
        digitalWrite(PinLed, HIGH); // led will be on if object is far
        Particle.publish("ir",String(ir), PUBLIC); //sends event to cloud + webhooks to chart on adafruit
        Particle.publish("motion sensed", PUBLIC);//sends event to be read by servo argon
    }

}

Argon #3 (Voltage Sensor)

C/C++
//Voltage Sensor argon Code
//this will run on 3rd argon
//this should connect to webhooks, then to adafruit for a chart of activity

//Wiring guide for voltage sensor:
// male pin group                          female pin group
// S to pin a3
//- to ground                              - towards ground of servo
//+ to Vusb                                + at servo power input

int Vsensor = A3; //s wire from voltage sensor goes to a3 and named Vsensor
int analogValue; // creates reference variable for last defined int
int led = D7; // onboard led at pin d7 given the name led

void setup() {

pinMode(Vsensor, INPUT); // set the a3 pin as input
pinMode(led, OUTPUT); // set the led as output

}

void loop() {
delay (4000); //voltage sensor will take reading every 4s
analogValue = analogRead(Vsensor); //names analog reading of the a3 pin

    if (analogValue!=0) { // runs as long as voltage is not 0
    
     digitalWrite(led, HIGH); // led is on
     Particle.publish("voltage detected", String(Vsensor),PUBLIC); //sends the event + data to particle console
     
    } else  { // runs if voltage is exactly 0
        
     digitalWrite(led, LOW);// led is off
     Particle.publish("No Voltage",String(Vsensor==0), PUBLIC);//sends event + data to particle console

    }
}

Argon #1 (Photoresistor/Servo)

C/C++
int pos = 0; // creates a postition variable for the last item coded, the servo
int motion = 0;
int turnon = 0;

void setup() { 

    Particle.subscribe("lights on", lightfunc , MY_DEVICES); //when the phrase "lights have been left on" occurs in the particle cloud, this will run the function
   
    Particle.subscribe("motion sensed", motionfunc , MY_DEVICES);

    myservo1.attach(D2); //tells the pwm servo wire to send instructions to servo thru d2. 

  pinMode(led1, OUTPUT); //plug in led is an output, meaning a5 is an output
  pinMode(led2, OUTPUT); //onboard led is an output, meaning d7 is an output
  pinMode(D2, OUTPUT);
  pinMode(photoresistor, INPUT); //photoresistor is an input, meaning a0 is an input
  
}

void loop() {
    delay(1000); //delays entire loop 1 sec to give particle publish time
    analogValue = analogRead(photoresistor);
   
        if( analogValue>20 && (motion == 1) ) { //when the lights are on, servo will move and push switch to off, then reset until lights are on again
         digitalWrite(led1, HIGH);// plug in led is on
         
         Particle.publish("lights on", PUBLIC); //sends the event to particle app
         motion = (motion - 1);
         
         } else{   //if the lights are detected as off, then nothing will happen
        digitalWrite(led1, LOW); //plug in led light is off
        
        Particle.publish("lights off", PUBLIC);  //send the event to the particle cloud
    }

    }

void motionfunc(const char *event, const char *data){
    motion = (motion + 1);
    
}

int lightfunc(const char *topic, const char *data){
        
         digitalWrite(led2,HIGH); // on board d7 light will blink on
         myservo1.write(pos+45); //servo will go to the current pos + 45 degrees
         delay(2000); //2 sec delay to let servo move
         myservo1.write(pos-45); //servo will go to the current pos -45 degrees, servo pos is reset
         delay(2000); //2 sec delay to let servo move
         digitalWrite(led2, LOW); // on board d7 light will blink off
         return 1; //returns low, 1 would be high
         
}

Credits

Slade Garraghty
1 project • 1 follower
Contact
Alex Baltos
0 projects • 1 follower
Contact
Bahaa Eddine Antar
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.