How to Use a Light Dependent Resistor (LDR)

165,513

62

31

About: Technologist, Electronic Engineer, sometime Coderdojo mentor.

Lots of projects use a Light Dependent Resistor (LDR) to sense light levels but don't really focus on how they work.

This project shows you how to use a Light Dependent Resistor (LDR) to sense light levels, measure those levels with the Arduino and print the measurements to the Serial port.

Arduino measures Voltages on pins A0 to A5; but the LDR is a variable resistor (varying with Light). So we need to convert the varying resistance to a voltage that the Arduino can measure.

We do that by using the LDR and a Resistor in a Potential Divider circuit.

==========

By the way. If you like this Instructable, you might also like to read My Blog covering various projects and Tutorials.

====

Step 1: How It Works

The top of the Potential Divider is 5V; the bottom is at 0V and the middle (connected to A0) is some value between 5V and 0V that varies as the LDR resistance varies according to the light level.

As both resistors are in series the same current must flow through them both. The LDR resistance drops with light, which causes the current in both resistors to increase (I=V/R), and therefore the voltage across the other (non-LDR) increases.

So as the LDR resistance varies with Light, the Voltage at A0 will too.

Technically you can work out the Voltage on A0 as :

Va0 = 5 * R1/(R1+R2)

where Va0 is the voltage at A0 pin, R2 is the top resistor value, R1 is the bottom resistor value;

e.g. R1 = 10k, R2 = 5k => Va0 = 5 * 10000/(10000 + 5000) = 5 * 10/15 = 3.33V

The LDR has a high value when no light is present. The value of resistance of the LDR depends on the type. In this case it's about 10k. As the light level increases the resistance drops, which makes the current increase (by Ohm's Law), which in turn, makes the voltage at A0 (Va0) increase.

Electrically this is what's happening (skip this if you're not interested in this): The LDR and resistor are in series with the applied voltage (5V), so the current flowing through them is the same (the A0 pin draws virtually zero current). So the current through the resistor is (by Ohm's Law):

I = 5 / (R1+R2)

Now, the voltage across the resistor is applied to A0. Again, by Ohm's Law that is:

Va0 = I * R1

Substituting the equation for I back in to this equation we get:

Va0 = 5 * R1/(R1+R2)


See what it looks like on a Breadboard.

Step 2: The Code

You can use the AnalogInOutSerial code (File -> Examples -> Analog -> AnalogInOutSerial) to measure a value on A0 and print it to the Serial Monitor.


Step 3: How to Make It Better

  1. The AnalogInOutSerial code also uses the measured sensor value to change the brightness of an LED on pin 9. Add the LED.
  2. Make an Arduino Light Sensitive Bar Graph.
  3. Graph the light readings on your PC. See How to Send Data from Arduino to a PC

Share

Recommendations

  • Plastics Contest

    Plastics Contest
  • Optics Contest

    Optics Contest
  • Make it Glow Contest 2018

    Make it Glow Contest 2018

31 Discussions

0
None
RashikaJ

10 months ago

Can you tell me what is the analog output obtained by the LDR module? The unit of the quantity obtained as the analog output visible in the serial monitor of the Arduino

0
None
NounN

1 year ago

what is the role of resistor ? how i can select it ? why 10k ?

1 reply
0
None
akellyirlNounN

Reply 1 year ago

Because it's about the same value as the LDR's resistance. See my description about how it works.

0
None
MaheshA25

1 year ago

i need a cood

0
None
sanglap patra

1 year ago

i made my ldr project but values are fructuating in a constant condition can anyone tell me why?

0
None
JasonC302RayC23

Reply 2 years ago

I would imagine you could use multiple ldr's (inside varying lengths of tube, mentioned above) at different permanent angles, and check the amount of light (voltage to ldr) at the different sun positions if you kept the device in a fixed location. Or also if you didn't use multiple ldr, you could use one tube, and pre messure the light amounts during different angles of sun, and reference those values in your code. I guess it would work more like a digital sundial, but for sun angle. I think a device that actually determined the sun's angle at random geographic locations would have a lot more brainstorming...

0
None
luoXRayC23

Reply 2 years ago

it's not that easy. ldr only measure the power of light , not the angle to sun.

0
None
Tanmay Deuskar

2 years ago

Hello,

Why do you need to connect a resistor to the LDR? Can you connect it directly between +5v and A0 without the resistors?

7 replies
0
None
SeBsZTanmay Deuskar

Reply 2 years ago

Hi tanmay, you need the resistor in order to create a so-called voltage divider. Basically you are turning the varying resistance of the LDR into a varying voltage at the midpoint (between the LDR and the resistor). This can then be easily measured by any ADC (Analog to digital converter).

If you don't put a resistor to ground and connect it directly between +5V and A0 then the ADC will not measure anything that is related to the LDR's resistance. All that's happening is a varying current enters the A0 pin which can not be measured like this. ADCs can only measure voltages directly.

0
None
Dram229SeBsZ

Reply 2 years ago

Good answer. Now it makes sense. Thanks.

0
None
CharlieT36SeBsZ

Reply 2 years ago

"The top of the Potential Divider is 5V; the bottom is at 0V and the
middle (connected to A0) is some value between 5V and 0V that varies as
the LDR resistance varies. Remember the LDR resistance varies with Light
so the Voltage at A0 will too."

-The article already states that as the LDR resistance varies, the voltage at A0 will vary too. If the voltage already varies with the varying resistance of the LDR, why do we need the 2nd resistor to vary the voltage if that's already happening without it??

0
None
akellyirlCharlieT36

Reply 2 years ago

Both reistors are required to make the voltage change. Read on to this section which explains what's going on:

"As the light level increases the resistance drops, which makes the
current increase (by Ohm's Law), which in turn, makes the voltage at A0
(Va0) increase."

As both resistors are in series the same current must flow through them both. The LDR resistance drops with light, which causes the current in both resistors to increase (I=V/R), and therefore the voltage across the other (non-LDR) increases.

0
None
CharlieT36akellyirl

Reply 2 years ago

Thanks! I sent you a longer private message in response.

0
None
CharlieT36akellyirl

Reply 2 years ago

Thanks! I sent you a longer private message in response.

0
None
CharlieT36akellyirl

Reply 2 years ago

Thanks! I sent you a longer private message in response.

0
None
Hajra Naeem

2 years ago

hy.plz tell me how to shield LDR from stray light.i am working on burglar alarm using LDR and lazer...i want to make ldr operative just with lazer light.plz plz reply me soon

1 reply
0
None
akellyirlHajra Naeem

Reply 2 years ago

Put the LDR in a tube. The tube will shield the LDR from stray light so only the laser light will fall on it. SImple as that :-)

0
None
JanetA20

2 years ago

hey how do you use an ldr to create a robot that can follow light

1 reply
0
None
akellyirlJanetA20

Reply 2 years ago

You would need 2 LDRs separated by about 5 to10cm. Program the robot to turn in the direction of the greatest amount of light (i.e. left LDR or right LDR)