Build a Raspberry Pi Pico dual-axis solar tracker with four LDRs, two servos and an OLED. Includes MicroPython code, a wiring table and a connection diagram.
Need it ready-made? Buy Ready Project
This Raspberry Pi Pico dual-axis solar tracker turns a small solar panel toward the brightest light on both axes. Four LDR (light-dependent resistor) sensors sit on a plus-shaped divider. They are wired as two voltage dividers — left against right, and up against down — so the Pico only needs two analog pins to tell which way the sun has moved. Two SG90 servo motors then pan and tilt the panel, and a 0.96" SSD1306 OLED shows the raw ADC readings, the servo angles and the panel voltage.
It is a bigger combination build: a Pico (or Pico W), four LDRs, two servos, an I2C display and a simple resistor divider that measures the panel. The Pico has only three usable ADC channels, so pairing the LDRs is the right design — you get a true difference signal on each axis and still have GP28 free to watch the panel. There is no Wi-Fi in the basic program. If you later add a Pico W web page, put YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD in the code and never type real credentials.
Use it as a desktop sun-follower, a science-fair demonstrator or the pointing head on a small 6 V hobby panel. It is not a storm-rated roof tracker and the plastic SG90 gears are not meant for a heavy framed module.
How the Pico Dual-Axis Solar Tracker Works
Each LDR drops in resistance as more light falls on it. Wire LDR LEFT from 3.3 V to the midpoint and LDR RIGHT from that midpoint to ground. When the left sensor is brighter its resistance falls, so the midpoint voltage rises toward 3.3 V. When the right sensor is brighter the midpoint falls toward 0 V. Equal light sits near 1.65 V. The vertical pair does the same for up and down.
The Pico reads those midpoints with its 12-bit ADC (MicroPython’s read_u16() scales the result to 0–65535). A centre value of about 32768 means the two sensors in that pair match. If the reading stays inside a dead-band, the code holds the servos still so the frame does not chatter. Outside the dead-band it steps the matching servo one degree at a time toward the brighter side.
- Pan (azimuth): GP26 reads the left/right pair. GP14 sends a 50 Hz servo pulse to the base (pan) servo, which turns the whole tilt bracket.
- Tilt (elevation): GP27 reads the up/down pair. GP15 drives the tilt servo that pitches the panel.
- Panel voltage: the panel positive goes through 22 kΩ to GP28, then 10 kΩ to ground. The code reconstructs the real panel voltage so you can see it climb as the panel faces the lamp or the sun.
- OLED: I2C0 on GP4 (SDA) and GP5 (SCL) shows H and V readings, pan and tilt angles, panel volts and AUTO or HOLD.
- HOLD button (optional): tap GP16 to ground to freeze the servos, and tap again to resume tracking.
Power is split on purpose. The Pico, the OLED and the LDR dividers run from the Pico’s 3.3 V rail (the Pico itself is powered from USB). The two servos need a separate 5 V supply of about 2 A. A stalling SG90 can pull several hundred milliamps, and two of them will brown out a PC USB port and reset the Pico. Join the external 5 V negative to a Pico GND pin so the 3.3 V control pulses have a return path. Do not feed 5 V into the Pico’s 3V3 pin.
Components Required
- Raspberry Pi Pico or Pico W, with MicroPython firmware – 1
- SG90 (or MG90S) 5 V servo motors – 2
- LDR sensors, 5 mm, typical dark resistance in the 100 kΩ–1 MΩ range – 4
- 0.96" SSD1306 I2C OLED (128×64, 3.3 V, often address 0x3C) – 1
- Small 6 V hobby solar panel (about 1 W; open-circuit a little over 6 V) – 1
- Resistors: 22 kΩ (panel series) – 1, and 10 kΩ (panel lower leg) – 1
- Optional 10 nF ceramic capacitors across each ADC node to ground – 3 (they quiet the readings)
- Tactile push-button (optional AUTO/HOLD) – 1
- External 5 V DC supply, 2 A, for the servos – 1
- Pan-tilt bracket for two SG90 servos (ready-made, 3D-printed or built from light plywood/card)
- A plus-shaped shade between the four LDRs (card, foam-board or 3D-printed) so each sensor sees a different quarter of the sky
- Jumper wires, a breadboard for the electronics, and a USB cable for the Pico
Wiring Table (Pin-to-Pin)
| Part | Part Pin / Node | Pico Pin | Notes |
|---|---|---|---|
| OLED SSD1306 | VCC | 3V3 | 3.3 V only |
| OLED SSD1306 | GND | GND | Common ground |
| OLED SSD1306 | SDA | GP4 | I2C0 data (physical pin 6) |
| OLED SSD1306 | SCL | GP5 | I2C0 clock (physical pin 7) |
| LDR LEFT | one lead | 3V3 | Top of the horizontal divider |
| LDR LEFT | other lead | GP26 and LDR RIGHT | Midpoint of the left/right pair (ADC0, pin 31) |
| LDR RIGHT | other lead | GND | Bottom of the horizontal divider |
| LDR UP | one lead | 3V3 | Top of the vertical divider |
| LDR UP | other lead | GP27 and LDR DOWN | Midpoint of the up/down pair (ADC1, pin 32) |
| LDR DOWN | other lead | GND | Bottom of the vertical divider |
| Solar panel | + | 22 kΩ, then GP28 | Series resistor into ADC2 (pin 34) |
| Divider | 10 kΩ from GP28 | GND | Lower leg. 22 kΩ + 10 kΩ keeps a 6 V panel under 3.3 V at the ADC |
| Solar panel | − | GND | Join to Pico ground |
| Pan servo | Signal (usually orange) | GP14 | 50 Hz PWM (physical pin 19) |
| Pan servo | VCC (usually red) | External +5 V | Do not take this from Pico 3V3 |
| Pan servo | GND (usually brown) | Pico GND and supply GND | Common ground is required |
| Tilt servo | Signal (usually orange) | GP15 | 50 Hz PWM (physical pin 20) |
| Tilt servo | VCC (usually red) | External +5 V | Share the 2 A servo supply |
| Tilt servo | GND (usually brown) | Pico GND and supply GND | Common ground |
| HOLD button | one contact | GP16 | Internal pull-up; physical pin 21 |
| HOLD button | other contact | GND | Tap to freeze or resume |
| Pico | VBUS / USB | USB computer or 5 V | Powers only the Pico and the 3.3 V rail |
The Pico’s ADC pins are GP26, GP27 and GP28 only. GP29 is not a free analog input — it is used on the board to measure VSYS — so do not plan a fourth independent LDR channel without an analog multiplexer. Pairing the sensors, as in the table, is the intended design.
Never let the voltage on GP28 go above 3.3 V. With 22 kΩ and 10 kΩ the ADC sees about 31 percent of the panel voltage, so the panel can rise to about 10.5 V before the pin is out of range. That covers a typical 6 V hobby panel, including its higher open-circuit voltage in full sun. A 12 V panel needs a taller top resistor (for example 33 kΩ + 10 kΩ) and a matching change to R_TOP_OHM in the code.
Connection Diagram
The diagram below matches the wiring table. The generated illustration is the featured image for this post; the labelled drawing under it is the pin-accurate version to build from.

