Vadim
Published © LGPL

Kitchen Working Area Automatic Lighting on Arduino

Simple device that I designed for automatic control of additional LED lighting in the kitchen.

IntermediateFull instructions provided8 hours4,684
Kitchen Working Area Automatic Lighting on Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Controller Schematic Diagram

PCB Project Files

Gerber Files

Code

Code snippet #1

Arduino
The ambient light level measurement function.
void LightingChange_Det () {
  current_luminosity = analogRead(Light_Sensor);
  if ((current_luminosity - previus_luminosity > 40) && (Light_On == false)) {
    mode = 1;    //If main light On and working area light is Off;
    Mode_Set();  //Then working area light turn On (day mode, step 1)
  }
  if ((current_luminosity - previus_luminosity > 40) && (Light_On == true) && (mode == 1)) {      
    mode = 2;    //If main light On and working area light is On (day mode);
    Mode_Set();  //Then increases brightness (day mode, step 2)
  }
  else if ((current_luminosity - previus_luminosity > 40) && (Light_On == true) && (mode == 4)) { 
    mode = 5;    //If main light On and working area light is On (night mode);
    Mode_Set();  //Then increases brightness (night mode, step 2)
  }
  previus_luminosity = current_luminosity;
  if ((current_luminosity > 110) && (Light_On == true)) {                                       
    mode = 0;    //sufficient ambient lighting in the room 
    Mode_Set();  //working area lighting Turn Off  
  }
}

Code snippet #2

Arduino
Implementation of timeslots using millis() function.
if (millis() - timeout_1 >= interval_1) {     //1 s interval 
    timeout_1 = millis();                     //Light Level measuring every second 
    LightingChange_Det();                     
  }

Kitchen_Light_Controller_ENG.ino

Arduino
Arduino Controller Sketch
// Operating principle:
// The sufficient level of lighting in the kitchen and motion is detected - the local lighting does not turn on
// The level of lighting in the kitchen - the average, motion is detected - gradually increase the brightness to 20%, start the timer for 3 minutes
// If for 40 seconds before the end of 3 minutes interval movement is detected - activates the backlight at 100%, run timer for 7 minutes, light level and movement monitoring
// If there is no movement - reduce the level to 20%, and then, after 3 minutes, if the motion not detected - gradually disable the backlight. If motion is detected again, increasing to 100%
// The level of lighting in the kitchen is low (night), motion is detected - turn on the backlight for 10%, start the timer for 3 minutes
// If for 40 seconds before the end of 3 minutes interval movement is detected - increase brightness to 50%, start the timer for 7 minutes. 
// If there is no movement - slowly reduce the level to 10%, a 3-minute hold, then turn off

// LDR connected to A7, Motion sensor connected to D2, PWM output - D5

// mode=1 - gradually increase the brightness to 20%, start the timer for 3 minutes
// mode=2 - gradually increase the brightness from 20% to 100%, start the timer for 7 minutes. Motion Detection reset this 7 min timer 
// mode=3 - gradually decrease the brightness from 100% to 20% (=mode 1)
// mode=4 - gradually increase the brightness to 10%, start the timer for 3 minutes (night).
// mode=5 - gradually increase the brightness from 10% to 50%, start the timer for 7 minutes . Motion Detection reset this 7 min timer
// mode=0 - turn off

const byte Light_Sensor = 7;    //LDR
const byte Motion_Sensor = 2;   //Motion sensor
const byte PWM_Out = 5;         //PWM Out
const byte Status_LED = 13;     //At your discretion

boolean Light_On;               //true if local lighting is on , false - if local lighting is on

byte mode;                      //current operation mode

int current_luminosity, previus_luminosity, PWM_Value;

unsigned long interval_1 = 1000;                    //1 second
unsigned long interval_2 = 180000UL;                //3 min
unsigned long interval_3 = 420000UL;                //7 min
unsigned long interval_4 = 100;                     //100 ms
unsigned long checkpoint_1 = 140000UL;              //40 second before end of 3 min interval
unsigned long timeout_1;
unsigned long timeout_2;


void Mode_Set() {
  switch (mode) {
    case 0:
      for (PWM_Value; PWM_Value > -1; PWM_Value--) {
        analogWrite(PWM_Out, PWM_Value);                  //local lighting turning off 
        timeout_2 = millis();
        do {
        } while ((millis() - timeout_2) < interval_4);
      }
      Light_On = false;                          //local lighting is off
      PWM_Value = 0;
      break;

    case 1:
      for (PWM_Value = 0; PWM_Value < 51; PWM_Value++) {
        analogWrite(PWM_Out, PWM_Value);                  //gradually increase the brightness to 20%
        timeout_2 = millis();
        do {
        } while ((millis() - timeout_2) < interval_4);
      }
      Light_On = true;
      timeout_2 = millis();                     //start timer for mode 1 
      break;

    case 2:
      for (PWM_Value; PWM_Value < 254; PWM_Value++) {
        analogWrite(PWM_Out, PWM_Value);                  //gradually increase the brightness from 20% to 100%
        timeout_2 = millis();
        do {
        } while ((millis() - timeout_2) < interval_4);
      }
      Light_On = true;
      timeout_2 = millis();                     //start timer for mode 2
      break;

    case 3:
      for (PWM_Value; PWM_Value > 51; PWM_Value--) {
        analogWrite(PWM_Out, PWM_Value);                  //gradually decrease the brightness from 100 to 20%
        timeout_2 = millis();
        do {
        } while ((millis() - timeout_2) < interval_4);
      }
      Light_On = true;
      timeout_2 = millis();                     //start timer for mode 1
      mode = 1;                                 //after decreasing this is mode 1
      break;

    case 4:
      for (PWM_Value = 0; PWM_Value < 25; PWM_Value++) {
        analogWrite(PWM_Out, PWM_Value);                  //gradually increase the brightness to 10%
        timeout_2 = millis();
        do {
        } while ((millis() - timeout_2) < interval_4);
      }
      Light_On = true;
      timeout_2 = millis();                     //start timer for mode 4
      break;

    case 5:
      for (PWM_Value; PWM_Value < 130; PWM_Value++) {
        analogWrite(PWM_Out, PWM_Value);                  //gradually increase the brightness from 10% to 50%
        timeout_2 = millis();
        do {
        } while ((millis() - timeout_2) < interval_4);
      }
      Light_On = true;
      timeout_2 = millis();                     
      break;
  }
}

