Lilly Moore
Published © LGPL

Lane Tech HS - PCL - <Lamp powered with Photocell and Relay>

This project controls the power going through an extension cord into a lamp using surrounding light.

IntermediateFull instructions provided10 hours1,120
Lane Tech HS - PCL - <Lamp powered with Photocell and Relay>

Things used in this project

Hardware components

Relay (generic)
×1
Photon
Particle Photon
×1
Extension Cord
×1
Lamp
×1
Resistor 10k ohm
Resistor 10k ohm
×1
SparkFun Photocell
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1

Software apps and online services

Particle
Blynk
Blynk

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Exacto Knife
Wire Cutter

Story

Read more

Schematics

Tinkercad's Relay

Code

This is the code to go with the Tinkercad Schematic

Code

Project's code

Java
This code includes the relay as well as the photocell.
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_PRINT Serial
char auth[] = "YOUR_AUTH_CODE_HERE";

const float VCC = 4.98;
const float DARK_THRESHOLD = 1500; //If your photon reads less than this it should turn on.
const int LIGHT_PIN = A0;
float lightV;
int lightADC;
int pinValue;
 //NO -> LOW is on


void setup()
{
  Serial.begin(9600); //lets you check the voltage number
  pinMode(LIGHT_PIN, INPUT); //LIGHT_PIN is the photocell
  pinMode(D6, OUTPUT); //D6 is the relay
  digitalWrite(D6, HIGH); //D6 is automatically set to LOW, but this turns the relay on and we want the relay off at first.
  pinValue = 0; //Sets the VirtualPin to zero
  Blynk.begin(auth);
}

BLYNK_WRITE(V0) // V0 is the number of Virtual Pin  
{
    pinValue = param.asInt(); //This grabs the value of the virtual pin
}


void loop()
{
    Blynk.run();
    if(pinValue == 1 ) { //if the virtual pin is on
      lightADC = analogRead(LIGHT_PIN); //read the value the photocell is sending
      if (lightADC > 0)
      {
          
        lightV = (lightADC * VCC / 1023.0)*100; //Calculates voltage, then multiples by 100 to make the # easier to read
        Serial.println("Voltage: " + String(lightV) + " V"); //Displays Voltage
            if ((lightV) <= DARK_THRESHOLD) //If its dark
                digitalWrite(D6, LOW); //turn the relay and therefore the extension cord on
            else //if its not dark
                digitalWrite(D6, HIGH); //turn the relay and extension cord off
        Serial.println();
      }
    }
}

Credits

Lilly Moore

Lilly Moore

2 projects • 2 followers
Thanks to Ostin Jos.

Comments