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
Everything below is what the build uses. The links go to the parts we make and test.
- Official Arduino Uno
- DIYables STEM V3, Fully Compatible with Arduino Uno R3
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- Light Sensor
- 10 kΩ Resistor
- Servo Motor
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V3 Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
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.
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