Project in progress
Motion and Light Dependent LEDs

Motion and Light Dependent LEDs © GPL3+

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

  • 2,126 views
  • 2 comments
  • 10 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

The project became necessary to me once I came back home late, leaving my dog (God bless her heart) alone for a long time. It was all dark and I felt bad. So, it is mainly for her: when she's not sleeping, the PIR picks up her movement and if it's dark, it will light up.

The main idea is not to simply implement an LDR to turn the LED bulbs on and off accordingly with brightness in the room; neither to attach a PIR sensor to turn it off and on depending on someone's presence in the room. It would simply be annoying, and pretty useless. And again, annoying… On, off, on, off, on, on, off… over and over.

However, by combining both sensors and properly using the millis function, this automation becomes fluid and useful.

I also added a physical switch to be able to disable the automation without turning the arduino off, for when you don't want the lights turning on automatically. Just switch it and reset the arduino.

I'm still working on an irrigation system, I'll update the project once it's complete, the soil humidity sensor didn't arrive yet.

Feel free to comment or to ask anything.

(If you know how to make the code more elegant, feel free to tell me in the comments, I am not much of a programmer.)

Cheers!


Code

01_RelayArduino
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);
    }
  }
  
}

Schematics

01_Relay
uploads2ftmp2f79c5790c-e70c-42a7-915e-03be21f5765a2f01_relay_j8du3qgFw6.fzz

Comments

Similar projects you might like

Automatic Motion Triggered Street Light

Project showcase by ReddSathvik

  • 3,546 views
  • 2 comments
  • 16 respects

Motion Activated Night Light

Project tutorial by Haziq Afandi

  • 5,088 views
  • 0 comments
  • 6 respects

RobotGeek PIR Motion Sensor Night Light

Project tutorial by Team RobotGeek Projects Team

  • 2,107 views
  • 1 comment
  • 6 respects

MOSMusic

Project tutorial by Victor Altamirano and EdOliver

  • 3,565 views
  • 10 comments
  • 17 respects

Bluetooth Controlled Home Light

Project showcase by Shahadat Hossain Afridi and Khaled Md. Saifullah

  • 1,807 views
  • 2 comments
  • 11 respects

The "Tennis Ball" Garage Stop Light

Project tutorial by Team Stuart Mace

  • 10,438 views
  • 2 comments
  • 36 respects
Add projectSign up / Login