Arduino with LDR
Photocell
​
A photocell (sometimes called Light Dependent Resistor LDR) is a device that is used to detect and measure light. It is a resistor that changes resistance depending on the amount of light incident on it. There are used in varieties of application such as a switching OFF light during the day and turning them back ON during the night, as we will see below:
Schematic
​
Required components
-
Arduino UNO R3
-
Solderless breadboard
-
Photocell
-
Resistor (220, 10K)
-
Jumper wires
Sketch
​
int ldr = A0; //set A0 as Analog Input pin for LDR.
int value = 0;
​
void setup(){
Serial.begin(9600);
}
​
void loop(){
value = analogRead(ldr) //read value from LDR.
Serial.print("LDR values: ");
Serial.println(value);
​
The above sketch will display the analog LDR values on Serial monitor. Result sample is as shown below:
LDR values: 126
​
​
Control LED based on light intensity detected by LDR
​
int ldr = A0; //set A0 as Analog Input pin for LDR.
int value = 0;
​
void setup(){
Serial.begin(9600);
pinMode(3, OUTPUT); //set pin3 as output for LED.
}
​
void loop(){
value = analogRead(ldr) //read value from LDR.
if(value<300){
digitalWrite(3, HIGH); //turn on LED.
}
else{
digitalWrite(3, LOW); //turn off LED.
}
}
The above sketch will turn on an LED when LDR value<300 (low light). This can be used in street light to automatically turn on when dark.
​
This can be used to automatically turn on street light when it's dark or in cell phones to adjust screen brightness depending on the light intensity of the environment, etc.