MicroPython Code for the Raspberry Pi Pico
Flash official MicroPython onto the Pico with Thonny (Interpreter: MicroPython (Raspberry Pi Pico)). Install the SSD1306 driver from Thonny’s package manager (search for ssd1306) or copy ssd1306.py onto the board. Save the program below as main.py if you want it to start whenever the Pico gets power, or as tracker.py and run it from Thonny while you tune the dead-band.
"""
Raspberry Pi Pico Dual-Axis Solar Tracker
MicroPython, SSD1306 OLED, two SG90 servos, four LDRs, panel voltage sense
Hardware (GP numbers as printed on the Pico silk):
Horizontal LDR pair midpoint -> GP26 (ADC0)
Vertical LDR pair midpoint -> GP27 (ADC1)
Panel divider midpoint -> GP28 (ADC2)
Pan servo signal -> GP14
Tilt servo signal -> GP15
OLED SDA -> GP4 (I2C0)
OLED SCL -> GP5 (I2C0)
Optional AUTO/HOLD button -> GP16 to GND (internal pull-up)
OLED VCC -> 3V3
LDR pair tops -> 3V3
All grounds together, including the external 5 V servo supply
Copy ssd1306.py onto the Pico (Thonny: Tools > Manage packages > ssd1306)
or from micropython-lib. Save this file as main.py to run on power-up.
"""
from machine import Pin, ADC, PWM, I2C
from ssd1306 import SSD1306_I2C
import time
# --- pins ---
ADC_H_PIN = 26 # left / right LDR pair
ADC_V_PIN = 27 # up / down LDR pair
ADC_PANEL_PIN = 28 # solar panel voltage divider
PAN_PIN = 14
TILT_PIN = 15
SDA_PIN = 4
SCL_PIN = 5
BTN_PIN = 16 # tap to freeze or unfreeze the servos
# --- tracker tuning ---
# ADC is 16-bit via read_u16() (0 to 65535). Equal light is near 32768.
DEADBAND = 2500 # ignore small differences so the frame does not jitter
STEP_DEG = 1 # degrees to move each loop while light is off-centre
LOOP_S = 0.04 # seconds between moves
INVERT_PAN = False # True if the pan servo runs away from the light
INVERT_TILT = False # True if the tilt servo runs away from the light
PAN_MIN, PAN_MAX = 10, 170
TILT_MIN, TILT_MAX = 20, 160 # keep the panel from slamming the bracket
# SG90 pulse width. Many horns want 0.5 ms (0 deg) to 2.5 ms (180 deg).
PULSE_MIN_MS = 0.5
PULSE_MAX_MS = 2.5
PWM_HZ = 50 # 20 ms period
# Panel divider: 22 kOhm from panel+ to ADC, 10 kOhm from ADC to GND.
# Vpanel = Vadc * (RTOP + RBOT) / RBOT
R_TOP_OHM = 22000
R_BOT_OHM = 10000
ADC_VREF = 3.3
OLED_W, OLED_H = 128, 64
OLED_ADDR = 0x3C # try 0x3D if the screen stays blank
adc_h = ADC(ADC_H_PIN)
adc_v = ADC(ADC_V_PIN)
adc_panel = ADC(ADC_PANEL_PIN)
pan_pwm = PWM(Pin(PAN_PIN))
tilt_pwm = PWM(Pin(TILT_PIN))
pan_pwm.freq(PWM_HZ)
tilt_pwm.freq(PWM_HZ)
button = Pin(BTN_PIN, Pin.IN, Pin.PULL_UP)
i2c = I2C(0, sda=Pin(SDA_PIN), scl=Pin(SCL_PIN), freq=400000)
oled = None
try:
oled = SSD1306_I2C(OLED_W, OLED_H, i2c, addr=OLED_ADDR)
except Exception as exc:
print("OLED not found:", exc)
print("I2C scan:", i2c.scan())
def angle_to_duty(angle):
"""Convert 0-180 degrees to a 16-bit PWM duty for a 50 Hz servo."""
if angle < 0:
angle = 0
if angle > 180:
angle = 180
pulse_ms = PULSE_MIN_MS + (angle / 180.0) * (PULSE_MAX_MS - PULSE_MIN_MS)
period_ms = 1000.0 / PWM_HZ
return int(65535 * pulse_ms / period_ms)
def set_servos(pan_deg, tilt_deg):
pan_pwm.duty_u16(angle_to_duty(pan_deg))
tilt_pwm.duty_u16(angle_to_duty(tilt_deg))
def read_panel_volts():
raw = adc_panel.read_u16()
v_adc = (raw / 65535.0) * ADC_VREF
return v_adc * (R_TOP_OHM + R_BOT_OHM) / R_BOT_OHM
def clamp(value, lo, hi):
if value < lo:
return lo
if value > hi:
return hi
return value
def draw(pan_deg, tilt_deg, raw_h, raw_v, volts, tracking):
if oled is None:
return
oled.fill(0)
oled.text("Pico Solar Track", 0, 0)
oled.hline(0, 10, OLED_W, 1)
mode = "AUTO" if tracking else "HOLD"
oled.text("H:{:5d} P:{:3d}".format(raw_h, int(pan_deg)), 0, 16)
oled.text("V:{:5d} T:{:3d}".format(raw_v, int(tilt_deg)), 0, 28)
oled.text("Panel:{:5.2f} V".format(volts), 0, 40)
oled.text("Mode: {}".format(mode), 0, 52)
oled.show()
def button_pressed():
"""True on a falling edge. Simple debounce."""
if button.value() == 0:
time.sleep_ms(30)
if button.value() == 0:
while button.value() == 0:
time.sleep_ms(10)
return True
return False
def main():
pan = 90.0
tilt = 90.0
tracking = True
set_servos(pan, tilt)
print("Dual-axis solar tracker starting")
print("I2C devices:", i2c.scan())
print("Tap GP16 to HOLD or resume AUTO")
while True:
if button_pressed():
tracking = not tracking
print("Mode:", "AUTO" if tracking else "HOLD")
raw_h = adc_h.read_u16()
raw_v = adc_v.read_u16()
volts = read_panel_volts()
if tracking:
err_h = raw_h - 32768
err_v = raw_v - 32768
# Midpoint rises when the first LDR of that pair (LEFT or UP) is brighter.
if err_h > DEADBAND:
pan += -STEP_DEG if INVERT_PAN else STEP_DEG
elif err_h < -DEADBAND:
pan += STEP_DEG if INVERT_PAN else -STEP_DEG
if err_v > DEADBAND:
tilt += -STEP_DEG if INVERT_TILT else STEP_DEG
elif err_v < -DEADBAND:
tilt += STEP_DEG if INVERT_TILT else -STEP_DEG
pan = clamp(pan, PAN_MIN, PAN_MAX)
tilt = clamp(tilt, TILT_MIN, TILT_MAX)
set_servos(pan, tilt)
draw(pan, tilt, raw_h, raw_v, volts, tracking)
time.sleep(LOOP_S)
if __name__ == "__main__":
main()
How to Build It
- Fit the pan servo in the base of a pan-tilt bracket so it turns the upper (tilt) servo. Fit the tilt servo so it pitches a flat plate that will hold the panel and the LDR head.
- Screw or tape the small 6 V panel to that plate. In front of the panel, stand a plus-shaped cardboard or 3D-printed shade about 3–4 cm tall. Glue one LDR in each quadrant: left, right, up and down. The shade is what gives each sensor its own view of the sky.
- On the breadboard, series-wire LDR LEFT and LDR RIGHT between 3.3 V and GND, and take the midpoint to GP26. Do the same with LDR UP and LDR DOWN to GP27.
- Wire the panel positive through 22 kΩ to GP28, and 10 kΩ from GP28 to GND. Connect the panel negative to Pico GND. Double-check this divider before you put the panel in strong sun.
- Connect the OLED: VCC to 3V3, GND to GND, SDA to GP4, SCL to GP5.
- Connect pan signal to GP14 and tilt signal to GP15. Power both servo red leads from the external 5 V / 2 A supply, and join that supply’s ground to a Pico GND pin. Do not power the servos from the Pico 3.3 V pin.
- Optionally wire a push-button from GP16 to GND.
- Plug the Pico into USB, install
ssd1306.py, upload the program and open the Thonny shell.
Testing the Tracker
- On start the OLED should show Pico Solar Track, angles near 90°, and a Mode line that says AUTO. The Thonny shell prints the I2C scan; you should see
60(that is 0x3C) if the OLED is present. - In even room light the H and V readings should sit somewhere near 25000–40000, depending on your LDRs. If one pair is stuck at 0 or 65535, that midpoint is shorted to ground or 3.3 V.
- Hold a flashlight close to the left LDR. The pan servo should turn the head toward the lamp. If it turns the other way, set
INVERT_PAN = Trueand run the program again. Repeat for the top LDR andINVERT_TILT. - When the lamp is centred, both errors should fall inside the dead-band and the servos should stop. If they keep hunting, raise
DEADBAND(try 3500 or 4000) or add the optional 10 nF capacitors. - Shine the lamp on the panel. The OLED Panel voltage should rise. A small 6 V panel under a desk lamp may only show a volt or two; in sunlight it should read closer to its rated voltage.
- Tap the GP16 button. Mode changes to HOLD and the servos freeze, even if you move the lamp. Tap again to go back to AUTO.
Troubleshooting
| Problem | Fix |
|---|---|
| OLED stays blank | Confirm SDA is on GP4 and SCL on GP5, and that VCC is 3.3 V. The shell’s I2C scan should list 60 (0x3C). If it lists 61, set OLED_ADDR = 0x3D. |
| Servos twitch or reset the Pico | They are almost certainly drawing power from USB through the Pico. Move both servo VCC leads to the external 5 V / 2 A supply and keep only the signal wires on GP14 and GP15. Share the grounds. |
| Servos run away from the light | Set INVERT_PAN or INVERT_TILT to True. Also check that LEFT really sits on the 3.3 V side of the horizontal pair, and UP on the 3.3 V side of the vertical pair. |
| H or V reading stuck at 0 or 65535 | The midpoint is open, or shorted to GND or 3.3 V. Recheck the series wiring of that LDR pair. |
| Tracker hunts back and forth | Increase DEADBAND, slow LOOP_S a little, or make the plus-shaped shade taller so each LDR is better isolated. |
| Panel voltage reads too high or the Pico resets in sun | The ADC pin may be going above 3.3 V. Recheck the 22 kΩ / 10 kΩ divider and that the lower 10 kΩ really goes to ground. Do not connect a 12 V panel to this divider. |
| Servo does not reach the mechanical ends | Change PULSE_MIN_MS and PULSE_MAX_MS slightly (for example 0.6 and 2.4) to match your horn. Keep PAN_MIN/TILT_MIN inside the bracket’s safe range. |
| Thonny cannot find the Pico | Hold the BOOTSEL button, plug the USB cable in, and copy a fresh MicroPython UF2 onto the RPI-RP2 drive. Use a data-capable cable. |
Ideas to Extend This Project
- Log panel voltage against time on the Pico’s on-board flash (or an SD card module) so you can compare a fixed panel with the tracking panel over a day.
- Add two limit switches or simply tighten
PAN_MIN,PAN_MAX,TILT_MINandTILT_MAXafter you measure the bracket with a protractor. - At night, when all four LDRs are dark and the panel voltage is near zero, park the head facing east so it is ready for sunrise.
- On a Pico W, add a tiny web page that shows the angles and panel volts. Use the placeholders
YOUR_WIFI_SSIDandYOUR_WIFI_PASSWORD— never hard-code a real password. - Swap the SG90s for metal-geared MG995 servos and a stiffer bracket if you move up to a larger panel. Recalculate the 5 V supply current first.
- Read all four LDRs separately with a 74HC4051 analog multiplexer if you want per-sensor values on the OLED as well as the pair differences.
FAQ
Why not give each LDR its own ADC pin? The Raspberry Pi Pico only exposes three analog inputs (GP26, GP27 and GP28). Pairing opposite LDRs as voltage dividers turns that limit into a feature: each ADC channel already carries the difference the tracker needs.
Will this work with a Pico W? Yes. The same GP numbers are on the Pico W. This program does not start Wi-Fi, so a Pico and a Pico W behave the same here.
Can I power the servos from VBUS (pin 40)? Only for a first no-load twitch test. Two SG90s under a panel will dip USB voltage and reset the Pico. Use the external 5 V / 2 A supply for any real run.
Does the control signal need to be 5 V? No. SG90 and MG90S servos accept the Pico’s 3.3 V PWM on the orange wire. Only the red wire needs 5 V.
What LDR value should I buy? A common 5 mm GL5528 (about 10 kΩ in bright light, hundreds of kilohms in the dark) works well. Matched sensors help, but the dead-band hides small mismatches.
Can I use this on a roof with a large module? No. This is a lightweight hobby tracker. A roof installation needs a rated structure, proper DC isolators and, in many places, a qualified installer.
Related Projects
- Raspberry Pi Pico Digital Weighing Scale with HX711
- IR Remote Controlled Stepper Motor with Arduino
- Automatic Plant Watering System with Arduino
- ESP32 Digital Spirit Level with MPU6050
Browse more Raspberry Pi Pico on Arduino Project Hub.
2 thoughts on “Raspberry Pi Pico Dual-Axis Solar Tracker with LDR Sensors, Servos & OLED (MicroPython)”