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!
Omar AlnamasiMohammed Ali AL Hashel
Published © GPL3+

Door Lock with Light Sensor

Door lock with a light sensor using two Particle Photons.

IntermediateFull instructions provided5 hours1,565
Door Lock with Light Sensor

Things used in this project

Hardware components

SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Photon
Particle Photon
×2
Photo resistor
Photo resistor
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×3
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×2

Software apps and online services

Particle Pi
Particle Pi

Story

Read more

Schematics

light controller circuit

This is the Particle Photon with the photo resistor attached to it to read the ambient light values, then publish the event either "Bright" or "Dark" to the Console so the other Particle can subscribe to this event to control the servo.

servo motor circuit

This Particle Photon responds to the messages sent from the light sensing photon and controls the servo to Lock and Unlock the door.

Code

servo motor

Arduino
//ServoLightSwitch
// Connect RED wire to VIN (~5V)
// Connect ORANGE, YELLOW, or WHITE wire to A0 (servo signal)
// Connect BLACK or BROWN wire to GND (0V)
// Adjust these connections for your particular servo
// if you have a wiring diagram for it
//----------------------------------------------------------------------------
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{ Particle.subscribe("controller", servo);
  // attaches the servo on the A4 pin to the servo object
  myservo.attach(A4);

  // register the Spark function
  Spark.function("servo", updateServo);
}
int updateServo(String command)
{
  // convert string to integer
  uint8_t pos = command.toInt();
  
  
}
void servo(const char *event, const char *data)
{
    if (strcmp(data,"Bright")==0) 
  {
    // if the kitchen light is on it will send to keep the door open and the the LED light off. 
    myservo.write(20);
  }
  else if (strcmp(data,"Dark")==0) {
    // if the kitchen light is off it will send to keep the door close and the the LED light on.
    myservo.write(100);
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
}

light controller

Arduino
int led = D0;
int photoresistor = A0;
int power = A5;
int analogvalue;     

//This section of code simply assigns the pins on the Particle Photon, and names 
//variables that will be used later in the code.

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

//This section of the code is the setup for the rest of the code.  It applies power
//to the pins requiring power, or names the pin to recieve an input; in this case
//from our photo resistor.

void loop() {
    
    analogvalue = analogRead(photoresistor);
    delay(100);
    
    if (analogvalue < 60){
        Particle.publish("controller","Dark", PUBLIC);
        digitalWrite(led,HIGH);
        delay(4000);
        //delay(900000);
    }
    else {
        Particle.publish("controller","Bright", PUBLIC);
        digitalWrite(led,LOW);
        delay(4000);
        //delay(900000);
    }
    
}
    
//This loop checks the analogue value of the photoresistor and compares it to the
//average light value constant selected.  Then, depending on whether the value is
//higher or lower than the selected constant, the Particle publishes an event to the
//console, with the event reading either "Bright" or "Dark."
//The delay is in there simply to slow the iteration process of the loop so the 
//Particle does not overload the console with too many publishes per second. 

Credits

Omar Alnamasi

Omar Alnamasi

1 project • 1 follower
MEGR 3171 course
Mohammed Ali AL Hashel

Mohammed Ali AL Hashel

0 projects • 0 followers
BS Mechanical Engineering student at UNCC.

Comments