LDR -> Arduino -> LED

Objective:

Connect LDR to Arduino and switch an LED ON/OFF based on the out from LDR

Working of LDR

Whenever there is a change in intensity of light to which our LDR is exposed there will be a change is resistance within our LDR, higher the intensity of light, lower the resistance and vice-versa.

Resources needed

SNo Component Quantity
1 Arduino uno 1
2 Light Dependent Resistor 1
3 LED 1
4 1K resistor 2
5 Jumper wire 5
4 Breadboard 1

Fritzing:

cod screen shot

Code:

int x;
void setup() {
pinMode(4, OUTPUT);
}
void loop() {
x = analogRead(A0);
if(x<300){
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, LOW);
}
delay(1000);
}

Below is the copy of working code

cod screen shot

Code Explanation

We setup our digital pin 4 as output pin under our void setup() function.Our LED is connected to digital pin 4. Under our void loop() function we read LDR's analog values using analogRead(); function on our integer variable x, we choose a threshold limit for reading from LDR, in my case 300, if LDR values goes below the threshold I turn ON the LED, else I turn OFF the LED.