Solar Tracker

[work in progress]

Arduino project: have a solar panel find and follow the sun

Many solar tracking projects use four Light Dependent Resistors for finding the brightest spot in the sky, but for the ever changing Dutch skies I chose a different approach:

  • Calculate the sun’s azimuth and inclination, based on geographical position and the current date and time.
  • Attach an absolute orientation sensor to the solar panel to tell Arduino its orientation.
  • Have two stepper motors (pan and tilt) rotate the panel towards the sun’s position and then follow its path over the celestial sphere.
  • Send Arduino to sleep at sunset and wake it up again at sunrise.


For a start, I will hard code my geographical position in the sketch and use a DS3231 Real Time Clock module to tell Arduino the current date and time. Although a GPS module can give both position and time, it will certainly use more power.


The absolute orientation sensor I will use is the Adafruit BNO005. This module already takes care of the Kalman filtering, so I don’t have to include code for that in my Arduino sketch.

My reason for choosing stepper motors instead of servos is that the motors of a solar tracker will be idle for most of the time. My guess is that idle steppers use less power than idle servos. Moreover, I always find servos to be a bit nervous and noisy.

I will only use the steps per rotation info of the stepper motors during initial positioning of the panel. Once the panel is facing the sun, intermediate adjustments will never need more than one step for one or both motors. The direction of those single steps comes from the orientation sensor. So the more interesting part of the sketch will be the initial positioning, at least if you want it to happen as efficiently as possible. Here’s the plan:

I. Horizontal rotation (pan)

At startup, the orientation sensor will tell the Arduino the horizontal (compass) angle that the panel is pointing at (Ap). The Arduino reads the current date and time from the RTC and calculates the sun’s azimuth As (its compass direction). Now it can calculate the number of degrees it has to rotate the panel to give it the same compass direction as the sun.

We obviously don’t want rotations over 180 degrees ore more because in that case rotation in the opposite direction would be more efficient.

If  Adif = As – Ap  then we can calculate the (efficient) number of degrees to move:

     ToGo(Adif) = Adif – 360 * int(Adif/180)   where 360<Adif<360

This graph may help:

ToGo(x) = x – 360 * int(x/180)  (-360<x<360)

In Arduino code this would be something like:


Now that we can efficiently adjust the panel in horizontal direction (pan), we could let our sketch do this first, and then adjust its vertical orientation (tilt). The following example shows that this wouldn’t be the best strategy.

Imagine the situation where the sensor’s compass direction is 0 degrees (North) and the sun’s azimuth is 180 degrees (South). In our formula, that would mean that Adif = 180, so according to our formula we would have to rotate the panel horizontally over -180 degrees.Let’s further imagine that our sensor’s tilt angle is 120 degrees (90 degrees meaning facing upwards) and the current sun’s elevation angle is 60 degrees. So, after adjusting the panel horizontally over the maximum 180 degrees, we would also have to tilt it over another 60 degrees. But, the panel in our situation was already facing the sun right from the start! Although its mounting (and the sensor) was facing North, it’s panel was tilted ‘backwards’, looking right at the sun.

What we learn from this extreme example is that finding the most efficient adjustment  depends on the current status in both orientations together. We have to calculate the steps needed in two scenarios (1. horizontal before vertical and 2. vertical before horizontal) and then pick the cheapest scenario.


II. Vertical rotation (tilt)

Let Ep and Es be the elevation angles of the panel and of the sun, respectively. We are only interested in Es values between 0 and 90 degrees because that’s when the sun is visible.

As we saw earlier, we have two scenarios for getting the right tilt angle. Depending on how we will rotate horizontally, the number of tilt steps needed will either be abs(Es-Ep) or abs(Ez+Ep-180).


III. Finding the best scenario

After some simple math, I think the algorithm for the most efficient initial panel adjustment would become something like this:


IV. Mounting

This is what I have in mind for the mechanical part of the project.

I will need a rectangular box, about the size of my solar panel. I will also need a U-shape mounting to hold the box so it can rotate around its horizontal axis. Finally I’ll need a ground plane that allows the U-shape mounting to rotate around the pan stepper’s (vertical) axis.

  • The solar panel will be on top of the cover of the box.
  • The orientation sensor will be at the back of the cover, exactly in line with the panel.
  • Apart from the Arduino, the box will also contain a LiPo battery, a LiPo charger, a Power Booster to deliver 5 volt, the Real Time Clock, two ULN2003 stepper motor drivers, the tilt stepper motor and a 6-wire slip ring.
  • The box can rotate around its horizontal axis in the U-shape mounting.
  • One end of the axis will be the tilt stepper’s shaft (the stepper is fixed inside the box, the shaft is fixated in the mounting).
  • The slip ring will form the other end of the axis. Its 6 wires will be used to control the second stepper (pan) and to power a 5 volt USB output connector.
  • The U-shape mounting will hold the pan stepper. Its shaft will go into the ground plane. It will be connected to the 6 wires from the slip ring.
  • The U-shape mounting will also hold a 5 volt USB output connector (the sun should not only power the tracker!).

By using a slip ring and continuous steppers, both pan and tilt rotations are unrestricted. If an external device is powered by the 5 volt USB output, we could attach it to the mounting or rely on the fact that solar tracking will have limited rotations anyway.


V. Intermezzo: calculating the position of the sun

Here’s an algorithm that I found on Internet (author unknown). Its accuracy is good enough for a solar tracker.

The following variables should be declared and be given values before calling sunPos():


This section must also be included in the main program of the sketch:


This is the sunPos function:


To be continued…