 # LDR reading to lux conversion.

Hello to all,

I just been wondering what can be wrong with my program code, as my result is returning to zero though all my other variables are giving correct values.

See parts of my code :

``````Connect LDR with a 10 Kohms resistor as shown:

Vin: 5V---*
|
\
0  LDR(16K - 2M Ohms)
/
|
|
analog 2----*
|
/
\
/   R1 (10K ohms)
\
|
*----> Ground

*/

int moisture;                // Analogical value obtained from the experiment
double PercentMoisture;      // Varible to store % Percentage Soil Moisture Value
int val;                     // Variable for reading the pin status to compare with buttonState
int val2;                    // Variable to read the debounced status
int buttonState;             // Variable to hold the last button state
int lcdMode = 0;             // Variable to swap between LCD menu.
float RLDR;                   // Resistance calculation of potential divider with LDR
float Vout;                   // voltage ouput from potential divider to Anolg input

float Lux;
int Luminosity;

float LightSensorLDR (){

RLDR = (10000.0 * (5 - Vout))/Vout;     // Equation to calculate Resistance of LDR, [R-LDR =(R1 (Vin - Vout))/ Vout]
// R1 = 10,000 Ohms , Vin = 5.0 Vdc.

Vout = (ADC * 0.0048828125);           // Vout = Output voltage from potential Divider. [Vout = ADC * (Vin / 1024)]

Lux = (500 / RLDR);

return Lux;

}

[code\]

These are some results im getting from serial monitor (Room with light ON at Night) :
LUX = 0         ADC = 63    Res LDR 0.00           Vout0.31
LUX = 0         ADC = 63    Res LDR 152539.68   Vout0.31
LUX = 0         ADC = 64    Res LDR 152539.68   Vout0.31
LUX = 0         ADC = 63    Res LDR 150000.00   Vout0.31
LUX = 0         ADC = 63    Res LDR 152539.68   Vout0.31
LUX = 0         ADC = 63    Res LDR 152539.68   Vout0.31
LUX = 0         ADC = 63    Res LDR 152539.68   Vout0.31
LUX = 0         ADC = 64    Res LDR 152539.68   Vout0.31
LUX = 0         ADC = 64    Res LDR 150000.00   Vout0.31
LUX = 0         ADC = 63    Res LDR 150000.00   Vout0.31
LUX = 0         ADC = 62    Res LDR 152539.68   Vout0.30

Any comments will be welcome if some kind of improvements can be achieved in terms of a more precise equation in converting to Lux.

Thanks

Regards

taz ..
``````
``````    ADC = analogRead (LDR_input);

RLDR = (10000.0 * (5 - Vout))/Vout;     // Equation to calculate Resistance of LDR, [R-LDR =(R1 (Vin - Vout))/ Vout]
// R1 = 10,000 Ohms , Vin = 5.0 Vdc.

Vout = (ADC * 0.0048828125);           // Vout = Output voltage from potential Divider. [Vout = ADC * (Vin / 1024)]
``````

I would compute Vout before using it to compute RLDR (swap the above two lines).

``````  Lux = (500 / RLDR);
``````

Try Lux = (500.0 / RLDR);

[code] :)

``````   int ADC;

``````

Have you got something against compact code?

``````   int ADC = analogRead (LDR_input);
``````
``````  RLDR = (10000.0 * (5 - Vout))/Vout;     // Equation to calculate Resistance of LDR, [R-LDR =(R1 (Vin - Vout))/ Vout]
``````

And what value does Vout have? Dividing by 0 is not generally a good practice.

lux is difficult to measure, - see wikipedia

I found the tsl235 a nice sensor to measure light intensity - http://playground.arduino.cc/Main/TSL235R - but may not be what you want?

Why are you converting to lux. In fact why convert anything when you can use the sensor value directly.

Mark

Hello,

Regards

RLDR = (10000.0 * (5 - Vout))/Vout;    // Equation to calculate Resistance of LDR, [R-LDR =(R1 (Vin - Vout))/ Vout]   // R1 = 10,000 Ohms , Vin = 5.0 Vdc.
Vout = (ADC * 0.0048828125);          // Vout = Output voltage from potential Divider. [Vout = ADC * (Vin / 1024)]