void LightingChange_Det () {
  current_luminosity = analogRead(Light_Sensor);
  if ((current_luminosity - previus_luminosity > 40) && (Light_On == false)) {    
    mode = 1;                                                                     //local lighting is off and turning on main Lighting
    Mode_Set();                                                                   //turn local lighting On (day mode, step 1)
  }
  if ((current_luminosity - previus_luminosity > 40) && (Light_On == true) && (mode == 1)) {
    mode = 2;                                                                     //local lighting is on (day mode) and turning on main Lighting
    Mode_Set();                                                                   //increasing brightness(day mode, step 2)
  }
  else if ((current_luminosity - previus_luminosity > 40) && (Light_On == true) && (mode == 4)) {
    mode = 5;                                                                     //local lighting is on (night mode) and turning on main Lighting
    Mode_Set();                                                                   //increasing brightness(night mode, step 2)
  }
  previus_luminosity = current_luminosity;
  if ((current_luminosity > 110) && (Light_On == true)) {
    mode = 0;                                                                     //Sufficient light level on kitchen
    Mode_Set();                                                                   //local lighting turn off 
  }
}


void setup() {
  analogReference(DEFAULT);
  pinMode(Motion_Sensor, INPUT_PULLUP);
  analogWrite(PWM_Out, 0);
  pinMode(Status_LED, OUTPUT);
  digitalWrite(Status_LED, HIGH);
  digitalWrite(Status_LED, LOW);
  //Serial.begin(9600);
  previus_luminosity = analogRead(Light_Sensor);
  current_luminosity = analogRead(Light_Sensor);
  timeout_1 = millis();
  PWM_Value = 0;
  mode = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - timeout_1 >= interval_1) {
    timeout_1 = millis();
    LightingChange_Det();
  }
  if ((digitalRead(Motion_Sensor) == HIGH) && (current_luminosity < 110) && (current_luminosity > 15) && (Light_On == false)) { //motion detected, average lighting in kitchen, local lighting is off
    mode = 1;
    Mode_Set();
  }
  if ((mode == 1) && (millis() - timeout_2 > checkpoint_1) && (digitalRead(Motion_Sensor) == HIGH)) {         //at the end of 3 min interval (40 sec)
    mode = 2;                                                                                                 //if motion is detected, set mode 2
    Mode_Set();
  }
  else if ((mode == 1) && (millis() - timeout_2 > interval_2) && (digitalRead(Motion_Sensor) == LOW)) {       //if motion not detected, set mode 0
    mode = 0;
    Mode_Set();
  }

  if ((mode == 2) && (millis() - timeout_2 > interval_3)) {                                                   
    mode = 3;                                                                                                 //set mode 3 (100%->20%)
    Mode_Set();
  }
  else if ((mode == 2) && (digitalRead(Motion_Sensor) == HIGH)) {
    timeout_2 = millis();                                                                                     //reset 7 min timer if motion is detected
  }

  if ((mode == 0) && (current_luminosity >= 0) && (current_luminosity < 15) && (digitalRead(Motion_Sensor) == HIGH)) {  //night mode, set mode 4
    mode = 4;
    Mode_Set();
  }

  if ((mode == 4) && (millis() - timeout_2 > checkpoint_1) && (digitalRead(Motion_Sensor) == HIGH)) {         //night mode: at the end of 3 min interval (40 sec)
    mode = 5;                                                                                                 //night mode: if motion is detected, set mode 5
    Mode_Set();
  }
  else if ((mode == 4) && (millis() - timeout_2 > interval_2) && (digitalRead(Motion_Sensor) == LOW)) {       //if motion not detected, set mode 0
    mode = 0;
    Mode_Set();
  }

  if ((mode == 5) && (millis() - timeout_2 > interval_3)) {                                                   
    mode = 0;
    Mode_Set();
  }
  else if ((mode == 5) && (digitalRead(Motion_Sensor) == HIGH)) {
    timeout_2 = millis();                                                                                     //reset 7 min timer if motion is detected
  }

}

Credits

Vadim

Vadim

0 projects • 4 followers

Comments