OPEN_ROBO_HUB Back to Projects
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

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

  1. First test one LDR on A0 and print its value in Serial Monitor.
  2. Connect all four LDR voltage dividers to A0, A1, A2, and A3.
  3. Test the horizontal servo on D9 using a simple servo sweep sketch.
  4. Test the vertical servo on D10 using the same sweep sketch.
  5. Upload the full solar tracker code below.
  6. Shine a torch from different directions and watch the panel move.

How The Code Works

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