Skip to Content

← All functionalities

Two-axis solar trackersRaspberry PLC 21CANopenControl

Astronomical solar tracking with PyEphem on a Raspberry PLC 21

A two-axis solar tracker only pays off if it points at the sun all day without a light sensor that fogs up or drifts. This example uses PyEphem astronomical solar tracking on a Raspberry PLC 21: given latitude, longitude and altitude, it computes the sun's elevation and azimuth for any instant and updates both axes every 300 seconds. The code comes from a real two-axis solar tracker deployment, including the night rest position and the automatic re-arm at sunrise.

Why astronomical instead of sensor-based tracking

Photodiode trackers chase the brightest point in the sky, which on cloudy days can be a reflection or a gap in the clouds. An ephemeris calculation never gets confused: the sun's position is pure geometry from GPS coordinates and time. PyEphem solves it in microseconds on the PLC, so a 300-second update period costs essentially no CPU and needs no extra hardware.

From sun angles to axis setpoints

The observer object holds the plant's latitude, longitude and altitude. Two helper functions return elevation and azimuth in degrees for the current UTC time. Before commanding the motors, each setpoint is clamped to the mechanical limits of the axis — software end stops that protect the structure even if the math says the sun is somewhere the tracker cannot reach.

Night rest and sunrise re-arm

When solar elevation drops below 5 degrees, the tracker performs a single move to its rest position: table flat, facing east. A state flag prevents the rest maneuver from repeating every cycle through the night. At dawn, the same flag detects the sun back above the threshold and re-arms normal tracking automatically — no operator action and no cron job needed.

A snippet from the implementation

Straight from the example as deployed on the Raspberry PLC 21 — copy it freely:

def get_sun_angle(t):
    """Sun elevation (degrees) at instant t (UTC)."""
    obs.date = t.strftime('%Y/%m/%d %H:%M:%S')
    sun.compute(obs)
    return math.degrees(float(sun.alt))

The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.

Frequently asked questions

How accurate is PyEphem for solar tracking?

Ephemeris accuracy is far better than one tenth of a degree, well below the mechanical tolerance of any tracker. In practice the limiting factors are encoder calibration and structure backlash, not the astronomy.

Why update every 300 seconds instead of continuously?

The sun moves about 0.25 degrees per minute at most, so a 5-minute period keeps pointing error around one degree while drastically reducing motor starts, which extends the life of the drive and gearboxes.

Does the PLC need an internet connection for this to work?

No. PyEphem computes everything locally. You only need a reliable clock, so an RTC module or periodic NTP sync is recommended for installations without permanent connectivity.

Related functionalities