Sensing the analog world
4. Sensing light
The photocell we will use in this module is of a type called a light dependent resistor, sometimes called an LDR. As the name suggests, these components act just like a resistor, except that the resistance changes in response to how much light is falling on them.
Hook it up to the Ohm-meter of the multimeter and guess what its range is!
The LDR isn’t very precise, so we cannot get a quantitative LUX reading
or anything like that. But it is good enough to tell the difference
between light and shadow, or know if the light in the room is on/off.
So if we just need to know if the light in the room has changed, or
someone walked by (casting a shadow) the LDR will do.
Connecting the LDR
The LDR changes its resistance with light so we can measure that
change using one of the Arduino’s analog pins. But to do that we need a
fixed resistor that we can use for that comparison. We
are using a 10K resistor.
This circuit is called a voltage divider and divides the 5v between the LDR and the resistor. Then we measure how much voltage is on the LDR using the analog read on the Arduino, and we have the reading. The amount of that 5V that each part gets is proportional to its resistance.
With the Arduino analogRead(), at 5V it would read 1023, and at 0v it read 0.
So if the the LDR and the resistor have the same resistance, the 5V is split evenly (2.5V), to each part. (analogRead of 512)
But if the LDR is hit with a ton of light and is reading only 1K of resistance, the 10K resistor is going to soak up 10 times as much of that 5V. So the LDR would only get .45V (analogRead of 92).
And if it is in a dark room, the LDR may be 40K or resistance, so the LDR will soak up 4 times as much of that 5V as the 10K resistor. So the LDR would get 4V (analogRead of 818).
int LDR_Pin = A0; //analog pin 0
void setup(){
Serial.begin(9600);
}
void loop(){
int LDRReading = analogRead(LDR_Pin);
Serial.println(LDRReading);
delay(250); //just here to slow down the output for easier reading
}