Module 02
Dual-Axis Solar Tracker
Dual-Axis Solar Tracker
This project moves a small solar panel toward the brightest light. Four LDR sensors compare light from left, right, top, and bottom. Two servo motors move the panel horizontally and vertically so the panel faces the strongest sunlight.
Components Required
- Arduino Uno or Arduino Nano
- 4 LDR sensors
- 4 fixed resistors, usually 10k ohm, for voltage dividers
- 2 SG90 servo motors
- Small solar panel or cardboard panel for demo
- Breadboard, jumper wires, and 5V power supply
Connections
| Component | Arduino Pin | Connection Note |
|---|---|---|
| Left LDR voltage divider output | A0 | Reads light from the left side |
| Right LDR voltage divider output | A1 | Reads light from the right side |
| Top LDR voltage divider output | A2 | Reads light from the top side |
| Bottom LDR voltage divider output | A3 | Reads light from the bottom side |
| Horizontal servo signal wire | D9 | Moves panel left and right |
| Vertical servo signal wire | D10 | Moves panel up and down |
| Servo VCC | 5V | Use external 5V supply for two servos if possible |
| Servo GND | GND | Servo GND and Arduino GND must be connected together |
| All LDR divider GND points | GND | Common ground is required for correct analog readings |
| All LDR divider VCC points | 5V | Each LDR must be used with a resistor as a voltage divider |
For each LDR, connect one side of the LDR to 5V. Connect the other side to the analog pin and also to a 10k resistor. Connect the other side of the resistor to GND. This creates a voltage divider that Arduino can read.
Build Steps
- First test one LDR on A0 and print its value in Serial Monitor.
- Connect all four LDR voltage dividers to A0, A1, A2, and A3.
- Test the horizontal servo on D9 using a simple servo sweep sketch.
- Test the vertical servo on D10 using the same sweep sketch.
- Upload the full solar tracker code below.
- Shine a torch from different directions and watch the panel move.
How The Code Works
- The Arduino reads four analog values from the LDR sensors.
- It compares left value with right value to control horizontal movement.
- It compares top value with bottom value to control vertical movement.
- If the difference is small, the servos stop moving.
- If one side is brighter, the servo angle changes slowly toward that side.
Full Arduino Code
#include <Servo.h>
Servo horizontalServo;
Servo verticalServo;
const int leftLDR = A0;
const int rightLDR = A1;
const int topLDR = A2;
const int bottomLDR = A3;
int horizontalPos = 90;
int verticalPos = 90;
const int tolerance = 40;
void setup()
{
Serial.begin(9600);
horizontalServo.attach(9);
verticalServo.attach(10);
horizontalServo.write(horizontalPos);
verticalServo.write(verticalPos);
delay(1000);
}
void loop()
{
int leftValue = analogRead(leftLDR);
int rightValue = analogRead(rightLDR);
int topValue = analogRead(topLDR);
int bottomValue = analogRead(bottomLDR);
int horizontalDifference = leftValue - rightValue;
int verticalDifference = topValue - bottomValue;
if(abs(horizontalDifference) > tolerance)
{
if(horizontalDifference > 0)
{
horizontalPos++;
}
else
{
horizontalPos--;
}
horizontalPos = constrain(horizontalPos, 0, 180);
horizontalServo.write(horizontalPos);
}
if(abs(verticalDifference) > tolerance)
{
if(verticalDifference > 0)
{
verticalPos++;
}
else
{
verticalPos--;
}
verticalPos = constrain(verticalPos, 0, 180);
verticalServo.write(verticalPos);
}
Serial.print("Left: ");
Serial.print(leftValue);
Serial.print(" Right: ");
Serial.print(rightValue);
Serial.print(" Top: ");
Serial.print(topValue);
Serial.print(" Bottom: ");
Serial.print(bottomValue);
Serial.print(" HPos: ");
Serial.print(horizontalPos);
Serial.print(" VPos: ");
Serial.println(verticalPos);
delay(50);
}
Common Problems And Fixes
- If servos shake, use an external 5V supply and connect all grounds together.
- If readings do not change, check the LDR voltage divider wiring.
- If movement is opposite, swap left/right sensor positions or reverse the plus and minus logic in code.
- If the panel moves too much, increase the tolerance value from 40 to 60 or 80.
- If the panel moves too slowly, reduce the delay from 50 to 20.