11:19:04 AM NPT Arduino, Robotics & IoT tutorials for Nepal
Beginner

Solar Tracker with Arduino, LDRs and Servo (Dual Axis Option)

Build a solar tracker with Arduino, four LDRs and two servos: single and dual axis wiring, code, calibration and troubleshooting.

On this page
Solar Tracker with Arduino, LDRs and Servo (Dual Axis Option)

A solar tracker turns a small solar panel to face the sun through the day, so that more light hits the panel. It is a popular BE and diploma project, and also a strong science exhibition model, because the panel visibly moves toward a torch or the sun. In this build, light sensors (LDRs) decide which way to turn, and servos move the panel.

We start with the single-axis version (two LDRs and one servo) and then extend it to dual axis (four LDRs and two servos). The tracker itself needs no solar panel to work, because the LDRs do the sensing. The panel is just the thing you move, so a small demo panel or a piece of cardboard is fine for a model.

What you need

Scroll sideways to see all columns.

Item Qty Notes
Arduino Uno or classic 5 V Nano 1
LDR (photoresistor), GL5528 type 2 for single axis, 4 for dual axis
Resistor 10 kΩ 2 or 4 One per LDR, forms a voltage divider
SG90 micro servo 1 or 2 Pan (and tilt for dual axis)
Small solar panel, or a light board of the same size 1 Panel for demo only
External 5 V supply (power bank or 5 V adapter, sized for the combined peak current of the servos) 1 For the servos
Electrolytic capacitor 470 uF 1 Across the servo supply
Cardboard or plastic sheet, and a divider cross Shades LDRs from each other
Pan-tilt bracket or a simple hand-made frame 1
Jumper wires, breadboard 1 set

Connection table

Each LDR forms a voltage divider with a 10 kΩ resistor. The analog pin reads the middle point: brighter light lowers the LDR resistance and raises the voltage when the LDR is on the supply side.

Divider for every LDR: 5 V to LDR, LDR to the analog pin, the same node through 10 kΩ to GND.

Scroll sideways to see all columns.

Pin or terminal Connects to
LDR top-left (TL) divider Arduino A0
LDR top-right (TR) divider Arduino A1
LDR bottom-left (BL) divider Arduino A2
LDR bottom-right (BR) divider Arduino A3
Pan servo signal (left-right) Arduino D9
Tilt servo signal (up-down) Arduino D10
Servo VCC / GND External 5 V / GND, with the 470 µF capacitor across them, positive leg to 5 V
External supply GND Arduino GND (common ground)
Arduino power USB cable or power bank

For the single-axis version, use only TL and TR (A0 and A1) and the pan servo.

Do not connect a solar panel's output directly to an Arduino pin. A real panel can give more than 5 V and could damage the board. Use the panel as a moving load only, unless you add a proper charge controller.

How it works

The four LDRs sit in four quadrants, separated by a cross-shaped shade. If the sun is to the upper left, the shade casts shadow on the right and bottom LDRs, so the top-left LDR is brightest. The Arduino compares the sum of the readings on each side:

  • Pan (left-right): compare (TL + BL) with (TR + BR).
  • Tilt (up-down): compare (TL + TR) with (BL + BR).

If the difference is larger than a small tolerance (to avoid jitter), it turns the servo by one degree toward the brighter side. When all four LDRs read about the same, the panel faces the light and the servos stop. This is a closed-loop (feedback) control system, and a viva-friendly one: the sensors measure the error and the servos reduce it.

Step-by-step build

  1. Build the pan-tilt frame. The pan servo rotates the whole assembly on its horn, and the tilt servo is on top and moves the panel up and down.
  2. Fix the panel on the tilt servo. Keep it light so the SG90 can lift it.
  3. Glue the four LDRs on the panel face in a 2 x 2 grid and add the cross-shaped shade, standing a few centimetres high, so each LDR gets a different amount of light as the angle changes.
  4. Wire each LDR with its 10 kΩ resistor as a divider.
  5. Wire the servos to the external 5 V supply, add the capacitor and join the grounds.
  6. Upload the test sketch below, then the full code.

LDR test

sketch-1.inoDownload code
// LDR test - not compiled yet
void setup() { Serial.begin(9600); }
void loop() {
  Serial.print(analogRead(A0)); Serial.print("\t");
  Serial.print(analogRead(A1)); Serial.print("\t");
  Serial.print(analogRead(A2)); Serial.print("\t");
  Serial.println(analogRead(A3));
  delay(300);
}

Shine a torch from different directions. The quadrant closest to the torch should show the largest number (with this wiring). If the lit sensor reads lower, check that the LDR goes to 5 V and the 10 kΩ resistor to GND.

Complete code

sketch-2.inoDownload code
// Dual axis solar tracker - not compiled yet
#include <Servo.h>

const bool DUAL_AXIS = true;
Servo pan, tilt;
const int PAN_PIN = 9, TILT_PIN = 10;

