Measuring the ambient light intensity using a photocell

/* 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 http://learn.adafruit.com/photocells */
 
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the analog resistor divider
 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin);  
 
  Serial.print("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading
 
  // We'll have a few threshholds, qualitatively determined
  if (photocellReading < 10) {
    Serial.println(" - Dark");
  } else if (photocellReading < 200) {
    Serial.println(" - Dim");
  } else if (photocellReading < 500) {
    Serial.println(" - Light");
  } else if (photocellReading < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
  delay(1000);
}

Using the above code, I placed a photocell on a breadboard with 1 leg connected to 5v and the other leg connected to analog pin 0 as well as connecting the same leg to ground using a 10k pulldown resistor.

Output

Analog reading = 774 - Bright
Analog reading = 754 - Bright
Analog reading = 768 - Bright
Analog reading = 876 - Very bright
Analog reading = 960 - Very bright
Analog reading = 943 - Very bright
Analog reading = 906 - Very bright
Analog reading = 941 - Very bright
Analog reading = 694 - Bright
Analog reading = 677 - Bright
Analog reading = 673 - Bright
Analog reading = 677 - Bright
Analog reading = 669 

...

Analog reading = 62 - Dim
Analog reading = 63 - Dim
Analog reading = 62 - Dim
Analog reading = 62 - Dim
Analog reading = 63 - Dim
Analog reading = 63 - Dim
Analog reading = 62 - Dim

With the room light on, the reading was Bright, and when I shone a torch light into the photocell it registered as Very bright. When I switched the room light off it registered as Dim, and when I cupped both hands over the photocell it registered as Dark.

Resources

Tags:
Source:
2243hrs.txt
Published:
16-02-2016 22:43