Light Tracking

Use two CdS photo resistors and a servo to construct a light tracking device.

Here’s some software to serve as a starting point.

// Control a servo based on two light sensors to track a flashlight.
//
// 1) Connect two CdS photo resistors in series to form a voltage divider. Apply 5V to one
//    end, ground to the other, and connect the point where the two connect to the
//    microcontroller ANALOG 0 pin.
// 2) Connect the servo to the microcontroller DIGITAL 9 pin.
// 

#include <Servo.h>

Servo servo;
int val;              // variable to hold the value read from the analog pin
int pos;              // variable to hold our servo position value

void setup()
{
myservo.attach(9); // attaches the servo object to pin 9
pos = 90;          // initialize the servo to center position (servo range is 0 to 179)
}

void loop()
{
val = analogRead(0);                 // reads the value of the light sensor voltage divider (value between 0 and 1023)

if(val < 512) pos = pos + 1;         // make a decision to move the servo clockwise or counterclockwise
else pos = pos – 1;

myservo.write(pos);                  // sets the servo position
delay(50);                           // waits for the servo to get there
}

.