Light-Tracking Servo (Arduino)
Aim a servo toward the brighter of two photoresistors — the core of a solar tracker. Learn to compare two analog inputs.
Intermediate30 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
light_tracker.ino
// Turn a servo toward whichever photoresistor sees more light.
#include <Servo.h>
Servo s;
int pos = 90; // start centered
void setup() {
s.attach(9);
s.write(pos);
}
void loop() {
int left = analogRead(A0);
int right = analogRead(A1);
if (left - right > 50 && pos < 180) pos++; // brighter on the left
else if (right - left > 50 && pos > 0) pos--; // brighter on the right
s.write(pos);
delay(20);
}
Steps
- Build two LDR dividers: each LDR from 5V to its analog pin (A0, A1), with a 10kΩ from that pin to GND.
- Mount the two LDRs angled apart on the servo horn so each sees a different side.
- Wire the servo signal to D9, power to 5V/GND.
- Upload — the servo turns toward the brighter side. Tweak the `50` dead-zone to taste.