Arduino, GPS based Heliostat, Suntracker
Published: 2024-04-27 22:59:32


The heliostat is a device that, via a mirror, address the sunlight to a target.
In change, the suntracker always is pointing to the sun.
The heliostat is useful to adress the light to the interior of a house.
The suntracker is useful to setup a solar panel and collect as much energy as be possible. The system shown here can work in any of the two modes.


![Front View](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scbzslwhyvgkfn7u2n3t.jpg)

## How know where is the sun ?

Basically, there are two sun tracking alternatives: one is search the sun using light sensors, and the other is calculate the sun position.
The first alternative could be affected by clouds and rain, and for the second, we need a algorithm that takes in count the complexity of celestial mechanics. Every one has its pros and cons, this implementation uses the second method.

So to calculate the sun position a "C" language library is used:

[NREL's Solar Position Algorithm (SPA)](https://midcdmz.nrel.gov/spa/)

The description say: **"This algorithm calculates the solar zenith and azimuth angles in the period from the year -2000 to 6000, with uncertainties of +/- 0.0003 degrees based on the date, time, and location on Earth."**

The device that can give us those needed parameters is a GPS, it can calculate datetime, location and altitude.
Yes GPS can give time, compared with a RTC (real time clock)  they have many advantages, first it rely on a constellation of satellites, equipped with atomic clocks, so basically we have the precision of an atomic clock in our hands at an affordable price, also unlike of a RTC clock it does not need to be initialized. 
Just turn on and in a few second we have date time, altitude and location.


## Mechanical configuration

The mechanical configuration chosen is azimuth-elevation, dual axis. The vertical axis control azimuth, and the horizontal axis control elevation. The zero value of azimuth is in the side of the sunrise and 180 degrees in the side of the sunset. The zero value of elevation is when the mirror normal is pointing to the horizon, and 90 when the normal is pointing to up.


![Mechanics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mt1obsf3tbs6u1fqbda7.jpg)

![Bottom View](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vv4ff0jxun1yc24cd2e.jpg)

The system have a Pin hole to verify the right alignment with the sun.
So before working as heliostat the system need to be run as suntracker, and use the pin hole to test it and align to the sun. 

## Pointing to a target, the math behind

The target is where we want to address the sun light.
To do that we need to move the mirror at a specific position, this position
is the bisecting ray between the sun and the target.

The next image show how to transform from spherical coordinates (azimuth-elevation) to 3d cartesian (x,y,z).
Where:
 "S" is the sun unit vector.
 "t" is the target unit vector.
 "n" is the mirror unit vector.
 "γ" (gamma) is azimuth.
 "α" (alpha) is elevation. 


![Equations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9gped6mnnuvp7mrrqsf.png)


## The equations translated to code:

The next link point to the lines of source code where the target vector is calculated:

[Mirror Orientation Equations](https://github.com/tomsawyercode/SunTrackerSlave/blob/main/Tracker.cpp#L494)



## The electronic setup

The system is arranged in two modules one in charge of human machine interface, read gps values, calculate sun position and command the next movement, the other module is in charge of the mechanics, run the motors trough the drivers and listen the zero position career sensors.

The human machine interface is done through telnet via wifi, or with serial trough bluetooth, using [Android Serial Bluetooth Terminal](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=es_AR&gl=US)


![Electronic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0erbi8x1ctindw5kmcuo.jpg)

## Master  module
In order to run the sun position algorithm a 32bit processor is needed, I started with "lolin nodemcu esp8266" but sudenly have not enough space and then move to "Node MCU ESP32S". Also change the partition table to:**4MB no OTA**


Useful links for partition table:
https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/tutorials/partition_table.html
https://robotzero.one/arduino-ide-partitions/

## Slave module
The second module is an Arduino Mega 2560 with RAMPS 1.6 shield. 
The shield have connected two MKS expansor driver that control two MKS 6600 Drivers for the steps motors, and two optics end stops taken from a 3D printer.

The first module feed with commands the second trough an I2C connection. The GPS can be connected either to the Slave or to the Master. Trough the serial ports.

There isn't a wiring diagram because the connection are straightforward, also in the web there are a lot of tutorial of how to connect a GPS or I2C.

# The code


There are two modules, the ESP 32 work as a master and the Mega 2560 work as slave:

[ESP32](https://github.com/tomsawyercode/SunTrackerMaster)

[Arduino Mega 2560](https://github.com/tomsawyercode/SunTrackerSlave)



If it code was useful to you and you build one of these,better than my wood an L shape steel, please give me a comment with the links to the images.
 **Thanks**