LDR(Light Dependent Resistor) is the component that is used to sense the light. Its resistance varies with the intensity of light. The resistance of LDR ranges from 5K ohm in bright to 20M ohm in the dark.
The arduino is used simply to display the value of light sensed using ANALOGREAD command. As LDR is a variable resistor, to avoid shorting between +5v and Gnd, a voltage divider is used.
Circuit:
Programming the arduino:
int LDR=A0;
int x;
int sense=100;
int led=8;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
x=analogRead(LDR);
Serial.print(x);
if(x<sense)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
Serial.print(" ");
delay(500);
}