Sunday, February 23, 2014

Project3 | Light Detector




Your children or your brothers or sisters having fear of darkness! Make a night light that automatically goes on when you turn off the lights. by using a light sensor, some wires, and LEDs you can easily create an automatic night light that will turn on when you turn off the lights.



Configuration



By following the diagram, you can notice the following:

The photocell (light sensor) is connected via there wires:
(1)Blue and black wires to one of photocell legs and (2) red wire to the other leg. A resistance is added along the photocell connected to ground (black wire). These wires are for:
·         Black wire à ground
·         Red wire à 5V
·         Blue wire à analog input A0 ( because photocell give analog data)

Code


1.       Some required definitions, which PIN the LEDs and photocell is connected to?
int LDR = 0;   //analog pin = A0
int LED1=3;
int LED2=4;
a.       Define light sensitivity, the threshold we want to control lights according to it, if the photocell senses a light less than this threshold, then turn LEDs on
int light_sensitivity1 = 300;
int light_sensitivity2 = 50;
b.      Define a variable to store the value which photocell senses
int LDRValue = 0;

2.       Set the LEDs pins to output mode, open the serial (optional) for output on screen
Serial.begin(9600);               
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

3.       Start listen to the photocell, put what the sensor reads to LDRValue
LDRValue = analogRead(LDR);              Serial.println(LDRValue);  // cout << LDRValue;
delay(50);                                               

4.       We will turn on the first LED, if the value read by the sensor less than the first light sensitivity (i.e. 300), the second will turn on, only when the LDRValue is less than the second light sensitivity ( more darkness)
if (LDRValue < light_sensitivity1){
     digitalWrite(LED1, HIGH);
     
     if (LDRValue < light_sensitivity2) 
           digitalWrite(LED2, HIGH);
     else
           digitalWrite(LED2, LOW);
}else{
        digitalWrite(LED1, LOW);
      }

No comments:

Post a Comment