Tuesday, November 5, 2013

Reading output from a photocell and Arduino

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
An LDR works by varying the resistance in a current, depending on the amount of ambient light it detects. As a kid, this was the coolest device for me (again, Side Story). Unfortunately, I never got to playing with an LDR until I got it as part of the Arduino starter kit.

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?

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.
It turns out the LDR was quite sensitive, detecting and printing out minute changes as I moved around the room. It's just that the circuitry and the LED weren't expressing the analog changes properly. I figured I could calibrate this by changing the other resistors in the circuit, but that's for another experiment.

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. :)

No comments:

Post a Comment

Thank you for commenting! Your comment will be submitted for review and approval. Please return soon!

What to do when you've got a virtual scrum team

Scrum and Agile are suddenly popular in Asia, and because a lot of companies take on outsourced projects, they usually have virtual teams, w...