Introduction: Light-Controlled Servo
Servos are very versatile motors; they can be used in a wide range of mechanical applications. This project focuses on the control of a servo motor depending on the light levels in the current environment. This idea can be applied to applications such as control of lamps and light switches where the servo will manually actuate the light switches given a certain light condition to both turn it on or off. This allows many homes be semi-automated without having to a large expense. Servo motors can be used virtually anywhere and this technology can be further applied to other fields and use cases.
Step 1: Tools and Materials
- Arduino 101 or Arduino Uno
- Breadboard
- Photresistor or LDR
- 10k Ω resistor
- Servo motor
- Jumper Wires
Step 2: Circuitry
Connecting the Arduino power to the breadboard and Servo motor. The LDR will be getting power from the 3.3V pin of the Arduino, while the servo will be getting power form the 5V pin of the Arduino and both are connected to a common.
- Connect the 5V pin of the Arduino to the red pin of the servo with a male-to-male red jumper cable. Red pins will be used to indicate 5V.
- Connect the GND pin of the Arduino to the black pin of the servo with a male-to-make black jumper cable.
- Connect the 3.3V pin of the Arduino to the red power rail of the breadboard with an orange jumper wire.
- Connect the GND pin of the Arduino to the ground power rail of the breadboard with a brown jumper wire.
Wiring the Servo to the Arduino
- Connect the white pin of the servo moor to the digital pin 9 of the Arduino board.
Wiring the LDR to the Arduino. For this, we will need a voltage divider.
- Connect one of the pins of the LDR onto the red power rail.
- Connect the other pin to a 10kΩ resistor in series to ground.
- Connect the remaining pin of the LDR connected to the 10kΩ resistor to the analog pin A0 of the Arduino.
Step 3: Code
//include the servo library
#include
//create a servo object called servo1 Servo servo1;
void setup() { // put your setup code here, to run once:
// set the servo pin, pin 9, as an servo output pin servo1.attach(9); }
void loop() { // put your main code here, to run repeatedly:
int lightValue = analogRead(A0);
// map the light readings to the angle possible by the servo motor lightValue = map (lightValue, 0, 1023, 0, 180);
// control the servo motor based on the light value read, adjust linearly by angles servo1.write (lightValue); }
Step 4: Demo
As the light level goes up, the servo turns towards 180 degrees. While, the light level goes up the servo turns in the opposite direction towards zero degrees, and varying light levels will turn the servo in it's respective direction.

Participated in the
Makerspace Contest 2017
9 Comments
3 months ago
I wired up the rest of the circuit, and then was wiring up the servo, and........................my board started smoking. I unplugged it about 1.3 milliseconds after I saw the smoke, but I don't really know if this was the wiring, or my Arduino. Thankfully, my Uno still works.
Question 1 year ago on Step 2
Thank you for this sketch! I am trying to add an or statement to the servo output similar to this project. Servo to start at 0 when dark, 180 when light. https://particle.hackster.io/OmarAlnamasi/door-lock-with-light-sensor-874107
2 years ago
I added the . On line 2
2 years ago
I added the on line 2
2 years ago
Can someone please tell me why I keep getting a code that says “ a function-definition is not allowed here before “{“ token. ?
It highlights the “ void loop () { //“ part of the text. Thanks if anyone can help
2 years ago
//include the servo library
#include
//create a servo object called servo1 Servo servo1;
void setup() { // put your setup code here, to run once:
// set the servo pin, pin 9, as an servo output pin servo1.attach(9); }
void loop() { // put your main code here, to run repeatedly:
int lightValue = analogRead(A0);
// map the light readings to the angle possible by the servo motor lightValue = map (lightValue, 0, 1023, 0, 180);
// control the servo motor based on the light value read, adjust linearly by angles servo1.write (lightValue); }
2 years ago on Step 3
The text of the code is missing <Servo.h> on line 2
Question 3 years ago on Step 2
Hello sir..how can i change the power source to a 9 volt battery?
Question 3 years ago
I'm trying to increase the angle the motor turns even more. I want it to turn a whole lot more than the tiny amount it's doing atm. How do I change the code so it opens to a particular angle once the light read drops below a certain amount? but still open linearly so it still opens based on the amount of light?