Measuring Light Intensity with Arduino
Get the physics right, then the code!
In this article, we will design a little light meter, making sure that we get the whole pipeline right: the physics, the math, the code, and the wiring.
The Sensor
First off, the sensor. This is a light-dependent resistor, or LDR aka photoresistor: a little gadget whose resistance R1 varies with the intensity L of the light to which it is exposed.
The equation on the right of the figure above gives an empirical formula for how L and R1 are related. The intensity L is measured in lux = lumens per square meter, while R1 is measured in Ohms. To use an LDR, one needs to know the coefficients B and m. They can be obtained by experiment. Get a light meter to measure L and an ohmmeter (e.g, multimeter) to measure R1. If you don’t have “real” light meter, you can get one for your smart phone. Expose both the meter and the LDR to the same light source. Record L and R1 for various light levels. Plot y = log L versus x = log R1 on some graph paper. You will get a bunch of dots. Take a ruler and draw the line that best fits the data (the dots). It will be a downward-sloping line with equation y = mx + b. Then (using natural logarithms), we have
log L = m(log R1) + b
so that
L = e^b R^m L = BR^m
where B = e^b
The exponent m is negative, so that light intensity decreases as resistance increases. This is the required relation between light intensity and resistance of the LDR. (See the references at the end for more on the L versus R relationship).
The Circuit
In the figure below you see the circuit: a voltage divider with the LDR R1 attached the 5 volt pin of the Arduino and a 10,000 Ohm resistor R2 attached to the ground pin. The two resistors are tied together and the tie point is attached to the analog input pin A0
. To the right of the circuit diagram, you see the voltage drops: V1 across R1, V2 across R2, and the total voltage drop Vtotal = V1 + V2. The voltage V1 is what the Arduino measures at A0
.
We need to compute R1, which will give us the light intensity, in terms of V2. V2 is what the Arduino senses. The computation is an exercise in Ohm’s law,
V = IR
On the right of the figure above, you see a derivation of the current I through the circuit. Another application of Ohm’s Law gives V2 = I x R2. Substitute the formula for the current in this expression and solve for R1 to obtain
R1 = (5.0/V2 - 1)R2 (*)
Since R2 is known, we have what we want.
The Code
Below is the Arduino code which implements the above theory. The Arduino reads the voltage at A0
encoded as an integer between 0 and 1024. It computes the light intensity in lux from this value and sends it out once per second over the serial port.
In addition, and just for fun, an LED is turned on if the intensity falls below light_intensity
. The LED is connected to pin 7 and to ground through a 230 Ohm resistor. Of course you could also do something more useful, such as tell a relay to turn on a light.
const double k = 5.0/1024;
const double luxFactor = 500000;
const double R2 = 10000;
const double LowLightLimit = 200;
const double B = 1.3*pow(10.0,7);
const double m = -1.4;const int LED = 7;
double light_intensity (int RawADC0) {
double V2 = k*RawADC0;
double R1 = (5.0/V2 - 1)*R2;
double lux = B*pow(R1,m);
return lux;} void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
} void loop() {
double intensity = light_intensity(analogRead(0));
if (intensity < LowLightLimit) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
Serial.println(intensity);
delay(1000);
}
The code is on GitHub.
References and Comments
Most LDRs are cadmium sulfide devices. Below are some refernces for the lux-to-resistance relationship. It is interesting to look at a typical datasheet. The figure below plots the logarithm of resistance versus the logarithm of the light intensity. The boundaries of the gray “wedge” give upper and lower limits for a linear relationship between these quantities. Notice that there is a considerable spread between these limits, which is why you need to calibrate your device using the method described above.