Analog Input/Output

Idea

While I really like sunlight or natural light filtering in through my window, if I am deeply focused on a task I often forget to turn the lights on when the sun has set and this has often lead to headaches and dryness in my eyes due to eye strain when working on my laptop in the dark. So I wanted to create an indicator using a light sensor and LED in which the LED starts to blink if the light in the room is very dim. The glaring red alarm LED can only be temporarily switched to a blue light when a button is pressed down because I often get lazy and don’t get up to turn on the lights. So the red light would continue to blink as long as lights are not turned on and it becomes brighter in the room.

Circuit

I created the following circuit for the light indicator. I connected the LDR with a pull down resistor of 10K Ω and in the same circuit added the red LED with its respective resistor of 330Ω. Then I connected the red LED and LDR with the blue LED through a button and following is the Arduino Uno code for the circuit:

const int BUTTON = 7; // the number of the pushbutton pin on the arduino board
int lastState = LOW; // the last state from the button
int currentState;    // the current reading from the button
const int LIGHT_SENSOR_PIN = A0; 
const int LED_PIN          = 3;  
const int LED_PIN_2          = 11;  
const int ANALOG_THRESHOLD = 500;
int Analog;

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT); 

  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  Analog = analogRead(LIGHT_SENSOR_PIN); // read the input on LDR

  currentState = digitalRead(BUTTON); //read input on button 
  if(Analog < ANALOG_THRESHOLD){
    if (currentState==HIGH){
      digitalWrite(LED_PIN, HIGH);   // turn LED on 
      delay(500);                       // wait 
      digitalWrite(LED_PIN, LOW);    // turn LED off 
      delay(500);
    }
    else{
      digitalWrite(LED_PIN_2, HIGH);   // turn LED 
      delay(500);                       // wait 
      digitalWrite(LED_PIN_2, LOW);    // turn LED off 
      delay(500);
    }    
  
  }
  else{
    digitalWrite(LED_PIN, LOW);  // turn LED off
  }


  
}

 

Improvements

For future improvements I would want to add some sort of sound alarm to it as well so that I do not ignore the indicator at all because of the noise. I would also like to add a LED that starts blinking again after a set time period of the lights are not turned on in for example 5 minutes or something similar to this.

Leave a Reply