Go Down

Topic: How to: ligth measurement with LDR? (Read 24656 times) previous topic - next topic


Oct 11, 2009, 08:50 pm Last Edit: Oct 12, 2009, 05:49 pm by madepablo Reason: 1

I have a LDR connected to analog pin, using a 10k resistance, following this example: http://www.ladyada.net/learn/sensors/cds.html: example used: Simple code for analog light measurements.

However, i would like to obtain light measurements in Lux units, such as these example: http://www.emant.com/316002.page

However, i am not sure how to develop correctly the the equation to derive Lux, because i am not sure what is reading the arduino from pin 0.

I know that LDR is not the better component to measure lux, but it is enough for my project.



Oct 12, 2009, 12:15 pm Last Edit: Oct 12, 2009, 12:16 pm by Grumpy_Mike Reason: 1
because i am not sure what is reading the arduino from pin 0.

It is simply the voltage input referenced to 5V expressed as a 10 bit value.

In other words 5V gets split up into 1024 levels, that is 5/1024 = 0.0048828125 volts per unit returned back.
So suppose you read a value of 412 that is the same as:-

0.0048828125 * 412 = 2.01171875 volts

The absolute accuracy of this depends on the reference voltage which is the same voltage as is driving your arduino.



Perfectly clear!
Just only one question may be stupid. This is also true for the digital pins? I mean, the 1024 levels.



What i am doing bad? :-[

I was trying to calculate some lux values, but i have wrong results. I will explain what i am doing:

I assume that for most of the photocells: R(light) = 500/ Lux (KOhms)
On the other hand,  Vcc x R(light) = Vo x (R(light) + R)
Vcc = 5V input of Arduino
Vo = Arduino analogue lecture in Volts (ej., a lecture of 412 is equal to 2.01171875 volts, as previously explained Mike in the previous reply to this post)
R = 3.3 KOhms (located in the circuit in the way to the LDR)

Then, following the equation proposed here: http://www.emant.com/316002.page, the Light measurement (in lux units) are obtained from this equation:
Lux = (2500/Vo - 500)/3.3

When i apply it to the results obtained by the circuit, i obtain a decreasing lux when i obtain a higher lecture from Arduino on pin analog 0, what it is completely wrong according to the theory of LDR sensors.  :(

So i don´t know what i am doing wrong in my calculations. I repeated it also in Excell to be sure that i was not doing it wrong by hand or with the calculator...



i obtain a decreasing lux when i obtain a higher lecture from Arduino on pin analog 0

You have the LDR in a potential divider with it in the lower half of the divider. The more light the lower the resistance and so the low the voltage you measure.

This is also true for the digital pins? I mean, the 1024 levels.

No a digital pin will read either high or low only 2 levels.


Oct 13, 2009, 12:18 pm Last Edit: Oct 13, 2009, 12:20 pm by madepablo Reason: 1
Thanks Mike,

You have the LDR in a potential divider with it in the lower half of the divider. The more light the lower the resistance and so the low the voltage you measure.

But i am not sure of your conclusion, because it is not what i really observed. When i have the LDR under the sunlight, the measurement from LDR on pin 0 is higher than when i have it inside a dark room.
It means that under brighter conditions ( 8-) higher Lux) the out voltage is higher, because the resistance of the LDR is lower. Right?

So, if what i say is true, the problem should be in the equation, because i obtained opposite results. May be the equation is not correct (?).
Lux = ((2500/Vo) -500)/3.3



Sorry I thought you had it in the bottom leg. If it is in the top leg then the voltage will go up with more light.

Point is that the voltage you see is as a result of the light dependant resistor being in a potential divider circuit with the other resistor being fixed. So that that to be taken account of in the equation.
I make it:-
((Rf * Vt) - (Vm*Rf)) / Vm = RL
Rf(Vt - Vm) / Vm = RL

Where RL is the resistance of the light dependant resistor
Vt is the total voltage across the divider in this case 5v
Rf is the resistance of the fixed resistor.
Vm is the voltage you measure.

Then you can apply the relationship between the RL value and the Lux reading. (is that linear by the way?)


Thanks so much!  :)

I am sorry for the simple problem. This is because i am completely new in electronics and also in arduino. I was checking some webpages, and now i understand what you mean with up and down in the potential divider....  depends on the relative location of the LDR and the other resistance in the circuit.  ::)

So, the problem was that i was using the incorrect equation for the circuit (or viceversa). :(  And now the lux increase with the increasement of the measurement in pin analog 0.

(is that linear by the way?)

I think that it is not linear. The calculations that i did for test the equation showed to me that it looks like an exponential law. I will try this night to draw a plot on Excel and show here the result.

Thanks so so much Mike for your great help!!


I made some calculations by excel (i am sorry, i don´t know how to upload  an image or picture), and it is mostly linear between the main values.

In general the lux increase quickly at lower and higher values, and more slowly in the main part of the values form 0 to 1024. Depending of the resistance that you could use (for example 1, 10 and 100 kOhms) you have a more sentitive circuit to low, medium and high values of Lux.

If you want the excel file with the calculation and plots, send me a private message with your email.


Oct 19, 2009, 10:54 am Last Edit: Oct 19, 2009, 10:57 am by madepablo Reason: 1
Here is the code that i used to obtain light value in Lux units:

Code: [Select]
/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground

For more information see www.ladyada.net/learn/sensors/cds.html
Modified by M.A. de Pablo. October 18, 2009.
Thanks to Grumpy_Mike for equations improvement.

int photocellPin0 = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading0;     // the analog reading from the analog resistor divider
float Res0=10.0;              // Resistance in the circuit of sensor 0 (KOhms)
// depending of the Resistance used, you could measure better at dark or at bright conditions.
// you could use a double circuit (using other LDR connected to analog pin 1) to have fun testing the sensors.
// Change the value of Res0 depending of what you use in the circuit

void setup(void) {
 // We'll send debugging information via the Serial monitor

void loop(void) {
 photocellReading0 = analogRead(photocellPin0);   // Read the analogue pin
 float Vout0=photocellReading0*0.0048828125;      // calculate the voltage
 int lux0=500/(Res0*((5-Vout0)/Vout0));           // calculate the Lux
 Serial.print("Luminosidad 0: ");                 // Print the measurement (in Lux units) in the screen
 Serial.print(" Lux\t");
 Serial.print("Voltage: ");                       // Print the calculated voltage returned to pin 0
 Serial.print(" Volts\t");
 Serial.print("Output: ");
 Serial.print(photocellReading0);               // Print the measured level at pin 0
 Serial.print("Ligth conditions: ");            // Print an approach to ligth conditions
 if (photocellReading0 < 10) {
   Serial.println(" - Dark");
 } else if (photocellReading0 < 200) {
   Serial.println(" - Dim");
 } else if (photocellReading0 < 500) {
   Serial.println(" - Light");
 } else if (photocellReading0 < 800) {
   Serial.println(" - Bright");
 } else {
   Serial.println(" - Very bright");

I hope it could be useful to somebody else.

I will improve it soon, showing the light conditions depending of the Lux value, what made it to have a more realistic results by the use of different resistances used in the circuit. Now they are based on 10KOhms resistance.


nice project..
i was looking for an Equation to relate the LUX to the resistance of the LDR but the given formula above looks not correct as you said.


There is an equation for thermistors relating temperature to resistance.  Is there a similar equation for and LDR?  This would really help your calculation.  If not, I suppose you could take measurements and then do a curve fit.  If you're only going to expose this to a limited range, assuming the linear part, you could take measurements using your Arduino and use the Map function.  Maybe one of those options will be helpful.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131