Emergency light

Main idea:

For this assignment, I tried to build an emergency light that automatically turns on whenever there is a power outage using both an analog sensor (LDR) and a digital sensor (switch).

Components:

x1 LDR

x1 Button push switch

x2 10K resistors

x1 350 resistor

x7 wires

View post on imgur.com

Process:

When the photoresistor’s surface (LDR) receives enough luminosity (light), its resistance decreases and is read by the program. When the values go lower than 350, it means that there was a power outage, therefore, the program turns the LED on. If the values remain higher than 350, the LED stays off and no power outage is detected.

Global variables:

I am using four global variables (3 are of data type int for the pins, and one boolean variable to check the state of the LED).

int led = 3; // LED pin 
int ldr = A0; // LDR pin 
int button = 5; // Button pin 
bool on = false; // Boolean variable for the state of the LED
Setup():

In the setup function, I mainly set the components to the pins (LED to 3, switch to 5, and LDR to A0) and set Serial.begin() to 9600.

void setup() { 
   Serial.begin(9600); 
   pinMode(led, OUTPUT); // Set the LED pinmode 
   pinMode(ldr, INPUT); // Set the LDR pinmode 
   pinMode(button, INPUT); // Set the button pinmode 
}
Loop():

To achieve the desired result, I am using analogRead() to get the luminosity’s value, then check if it is higher or lower than 350 using if/else conditions. 

// read the brightness from the ldr 
int brightness = analogRead(ldr);
// if its dark (power outage) 
if (brightness < 350) { 
   digitalWrite(led, HIGH); 
} 
// if everything is normal (light) 
else { 
   digitalWrite(led, LOW); 
}

The switch here works as an ON/OFF button for the whole process. I am using a boolean variable as well as digitalRead() to check the state of the LED.

// check if switch is clicked 
if (on == false && digitalRead(button)== HIGH){     
    on = true; 
} 
if (on == true && digitalRead(button) == HIGH){ 
   on = false; 
}

Challenges & thoughts:

To give it a realistic look, I wanted to add a delay() function after the click, and before the lights go on or off, but I wasn’t sure if it would affect the interactivity (mainly when the user clicks on the button). Therefore, I decided to avoid it.

Demo:

View post on imgur.com

Code:
int led = 3; // LED pin
int ldr = A0; // LDR pin
int button = 5; // Button pin
bool on = false; // Boolean variable for the state of the LED


void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT); // Set the LED pinmode
  pinMode(ldr, INPUT); // Set the LDR pinmode
  pinMode(button, INPUT); // Set the button pinmode
}

void loop() {
// read the brightness from the ldr
  int brightness = analogRead(ldr);
// check if switch is clicked
  if (on == false && digitalRead(button)== HIGH){
    on = true;
  }
  if (on == true && digitalRead(button) == HIGH){
    on = false;
  }
  // if switch is clicked, then proceed 
  if (on==true){
// if its dark (power outage)
    if (brightness < 350) {
      digitalWrite(led, HIGH);
    }
// if everything is normal (light)
    else {
      digitalWrite(led, LOW);
    }
  }
  else if (on==false) {digitalWrite(led, LOW);}
}

 

Leave a Reply