Joaquim Silveira
Published © GPL3+

Motion and Light Dependent LEDs

Light and presence detector project to properly control LED bulbs automatically.

IntermediateWork in progress2 hours2,793
Motion and Light Dependent LEDs

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
4-CHANNEL RELAY CONTROLLER FOR I2C
ControlEverything.com 4-CHANNEL RELAY CONTROLLER FOR I2C
Mine is the JY-53, has 8 entries for 8 different lights, whatever rocks your boat
×1
PIR Sensor, 7 m
PIR Sensor, 7 m
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
For the future irrigation system ( ignore it for now)
×1
SparkFun Soil Moisture Sensor (with Screw Terminals)
SparkFun Soil Moisture Sensor (with Screw Terminals)
same as the motor module
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

01_Relay

Code

01_Relay

Arduino
const int led = 13; 
const int relay1Pin = 8;
const int relay2Pin = 9;
const int pirPin = 10;
const int ldrPin = A0;
const int switchPin = 11;


unsigned long DELAY_LIGHT = 60000; 
unsigned long DELAY_PRESENCE = 5000;
unsigned long delayStart = 0;

bool delayRunning = false; 
bool present;

int light;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(switchPin, INPUT);


  // start delay
  delayStart = millis();
  delayRunning = true;
  
  
  light = analogRead(ldrPin);
  present = digitalRead(pirPin);
  delay(500);
}

void loop() {
  
  bool switchState = digitalRead(switchPin);
  
  if(switchState){
    if (delayRunning && ((millis() - delayStart) >= DELAY_PRESENCE)) {
      delayStart += DELAY_PRESENCE;
      present = digitalRead(pirPin);
      if(present){
          DELAY_PRESENCE = 180000; // 3 minutes
      }else{
          DELAY_PRESENCE = 1000; //
      }
    }

  
    if (delayRunning && ((millis() - delayStart) >= DELAY_LIGHT)) {
      delayStart += DELAY_LIGHT;
      light = analogRead(ldrPin);
    }

    if ((light <= 270) && present) {
      digitalWrite(led, HIGH);
      digitalWrite(relay1Pin, HIGH);
      digitalWrite(relay2Pin, HIGH);
    }else{
      digitalWrite(led, LOW);
      digitalWrite(relay1Pin, LOW);
      digitalWrite(relay2Pin, LOW);
    }
  }
  
}

Credits

Joaquim Silveira

Joaquim Silveira

4 projects • 19 followers
Participated and won superteam prize in Robocup 2012 Mexico and Robocup 2014 Brasil. Theorical Robotic Olympic Champion Brasil 2010 (OBR).

Comments