Firmware
Overview
The firmware does what most solar trackers do. It moves the array to keep the sun's rays square to the panels through the day, lays it flat when the wind rises, and returns it flat and facing east, ready for the next day. What is different here is how much of the code exists to check itself, and that all of it is written to be understood and built on.
Comments for Future Development
The firmware is a single Arduino INO file of more than 11,000 lines, and about two thirds of it is comments: roughly two lines of explanation for every line of code. That is deliberate, and it is for future development. Every decision is explained next to the code: what a block does, why it is done that way, and what was considered and rejected. The aim is that nobody modifying the firmware, including me in a year's time, is left guessing about the logic. There should be no mystery in it.
The comments cover the reasoning as well as the mechanics. Many explain why a check exists, what would go wrong without it, and what depends on a value, so that a change can be made with its consequences in view. Short excerpts from the code are in the gallery, with a zoomed-out picture of the whole file. The reference documents on the Documentation page go further, with dense technical references to the entire INO file.
Breaking It Into Modules
The same goal is why I would like to break out whatever can be separated into modules of their own, starting with the actuators. Other motors, drivers or mechanical designs could then be supported by changing one part and leaving the rest alone. I have not done this yet. If you have experience organizing Arduino or C++ code this way and would like to help or advise, I would like to hear from you at support@solartracker.diy.
Until then, the code is built around the actuators and geometry of this design, with 18" and 24" strokes. Stroke timings and drive levels are based on those lengths, so a different stroke would mean revisiting them.
Four Priorities
Every pass through the main loop works down an ordered list of priorities and acts on the first one that applies. They are not sequential blocks: When one applies, the ones below it are skipped for that pass. The flowchart in the gallery shows each decision.
Priority 1: System Fault. Anything that jeopardizes the electrical or mechanical safety of the system. The tracker stops where it is and stays stopped until someone inspects it and power-cycles it. This is the only priority that halts the system.
Priority 2: Manual Mode. The joysticks drive the tracker.
Priority 3: Wind Response. The array is laid flat and held there until conditions have settled.
Priority 4: Autonomous Mode. The array follows the sun, or parks for night and heavy cloud.
Checks That Run First
Two checks run before the priorities, in Auto and in Manual. One verifies the limit-switch wiring. The other watches the battery and, if it runs too low, lays the array flat and halts the system. Other health checks run on the same 8-second cycle. The motor drivers are tested while the motors are at rest: if either driver chip reports a protective shutdown, the system faults and refuses to move until it is power-cycled. The display and the memory chip are each probed over the I2C bus: if either stops responding, an advisory LED lights and the tracker carries on unaffected. The memory chip also gets a write test, so a failed write path is caught before the fault log silently stops recording.
Watchdog
A hardware watchdog resets the controller if the program ever hangs. The main loop resets it on every pass, so only a genuine lock-up trips it. A fault never clears itself. It stays latched until someone inspects the hardware and deliberately restarts the controller.
Fault Response and Logging
Rather than assume that a move worked, the firmware checks that it did and decides what to do if not. Failures have names. Each kind of fault lights its own indicator LED, and abnormal moves are recorded in non-volatile memory (FRAM) so that the history can be read later. A stow that cannot be confirmed is tried once more, except when the battery is low. If it still fails, the system halts because retrying indefinitely when the position is unknown could make things worse.
Telemetry and Serial Control
Over USB the firmware streams its output and accommodates commands (GET, SET, RESET, ACK) for state queries, setting the tunable constants and querying the fault log. It also sends JSON telemetry, which the application described on the Software page displays. A second, optional link sends the same data one way over WiFi for remote monitoring, described on the WiFi / Starlink page. The tracker cannot be addressed over that link.
Tunable Constants
Sixteen settings can be changed over the serial connection without re-compiling or re-flashing the NVM, such as tracking sensitivity, the length of each tracking step, the light level below which the array parks for overcast or night, the wind-stow speed, and the dawn and dusk hours. A change does not persist following a reset given they are not in the flashed code but in-operation modifications provide a means of adjusting the firmware behaviour for subsequent revision. Permanence dictates a recompile and reflash.
Unit Identity
Each tracker can be given a unit number from 0 to 99, set over the serial connection behind a confirmation step. It is reported as an identity such as SolarTracker_07. The number is kept in the Arduino Mega's own EEPROM rather than the FRAM, so it survives a normal re-flash and stays readable if the FRAM fails. A unit that has not been assigned reports SolarTracker_NN, and every telemetry line carries the identity along with a flag saying whether it is still the default.
On a single tracker this is mostly a label that says which machine a log or a screen came from. It matters more with two or more trackers. SolarLink is designed so that one SolarLink can monitor several, and the identity is how each stream is told apart and how each gateway's network name is matched to the tracker behind it. That is a design intention only. SolarLink is not built, and nothing here has been tried with more than one tracker.
Libraries and One Guarded Edit
The firmware relies on a handful of public Arduino libraries: Adafruit's for the display, the memory chip, the environment sensor and date and time arithmetic, and SparkFun's u-blox GNSS library for the GPS. They are installed through the Arduino Library Manager, and the comments say what each one is used for.
One of them needed an edit. The GPS reports its satellites through the SparkFun library, which is set up to store up to 255 satellites per report. That is just the largest number the report's one-byte count field can hold, not a number anyone expects to see: a busy sky, counting every satellite the receiver is tracking or merely searching for, runs about 30 to 45. At 255 the library reserves about 6,100 bytes for that one report, more than the Arduino Mega's 8,192 bytes can spare once the rest of the firmware is loaded. The memory is claimed while the program runs, so the compiler never warns. The result would be corruption in the field, with no build error to explain it.
So I edited the library's own header file and changed that constant from 255 to 64, which brings the reservation down to about 1,560 bytes. I didn't go lower, because if a report holds more satellites than the buffer, the library discards the whole report instead of keeping what fits, and the satellite data would silently stop updating. The receiver's datasheet does not state its true maximum, so 64 is my judgement, deliberately on the safe side.
Editing a library has one hazard: Updating the library overwrites the edit and puts 255 back without any sign, and the firmware would still compile. So the firmware code includes a check at compile time (a static assert) that the constant is 64. If it is not, the compile stops with a message that explains why. The fix is to edit the library's header file back to 64 and compile again. A library update becomes a build failure at the desk instead of a fault in the field. The check is for exactly 64, not a range, so a deliberate change to the value has to go back through the comments that explain the arithmetic.
Memory
Version 12.16 compiles to 153,968 bytes of program storage, 60% of the 253,952 bytes the Arduino Mega offers. Global variables take 2,851 bytes, 34% of its 8,192 bytes of dynamic memory (SRAM), leaving 5,341 bytes for everything that happens while the program runs. That second figure is the tight one: buffers claimed at run time, like the satellite report above, come out of it. The comments cost nothing in these figures, because the compiler discards them.
Versions
The firmware is at version 12.16. Version numbers are Major.Minor. By the project's own policy, a major step is for new hardware support or a change to fault, stow or limit-switch behaviour, and smaller features and logic changes are minor steps. Comment-only edits are not supposed to get a new number, but I didn't always hold to that, and a few small changes, comment edits among them, advanced the minor number anyway.
Status
The firmware is still being tested on the bench. So far it behaves as I expect, but it has not been proven in the field.