Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Huan TranPeter Yeung
Published

Auto Light

No more flipping the switch, have your light automatically turn on when you need it. IOT project for MEGR 3171 - UNCC

BeginnerFull instructions provided1 hour959
Auto Light

Things used in this project

Hardware components

Photon
Particle Photon
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×10
Breadboard (generic)
Breadboard (generic)
×2

Software apps and online services

ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
wire cutter
Hand drill

Story

Read more

Schematics

Photon 2 schematic (fritzing file)

photon 2 schematic

Photon 1 schematic

This photon uses the same schematic as the 2nd photon, except without the servo motor attached.

Code

Photon codes

Arduino
The first code is for the 1st photon with PIR sensor only. Whenever the motion sensor is activated, the sensor will output a voltage value of around (3.3V). The code will compare this output to "3000" (equivalent to around 2.4V) to make sure no stray signal falsely activate the code. Since the sensor will always output a 3.3V upon motion detection, the code will always run when motion is detected. The code will publish an event "lightswitch" which the 2nd photon will use as a signal to run its own code and turn on the light. Then the 1st photon will go into a delay, which should be set to be the same for both photons.

The second set of code is the code for the 2nd photon connected to the servo motor. This code will be looking for even published by the 1st photon. If there is an event, it will run the code turn the servo to the on position and publish a data value of "1" to thingspeak. An addition event will also be published by the 2nd photon which the 1st photon will use to put itself into a delay to keep both photons in sync. The 2nd photon will also go into a delay, then will run the code to turn the servo to the original position to turn the switch off. The delay on both photon should also be kept the same to keep them in sync.

The servo in this code is attached to pin D3 but it can also be attached to any D pin. The same is for the analog read pin A3, which can be any A pin.
//code for 1st photon - PIR sensor only

int in=A3; //analog read pin
int compare=3000; //value to compare PIR output to

  

void setup() {
  
pinMode(in, INPUT);
Particle.subscribe("sensortop", myHandler); //subscribe to 2nd photon

}

void loop() {
analogRead(in);


 if (analogRead(in) > compare) 
    { Particle.publish("lightswitch","on");
    delay(600000); 
    Particle.publish("lightswitch","off");}
     
    
   
    
}

void myHandler(const char *sensortop, const char *data) {
    if (strcmp(data,"on1")==0)  {delay(600000); 
    } //goes into a delay when the 2nd photon's sensor is activated





////////////////////////////////////////////////////////////////////////////////





//code for 2nd photon - connected to servo and light switch

int in=A3; //analog read pin
int compare=3000; // value to compare PIR output to
Servo myservo;
int pos1=25; //servo position for light switch off
int pos2=5; //servo position for light switch on




void setup() {

pinMode(in, INPUT);
myservo.attach(D3); //attatch servo pin to D3
Particle.subscribe("lightswitch", myHandler); //subscribe to 1st photon event
}

void loop() {

analogRead(in); //read voltage of A3 for PIR output



 if (analogRead(in) > compare) { 
    myservo.write(pos1); 
    Particle.publish("sensortop", "on1"); // publish event to 1st photon
    Particle.publish("light", "{ \"1\": \""  +String(1)+  "\", \"k\": \"WRITE_KEY\" }", 60, PRIVATE);  //send data to thingspeak
    delay(600000);   
    myservo.write(pos2);
    Particle.publish("sensortop", "off1") // publish event to 1st photon
    }
     
    
    
}

void myHandler(const char *lightswitch, const char *data) 
{
    if (strcmp(data,"on")==0) {myservo.write(pos1); 
    Particle.publish("light", "{ \"1\": \""  +String(1)+  "\", \"k\": \"WRITE_KEY\" }", 60, PRIVATE);  //send data to thingspeak
    delay(600000);   
    myservo.write(pos2); 
    }
  
  
    
  }

Credits

Huan Tran
1 project • 0 followers
Contact
Peter Yeung
1 project • 1 follower
Mechanical Engineer
Contact

Comments

Please log in or sign up to comment.