Arduino - Light Sensor Triggers Servo Motor

by DIYables in Circuits > Arduino

15 Views, 0 Favorites, 0 Comments

Arduino - Light Sensor Triggers Servo Motor

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-light-sensor-triggers-servo-motor

This build uses a light sensor to control a servo motor with an Arduino. The Arduino continuously reads the light level from the sensor, and when the reading crosses a set threshold, it drives the servo to one position; otherwise it holds the servo at another. It is a simple way to turn a change in light into mechanical motion.

Supplies

Wiring

Wire the light sensor (LDR) with the 10 kΩ resistor as a voltage divider and connect its output to Arduino pin A0. Connect the servo motor's signal wire to Arduino pin 9, and power and ground the servo and sensor from the Arduino. If you want simpler wiring, an LDR light sensor module can replace the bare LDR and resistor.

How It Works

The Arduino reads an analog value from the light sensor on pin A0. This value rises or falls depending on how much light hits the sensor. The sketch compares the reading against a fixed threshold of 500: when the value is above the threshold the servo rotates to 90 degrees, and when it is at or below the threshold the servo returns to 0 degrees.

Code

The sketch includes the Servo library and defines the sensor pin (A0), the servo pin (9), and the threshold (500). In setup(), it attaches the servo on pin 9 and sets its initial position to 0 degrees. In loop(), it calls analogRead() on the light sensor pin and uses an if/else to call servo.write(90) or servo.write(0) depending on whether the reading exceeds the threshold.

arduino-light-sensor-triggers-servo-motor.ino - the sketch from the tutorial, unchanged.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor-triggers-servo-motor
*/

#include <Servo.h>

// constants won't change
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's pin
const int SERVO_PIN = 9; // Arduino pin connected to Servo Motor's pin
const int ANALOG_THRESHOLD = 500;

Servo servo; // create servo object to control a servo

void setup() {
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(0);
}

void loop() {
int analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

if (analogValue > ANALOG_THRESHOLD)
servo.write(90); // rotate servo motor to 90 degree
else
servo.write(0); // rotate servo motor to 0 degree
}

Things That Go Wrong

If the servo does not move, check that its signal wire is on pin 9 and that it has power and ground connected. If the servo reacts the opposite way you expect to light, the threshold value of 500 may not match your sensor and lighting conditions, so adjust it in the code. Also confirm the light sensor's analog output is wired to A0, not a different pin.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-light-sensor-triggers-servo-motor