Saturday, January 12, 2013

Day 7: Photoresistors and Mapping Functions

There's nothing new in terms of setting up this circuit. You simply create one circuit with an LED and another circuit with a photoresistor that you use as a light sensor. The photoresistor is set up in a voltage divider in the same way as the potentiometer in Day 2. It's neat to see that you can use a single setup like a voltage divider for a range of different sensors.
Voltage Divider
Even though this is fundamentally identical to the potentiometer circuit, it feels a lot cooler. You can get the LED to increase and decrease in brightness just by waving your hand in front of it. Here's a terrible video by me of the circuit in action.

The other awesomely useful thing that I learned building this circuit was the map function. In short, this easily solves the problem that I faced in my potentiometer controlled multicolored LED side project in Day 3. In that one, the potentiometer read in an analog signal between 1-1023 and the LED pins read between 0 and 255 and so I had to invent an adjustment factor to move between the two. The map function does that for me. So in this case I could do the function

lightLevel=map(lightLevel, 0, 1023, 0, 255);

which is exactly the same map function as required in this circuit as well. Cool stuff that will definitely save me time in the future!

No comments:

Post a Comment