## Measuring light with an Arduino

The easiest way to measure light with an Arduino is with an LDR.
LDR’s (Light dependend resistors) have a low resistance in bright light and a high resistance in the darkness.
If you would us the LDR as the lower part of a voltage divider, then in darkness there would be a high voltage over the LDR, while in bright light, there would be a low voltage over that resistor.
Ofcourse if you would use it as the upper part of a voltage divider, it would be the reverse: low voltage in the darkness, high voltage in bright light

Doing that on an Arduino Analog port, would give a reading between 0 and 1024, which ofcourse are really non-descriptive numbers.
What you would want is an output in Lux or Lumen
That is possible but mind you that LDR’s are not really accurate for precise readings.
There is a somewhat rough formula that relates the resistance of an LDR to the light in Lux. That is:
```Rldr=500/Lux, or Lux=500/Rldr (in kOhm)```
as Rldr is related to the voltage measured over it, reading the Voltage over it, can be used to calculate the Rldr and thus the Lux level
If the LDR is the lower part of a 5 Volt Voltage divider and a 10kOhm resistor the upper part, the Voltage will be:
```Vout=(5/(10+Rldr))*Rldr Vout=5*Rldr/(10+Rldr)``` (remember: multiplication before division)
as we do not measure a voltage, but a value between 0 and 1024, every step can be defined by 5/1024=0.0048828125.
```=> Vout=Analogreading*0.0048828125 as Rldr=(10Vout)/(5-Vout) (remember Rldr is expressed in kOhm) => Lux=(500*(5-Vout))/(10*Vout) => Lux=(2500-500*Vout)/(10*Vout) => Lux=(2500/Vout-500)/10 => Lux=(2500/((AnalogRead*0.0048828125)-500))/10```

Anyway, in an arduino program that will look like:
``` //Lux double Light (int RawADC0){ double Vout=RawADC0*0.0048828125; //int lux=500/(10*((5-Vout)/Vout));//use this equation if the LDR is in the upper part of the divider int lux=(2500/Vout-500)/10; return lux; } void setup() { Serial.begin(115200); } void loop() { Serial.println(int(Light(analogRead(0)))); delay(1000); }```

Anyway, as said, an LDR is not the most accurate to measure light intensity. A photodiode would be better

Full work out of the Formula, for those interested (remember, this expresses Rldr in kiloOhm and is for a 10kOhm reference resistor:
```Vout=(5Rldr)/(Rldr+10) 5Rldr=Vout(Rldr+10)        // multiplied both sides with (Rldr+10) and switch left and right 5Rldr=Vout*Rldr+10out     // executed the right side multiplication 5Rldr+(-Vout*Rldr)=(Vout*Rldr+10Vout)+(-Vout*Rldr)   //added equal value (-Vout*Rldr) to both sides 5Rldr+(-Vout*Rldr)=Vout*Rldr+10Vout-Vout*Rldr        // work out right side  additions and subtractions 5Rldr-Vout*Rldr=Vout*Rldr+10Vout-Vout*Rldr           //work out left side additions and subtractions 5Rldr-Vout*Rldr=Vout*Rldr-Vout*Rldr+10Vout           // ditto 5Rldr-Vout*Rldr=10Vout     //ditto Rldr(5-Vout)=10Vout        //expressed left side as multiplication of Rldr Rldr=10Vout/(5-Vout)       // divided both sides by equal value (5-Vout)```
For a reference resistor other than 10k the soluton is as follows
`Rldr=(Rref*Vout)/(5-Vout)`
Also.. remember that though we expressed the value of Rldr as a function of Vout, ofcourse the value does not depend on Vout, it is the other way around, but it is necessary to inject the function of Vout into the Lux formula