My Arduino starter kit came with a photocell, aka photoresistor, aka a light dependent resistor (LDR). That made me happy (see Side Story, below). I didn't know if it was working or not.
Photo from Adafruit.com |
So I first tried inserting it in a basic Arduino-LED circuit. I tried covering the LDR with a finger or black cloth but I couldn't detect any change in the brightness of the LED.
Lady Ada came to the rescue with this Arduino photocell tutorial and I was able to set up the experiment. The tutorial should result to a LED light growing brighter as the room grows darker.
There was one problem though. Although I could get the LED to light up, I could only detect very faint changes in the brightness when I covered the LDR with different materials (including my finger). Turning off the room light didn't help because I couldn't be sure if the variation in brightness was the result of my eyes getting accustomed to the dark.
I wanted to be sure that this was really the LDR working and not some external factor. So I scrutinized the sketch and realized that the code included an output of the reading. Here's a snippet from the sketch, available at Adafruit:
photocellReading = analogRead(photocellPin); //read input from Pin 11
Serial.print("Analog reading = ");
Serial.println(photocellReading); //print the value from Pin 11
Lady Ada's circuit design involved hooking up the LDR into one of the analog inputs of Arduino (Pin 11). This then allowed Arduino to read and print the analog values as they got affected by the LDR. But where was the output being printed?Serial.print("Analog reading = ");
Serial.println(photocellReading); //print the value from Pin 11
Well, the new Arduino IDE (1.0.5) includes a Serial Monitor (Click Tools > Serial Monitor), a window that displays output from the Arduino board. I covered the LDR and started detecting minute decreases in the values. Then I turned off the lights and found a significant drop. Eureka!
I altered the delay value in the sketch so I could record the changes in one screen (from the default "100" to "5000". You could see this in the screenshot below:
Notice the changes in value as the ambient light changes. 569 is when the lights are off. |
See it in action in this video:
* * *
Trippy Side Story. As a kid, I was fascinated at how light-controlled switches worked. A friend of mine told me that such a switch was called a "light sensitive device" or LSD for short. I phoned our local electronics supply shop and asked the guy, "Do you guys sell LSD?" The guy at the other end said, "Huh?" and dropped the call. I only figured out his reaction years later, when I realized what LSD really meant. :)