int panAngle  = 90;
int tiltAngle = 90;

const int PAN_MIN = 20,  PAN_MAX = 160;
const int TILT_MIN = 40, TILT_MAX = 140;
const int TOLERANCE = 30;      // ignore small differences (difference between two sums)
const int STEP = 1;            // degrees per move
const int LOOP_DELAY = 80;     // ms; slower is gentler on the servos

int readAvg(int pin) {
  long sum = 0;
  for (int i = 0; i < 8; i++) sum += analogRead(pin);
  return sum / 8;
}

void setup() {
  pan.attach(PAN_PIN);
  pan.write(panAngle);
  if (DUAL_AXIS) {
    tilt.attach(TILT_PIN);
    tilt.write(tiltAngle);
  }
  Serial.begin(9600);
  delay(500);
}

void loop() {
  int tl = readAvg(A0);
  int tr = readAvg(A1);
  int bl = DUAL_AXIS ? readAvg(A2) : 0;
  int br = DUAL_AXIS ? readAvg(A3) : 0;

  int left   = tl + bl;
  int right  = tr + br;
  int top    = tl + tr;
  int bottom = bl + br;

  int panError  = left - right;      // positive: left side brighter
  int tiltError = top  - bottom;     // positive: top brighter

  // Directions below depend on how the servos are mounted.
  // If the panel turns away from the light, flip the + and - signs.
  if (abs(panError) > TOLERANCE) {
    panAngle += (panError > 0) ? STEP : -STEP;
    panAngle = constrain(panAngle, PAN_MIN, PAN_MAX);
    pan.write(panAngle);
  }
  if (DUAL_AXIS && abs(tiltError) > TOLERANCE) {
    tiltAngle += (tiltError > 0) ? STEP : -STEP;
    tiltAngle = constrain(tiltAngle, TILT_MIN, TILT_MAX);
    tilt.write(tiltAngle);
  }

  Serial.print(panAngle); Serial.print(" ");
  Serial.println(tiltAngle);
  delay(LOOP_DELAY);
}

For the single-axis version, set DUAL_AXIS to false and use the A0, A1 and D9 connections only.

Calibration and testing

  • Indoor test with a torch. Hold a phone torch about 30 cm from the LDRs and move it left, right, up and down. The panel should follow.
  • Tolerance. If the servos jitter when the light is steady, raise TOLERANCE. If the panel reacts too late, lower it.
  • Direction check. If the panel moves away from the light, flip the signs in the two if blocks.
  • Limits. Set the min and max angles so the frame never hits the base.
  • Real sun. Test outdoors in direct sunlight. Strong sunlight can saturate LDRs, so a shade of tinted plastic over each LDR helps.
  • Compare output. If you use a small panel, measure its voltage with a multimeter when it faces the sun and when it is tilted away, and record the difference for your report.

Troubleshooting

Scroll sideways to see all columns.

Symptom Likely cause Fix
Panel moves away from the light LDR wiring or code sign reversed Check divider wiring, then flip the corresponding servo sign in the code
Servos jitter constantly Tolerance too low or noisy supply Raise TOLERANCE; add capacitor; average more samples
Arduino resets when servos move Servos on the Arduino 5 V Use the external 5 V supply, common GND
Panel drifts to one side LDR values mismatched Calibrate by subtracting each LDR's offset in a dark room
Does not track in the room Ambient light is too even Use a torch and a taller shade cross
Servo hits the end and buzzes Limits too wide Tighten the min and max angles
Tracks the sun then returns at night No sleep logic Add a "night" test: if all LDRs are dark, hold or return to east

Why a tracker is worth showing

Claims such as "a tracker gives X percent more energy" depend on location, season and panel, so do not quote a number you did not measure. Instead, compare fixed and tracking panels under equivalent loads and lighting. For an energy claim, log loaded voltage and current over time, calculate watt-hours, and account for the servo energy.

Make it stand out

  • Add a "night return" feature that sends the panel back to the east at night.
  • Log the voltage of a real panel through a voltage divider and plot fixed versus tracking.
  • Add an I2C LCD showing the pan and tilt angles.
  • Compare an LDR-based method with a time-based method (using an RTC module) and discuss both.

FAQ

Is single axis enough?

For a basic project, yes. Single axis follows the sun's east-to-west path. Dual axis also adjusts for the sun's height, which changes with the season.

Can the same servos carry a big solar panel?

No. SG90 servos handle only very light loads. A larger panel needs stronger servos, a gear arrangement or a linear actuator.

Does the tracker use more power than it saves?

For a model it does not matter. For a real system, designers consider the motor energy, so mention it in your discussion.

Why use a shade cross?

Without it, all four LDRs see nearly the same light and the system cannot tell where the sun is. The shade turns direction into a brightness difference.

Related tutorials

See all Project Ideas →