still under construction, but feel free to explore :)
HOME / Articles/Tutorials / Electrical / Electronics Articles / Arduino Board Articles / Arduino General Articles / Arduino LDR

Arduino LDR

Example of using light dependent resistor (LDR) or photoresistor with Arduino.




For specific and categorized examples use the appropriate link in the previous page

What is LDR

A photo-resistor or light dependent resistor (LDR) is a resistor whose resistance decreases with increasing incident light intensity i.e. exhibits photo conductivity. A photo-resistor is made of a high resistance semiconductor. If light falling on the device is of high enough frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electron (and its hole partner) conduct electricity, thereby lowering resistance. more info for that on: http://en.wikipedia.org/wiki/Photoresistor.
LDR is quite cheap and not that precise to measure LUX, but can be used as a simple trigger for light change. For more accurate results you may want to get high sensitivity light sensor(BH1750FVI, TEMT6000, TSLXXX, NOAXXX, LA01XXX, LV01XXX)

How do we connect it to the Arduino

We simply need to measure the change of the resistance of our LDR. To do that will use a voltage divider (also known as a potential divider) is a linear circuit that produces an output voltage (Vout) that is a fraction of its input voltage (Vin). In this diagram you see R1 is the LDR and R2 is a fixed resistor. You can use whatever fixed resistance you have (1k-100k) and/or change the positions of the resistors but you have consider the following. Vout = Vin * (R2/(R1+R2)) Where Vout is the analogue output pin 0 (A0) and Vin is the 5V pin. So what does this mean:

Position

If you change the position from the one on the diagram you will get your LDR as R2 which means that you will get saturation close to 512(2.5V) when it is very light, unless if you get your R1 impedance very high and in that case you will not get accurate result when not so much light is applied. As shown in the scheme: the more light -> the less resistance -> the higher Vout(A0) reading. Similarly as in the previous case if you put very high resistance for fixed you will get saturation when very light.

Fixed resistance

This value really depends on the specs of the LDR and the how you want to use it. Lets say you LDR spec are 1K very dark 200k very light. This is vast range so if you choose 1K fixed resistance the readings will start around 0 when it is dark but will saturate to 2.5V as out R1 cannot get lass than 1k.

If whe use 200k for fixed we will have a problem as we will get values from 5V when it is light until it is extremely dark and then it'll be 2.5V.

So naturally we have to define what light are we going to get and is important for us, For similar type of LDR for normal room light detection i use fixed resistance ~20K

 

The code

this is a simple code that reads the value for A0 and displays it in the serial monitor every second.
int sensorValue = 0; 

void setup()
{
  Serial.begin(9600); 
}

void loop()
{
      Serial.print("\nsensor = " );    
       sensorValue = analogRead(A0);  
        Serial.print(sensorValue);  
    delay(1000); 
}

Enjoy :)

Author: DjoDjo