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.

  • 3,381 views
  • 2 comments
  • 11 respects

Components and supplies

Ph a000066 iso (1) ztbmubhmho
Arduino UNO
×1
Mcp23008 i2cio4r4g5le 10a d 1 1024x1024 s8wwqgnv5c
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
25ac5060 40
PIR Sensor, 7 m
×1
4767222
LDR, 5 Mohm
×1
12002 04
Breadboard (generic)
×1
09670 01
SparkFun Dual H-Bridge motor drivers L298
For the future irrigation system ( ignore it for now)
×1
13637 01
SparkFun Soil Moisture Sensor (with Screw Terminals)
same as the motor module
×1

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

  • 6,280 views
  • 7 comments
  • 16 respects

Motion Activated Night Light

Project tutorial by Haziq Afandi

  • 6,060 views
  • 0 comments
  • 6 respects

RobotGeek PIR Motion Sensor Night Light

Project tutorial by Team RobotGeek Projects Team

  • 2,359 views
  • 1 comment
  • 6 respects

MOSMusic

Project tutorial by Victor Altamirano and EdOliver

  • 3,974 views
  • 10 comments
  • 17 respects

Bluetooth Controlled Home Light

Project showcase by Khaled Md. Saifullah and Shahadat Hossain Afridi

  • 2,937 views
  • 2 comments
  • 11 respects

The "Tennis Ball" Garage Stop Light

Project tutorial by Team Stuart Mace

  • 10,685 views
  • 2 comments
  • 37 respects
Add projectSign up / Login