STEM

Arduino: Photoresistor and Servo motor working together to indicate light

This fun project is to visually and mechanically indicate day/night or light/dimness around with the help of a photoresistor that reads the light values. And that’s connected to a motor (a servo) which has an arm/arrow connected to it and it moves to certain positions depending on the light sensed by the photoresistor. Lastly, the code is written to do the scale mappings and take care of all the fun logic parts, and downloaded to the microcontroller so the unit runs independently from thereon (can be detached from the computer and placed anywhere).

For the circuit, I used a Servo. Three M-M wires to connect to servo (including its power wire). 2 power jumper cables (to power Breadboard power rail from a single 5V pin of UNO) + 1 read wire for photoresistor. One fixed 10K Ω resistance photoresistor. One 5K Ω resistor (for photoresistor). And an arm for the servo, along with a piece of paper to draw the markers 🙂

Once the hardware is set up, we need to focus on the software and math side. The code reads the photoresistor values which is returned in 0-1023 scale. However, we need to understand the ambient light so we can calibrate it to reflect more realistic range. For example, for my resistor and in my ambient light, I rescaled it to 50-600 range. Next we need to define the range of servo…which is 0-180 but in degrees. So, we need to map the light values 50-600 to degrees in 0-180. To do that, the simplest (and the best) way is to find the slope and intercept of the two extreme points…e.g. at complete darkness and full brightness. Doing the math, and applying the good old y=mx+b, we find the relationship between servo’s angle and the light values returned by sensor is: y= -0.33 x + 196 where y is the servo’s angle we want to move it by, and x is the light value being read.

Then the code continuously monitors the light values read by the photosensor in real-time and immediately calculates the servo’s arm angle and sends a signal to move it.

To see the position easily, I haphazardly drew some indicators on paper and taped to the servo. In the video below, you can see I cover the photosensor by finger to allow in very little light and you can see the servo’s arrow move toward dark “moon”. Whereas when I turned on an extra light. the arrow moved toward the brightest sun. For details and other fun projects with electricity, Arduino controller, and C/C++ code, please visit http://flyingsalmon.net/?tag=arduino

You can imagine, this combination of a sensor and a mechanical motion can be applied to plethora of useful applications. You can have a water sensor to indicate humidity for your potted plants, temperature sensor to indicate temperature converted to your favorite scale, a timer/counter to indicate time, an automatic task (such as feeding your fish or pet) with the arm when something is sense or some other condition as set in your code, etc. etc. etc. Have fun with it!

Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!


Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top