I will try to swap the lines, i.e make the Arduino calculate the Vout first and then RLDR, this quite make some sense, but sometimes we do not see it when we are just coding. :)

PaulS: Have you got something against compact code?

``````   int ADC = analogRead (LDR_input);
``````

:) .. i will try to stick to compact mode now . . :)

``````  RLDR = (10000.0 * (5 - Vout))/Vout;     // Equation to calculate Resistance of LDR, [R-LDR =(R1 (Vin - Vout))/ Vout]
``````

And what value does Vout have? Dividing by 0 is not generally a good practice.

At night in the room with light on, i could get only 0.31 Volts reading from serial monitor as well as from voltmeter.

Do i need to adjust the fixed resistor so as to get more coherent values for the Vout and less trouble for the Arduino to compute ??

Actually im using a 10 K ohms resistor with the potential divider circuit . .

Any views ?

Thanks

Hello,

The link to the sensor is quite nice, but i shall stick to the simple LDR, cause i shall be doing some switching only for some LIghts when sunset occurs.

Moreover i want to display this value on an LCD, like i displayed % soil moisture and now some light reading.

holmes4:
Why are you converting to lux. In fact why convert anything when you can use the sensor value directly.

I think it is more appropriate to display Lux reading on an LCD instead of ADC value to the people around . . Thanks

taz …

i want to display this value on an LCD,

You can make a lookup table for mapping voltages on Lux. You need a real Lux meter to do some calibrations.

multiMap can be used for non-linear mapping / lookup with interpolating - http://playground.arduino.cc/Main/MultiMap -

Is there any look up table, already up for the lux values .. ?? :)

Callibration is quite tough to be doing.. as its difficult to get a lux meter..

Back to the above equation ... if i need some greater values for the Vout at night, i.e. a bigger range than 0.31, should i render my circuit more sensitive,do i need to change the fix resistor to 100 k ohms or less ??

\thanks

taz...

taz3m: At night in the room with light on, i could get only 0.31 Volts reading from serial monitor as well as from voltmeter.

What are the values you get with the light off?

Do i need to adjust the fixed resistor so as to get more coherent values for the Vout and less trouble for the Arduino to compute ??

Probably. Have you measured the voltage with your meter (with light on and light off)? That is, is your circuit definitely wired properly and working?

Cheers, John

There is nice tutorial on a subject. http://learn.adafruit.com/photocells/using-a-photocell I like the idea on page "using photocell", when R measured via Time. Though, example code may need to be "tweaked" to get better accuracy.

Taz3m It might be late for you but still may be of use to someone ending up here:

you have used the formula Lux = (500 / RLDR) but as I understand that formula uses the RLDR expressed in k Ohm As I understand from your program you calculate in Ohm seems then that maybe you are dividing by a factor 1000 too much

hello tan3z may i know why the value of Lux alway is 0 only, when i give bright to LDR sensor still no change constant 0 value.

I am sure its too late in replying to it, but I am sure some new person will be reading this and find it useful.

I did the LUX calculation with the LDR, but somehow it was like a crack, readings were weird, some times sunlight was dark and reading was less, I went mad.. Finally I though why dont we simply calculate the voltage across the LDR, this helped

The formula is int vout =analogRead(A1); int Voltage = vout * (5 /1023); // 5v =vin

Serial.println(voltage);

Then I started calculating the serial monitor, readings like this e.g 2.40 @ 5.38pm

2.28 @ 5.40pm

2.04 @ 5.42 pm

1.63 @ 5.45pm

1.16 @ 5.48 pm

0.55 @ 5.53pm

Finally I looked through my window, @ 0.55 value in my serial monitor, my neigbor and surrounding lights just was getting on or glowing. So I mark my program to switch on a relay when voltage is 0.55..

to even make it a whole number, I add +1 to the voltage, which means when the voltage 1.55 is reached my relay gets on. You can take this formula or even multiple it like eg., voltage *10... which means 5.5 voltage is reached the relay gets ON..

Hope this helps. Thank you.