How to Use LDR in Your Arduino

In this article you will learn how to use an LDR in your Arduino .

LDRs

A Light Dependent Resistor (LDR) is a variable resistor whose resistance decreases depending on light intensity.



Figure 1: Light Dependent Register

Basic Idea

I don't know how many of you are aware of this, but a LDR is one of the most fundamental of electronic equipment that is used to absorb light. So, why are so concerned about absorbing light?

The answer is quite simple. It will absorb light and that actually means it detects light. So, whenever there is no light around a LDR it has high resistance causing no flow of current. Whereas in contrast, when there is light it has low resistance. Basically, a LDR is used to detect the presence of light.

Background

We will create a model that detects light in your room and it will turn on a LED when there is light and vice-versa.

Here are the components we will need:

  • Arduino Uno or Nano
  • Red LED
  • Few Jumper Wires
  • LDR
  • 1kOhms Resistor

Connection Layout

It would be like the following.



Figure 2: Connection Layout

Here, we are connecting the LED to digital PIN 12 and the LDR to Analog PIN 0. Of the rest, the second lead of the LDR is the Vcc voltage PIN. For safety purposes, we use a resistance to control the potential change.

Skech

  1. int LDR = 0; // Analog 0  
  2. int LED = 12; // Digital 11  
  3.   
  4. void setup()  
  5. {  
  6.     Serial.begin(9600);  
  7.     pinMode(LED, OUTPUT);  
  8.     //pinMode(LDR, INPUT);  
  9.   
  10. }  
  1. void loop()  
  2. {  
  3.   
  4.     Serial.println(analogRead(LDR));  
  5.   
  6.     if (analogRead(LDR) > 300)  
  7.     {  
  8.         Serial.println("LIGHT ON");  
  9.         digitalWrite(LED, HIGH);  
  10.     }   
  11.     else  
  12.     {  
  13.         Serial.println("LIGHT DOWN");  
  14.         digitalWrite(LED, LOW);  
  15.     }  
  16.   
  17.     delay(1000);  
  18. }  
Note: You can change the condition value if you want to light it up in low intensity.

Explanation

Since an LDR is an analog component, it absorbs light and converts it into an analog value that lies between 0-255. And in the Arduino library we have the analogRead() method that reads the analog value from an analog component.

Here, the LDR is only an analog device that is connected to PIN A0. In the very first line of the loop() block we will print the value in the Serial Monitor. And if the analog value is greater than 300 then the LED will light up else put it in the LOW state, in other words OFF.

And, last we maintained a delay of 1 seconds.



Figure 3: LDR

Conclusion

An LDR must be a small component, but it is frequently used in projects. Most street lights and light-sensitive devices use this component to detect the presence of light.