Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 42 of 47 · Smart farm and energy · Intermediate

Solar panel tracker with two LDRs

Two light sensors sit on the left and right of a panel. A servo turns the panel towards the brighter side until both sensors see the same light.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Solar panel tracker with two LDRs: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Left LDRLight sensor
Right LDRLight sensor
Panel servoServo

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1OrangeLeft LDR - 3.3 VPin 1 (3V3)
2ColouredLeft LDR - signalPin 7 (GPIO4)
3BlackLeft LDR - GNDPin 6 (GND)
4OrangeRight LDR - 3.3 VPin 17 (3V3)
5ColouredRight LDR - signalPin 29 (GPIO5)
6BlackRight LDR - GNDPin 9 (GND)
7ColouredPanel servo - signalPin 12 (GPIO18)
8RedPanel servo - 5 VPin 2 (5V)
9BlackPanel servo - GNDPin 14 (GND)

Python code (gpiozero)

from gpiozero import LightSensor, AngularServo
from time import sleep

left = LightSensor(4)
right = LightSensor(5)
panel = AngularServo(18, min_angle=-90, max_angle=90)

angle = 0
panel.angle = angle

while True:
    error = right.value - left.value          # positive: right side is brighter
    if abs(error) > 0.1:                      # a dead band stops jitter
        angle += 5 if error > 0 else -5
        angle = max(-90, min(90, angle))      # stay inside the servo range
        panel.angle = angle
    print("Left %.2f  Right %.2f  Panel %d deg" % (left.value, right.value, angle))
    sleep(0.5)

How the code works

You will learn

  • Comparing two sensors
  • A control loop that corrects a little each time
  • Dead band and limits

Try this

  • Move the Left LDR slider to 90 % and the right one to 20 %. Watch the panel turn.
  • Make both the same. Does the panel stop?
  • Change the step from 5 to 2 degrees. What changes?

On a real Pi

  • In the simulator the panel does not change the light on the sensors. On a real tracker it does, and that closes the loop.
  • A real panel is heavy and needs a strong motor or linear actuator, not a hobby servo.
  • Single-axis trackers can add 20 to 30 % energy. Check that the energy used by the motor is small.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4SG90 micro servo1Robu.in Amazon.in ThinkRobotics
5LDR (GL5528) photoresistor2Robu.in Amazon.in ThinkRobotics
61 µF capacitor2Robu.in Amazon.in ThinkRobotics
7Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
8Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 41. Greenhouse climate controlAll projects43. Cold-room temperature alarm →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.