Light Sensor Control of Servo with Photoresistor

This project makes use of a photoresistor as a light sensor to control the movement of a servo motor’s shaft. The change of position in the servo motor’s output shaft moves from 0 degrees to 180 degrees maximum depending upon the light intensity present at the photoresistor. The highest level of light places the output shaft arm to 0 degrees while the absence of light at the sensor moves the output shaft arm to about 180 degrees. The actual range of motion is limited to the quality of the servo, but 180 degrees is generally the limit.

All components in the system are powered by the Arduino Uno (R3) through the USB connection from a local supply or computer.

The purpose of the project is to demonstrate the simple use of a servo for the automatic positioning of a physical object as a function of light intensity. The light sensor is read at an analog GPIO pin to get continuously variable values to read into memory via an analogRead operation. The motor response to continuous analog read activity (with a small 250ms delay between each) is calculated to a PWM output level written to a port where the output shaft moves at a corresponding angle.

The micro servo mounts nicely onto the Arduino Uno in a secure way through the two standoffs on the controller. The various terminal connections in this photo correspond to the schematic diagram posted below.

The resistor is 5K-ohms with the analogRead connection between it and the photoresistor for active continuous input to the Arduino.

Project Operation:


Project Schematic:

Arduino IDE:

Servo Output Angle Calculations:

Code Setup:

#include <Servo.h>
int lightVal;
int lightPin=A4;
int dt=250;
int angle;
int servoPin=9;
int servoPos=165;
Servo myServo; //create object with a name

void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
pinMode(lightPin,INPUT);
pinMode(servoPin,OUTPUT);
}

void loop() {
lightVal=analogRead(lightPin);
Serial.println(angle);
delay(dt);
angle=(-16./63.)*lightVal+(16.*780.)/63.;
myServo.write(angle);
}


About

Servant of Christ Jesus. U.S. Military Veteran, Electrical Engineer, Pepperdine MBA, and M.A. Biblical and Theological Studies.

, ,

Comments are closed.