Go Down

Topic: Arduino solar tracker (Read 70844 times) previous topic - next topic


I tested your code with a libelium gps module and it works everywhere!


Cool project, glad it works OK with the GPS. I presume you are converting the output from the GPS to decimal or is it outputting in decimal?


Hey Mowcius,
I ordered a RTC kit, and I'm wondering if you wouldn't mind posting your updated tracker code that includes your RTC?

I also ordered and Ethernet shield, and I'll be working on doing HTTP post to SQL via PHP scripts, so my data is logged properly, so I will make sure and contribute my code once I get to that point.

Last night I welded up a solar panel 2-axis jig, so I can begin a sanity check of the hardware/software once my RTC arrives.


Jan 04, 2011, 06:56 pm Last Edit: Jan 04, 2011, 06:58 pm by mowcius Reason: 1
Umm, if I knew where I put it then I would :D

It's simple enough, I presume your RTC will be a DS1307, there are a few libraries around, easiest is probably DS1307.h then add some of this to your sketch:

Code: [Select]
#include <Wprogram.h>
#include <Wire.h>
#include <DS1307.h>

int rtc[7];

void setup()

void loop()

 for(int i=0; i<7; i++)
   if (rtc[0] < 10 && i==0){ Serial.print("0");}
   Serial.print(" ");




From the output of that sketch you will be able to work out what rtc[0] to rtc[6] are then you can assign them to variables.

You will also need a sketch to set the time on the RTC, there are a few of them around too.

Call RTC.get(rtc, true) at the start of the sun position calculation, then use rtc as the variables.

I'll see if I can find my code again, if not then I'll whip this back up into a sketch tonight - might add in the time set over serial code too.



Jan 06, 2011, 12:23 am Last Edit: Jan 06, 2011, 12:29 am by Risensun Reason: 1
I built a simple little light tracker for my controls class, I made a video of it at the request of the professor.  Its not nearly as complex as what you guys are planning, but it shows what you can do with a couple of photo-resistors.


It doesn't actually record the lights position, though this could be remedied by attaching a pot.  A 2-axis system would just be everything here except duplicated and turned 90 degrees.


I have been working hard on my solar tracking system this week, and have made great progress. All of the sun position calculations are based on Mowcius' code.
This uses a 20x4 HD44780 LCD for the display, and the AdaFruit motor driver shield (AFMotor.h library) to drive the two stepper motors. I built in logic to park the solar panels at night as well. I am a total hack programmer on my best day, so I greatly value any feedback or ways to tighten up the code. The last two remaining steps, once I receive the hardware (should be here this weekend) are to integrate the DS1307 RTC so I do not have to manually plug in UTC time each time I reset the Arduino. The next big step once my Ethernet shield arrives,  will be writing the Ele and Azimuth to SQL, via HTTP send and PHP scripts, which I haven't even touched yet, but seen others have done it.

Here's my full code:


My AdaFruit DS1307RTC arrived this weekend, so I built that and got it integrated into my code. Since I'm not using any of my analog ports for this particular project, I am able to use their suggestion of plugging it straight in to the Arduino's analog ports, and just set pin 17 HIGH and pin 16 LOW to power it. Below is a link to my current code, and I also have a second sketch that syncs the RTC to your PC's current time (of course you must set your PC to UTC, and in my case to offset the delay of compile time and upload time, I advance my PC clock 23 seconds, so it lands exactly on official UTC time when it writes to the RTC). Hope this helps! I should have my AdaFruit motor shield and my two stepper motors next week, so I can begin doing a sanity check on all of this madness :)



Eventually I will eliminate the separate clock sync sketch and just have a line in the main sketch that you can uncomment to sync the RTC. I just did this because the AdaFruit library in the example on her site drove me nuts (not because it's bad, but because I'm not a developer), so I ended up using the DS1307RTC.h and Time.h libs instead.


Hi guys, I am continuing to work hard on the Solar Tracker, and I need some senior level programming help at this point. I have been working with some guys in another thread to get my ENC28J60-based ethernet shield working, and that is all good now, but now I am at a cross-roads with the sunPos() Function donated by Mowcius. I am writing data to a php script via the "unofficial" ethernet shield, in order to log to a SQL database. The problem is, I can write data to php all day long, but as soon as I compile the sketch to tell it to call to the sunPos() function, and do the sun position calculations, it breaks. My feeling is this is some sort of memory overflow, due to the heavy duty calculations being performed by that function. What can I do to troubleshoot and fix this?
http://bishoptechnology.com/pub/SunTrackerSQL.pde is my sketch for reference.


Jan 18, 2011, 09:42 pm Last Edit: Jan 18, 2011, 09:43 pm by mowcius Reason: 1
Ok - Not completely sure of what the issue will be but I also suspect it is a RAM issue.

Some things you can make smaller:

Do all of these need to be ints? Can they be bytes?
Code: [Select]
       int ElevInt = 0;
       int AziInt = 0;
       int ElevFrac = 0;
       int AziFrac = 0;
       int AziStep = 0;
       int ElevStep = 0;
       int AziPrevious = 0;
       int ElevPrevious = 0;
       int AziOneDegree = 10;   // Enter number of steps to physically move your rig 1 degree Azimuth.
       int ElevOneDegree = 20;   // Enter number of steps to physically move your rig 1 degree Elevation.

I would also move a load of these into the sunPos() function as they are only needed in that function:
Code: [Select]
     float ZenithAngle;
     float Azimuth;
     float RightAscension;
     float Declination;
     float Parallax;
     float ElevationAngle;
     float ElapsedJulianDays;
     float DecimalHours;
     float EclipticLongitude;
     float EclipticObliquity;

See how that goes to start with.



I actually sorted this out just a few minutes ago: sunPos() is actually not the problem, the issue was any reference to delay() was breaking my ethernet shield's /GET to my php script. I'm not sure what the deal is with that. I also noticed if I had anything in the loop() after the php script, it would fail, so I either moved everything before it, or in some cases broke them out into another Function. At this point everything is working 100% normal.

My last step before getting this thing functional and outside, is to complete the brackets for the two stepper motors on the tracking rig, and also when I get home tonight, verify that my stepper motors are actually running as the code says it is :)


Well I would possibly move/change some of the variables anyway but glad you got it working.



Thanks Mowcius, I went ahead and moved those "sunPos()" specific vars into that Function, with the exception of "ElevationAngle" and "Azimuth", as I refer to those in other functions for calculating the stepper motors' motion.

My last hurdle before getting this rig operational is working out some hardware compatibility issues, due to the number of pins required for all the things I have slapped on to my Arduino! (wishing I had a Mega..!) I have a RTC module, Ethernet, the AdaFruit motor board, and my HD44780-based LCD display. Except for the RTC, they all use a fair amount of I/O pins, so I'm seeing what I can do to shuffle things around and make it all work on one board.

Worst-case scenario, I will break the Ethernet off to a separate "data logging only" Arduino, and just talk to the Arduino that is running the steppers via I2C bus.


Well I'm not sure what all of that uses pin wise but I bet you can get it all on one board with a bit of fiddling :)

Perhaps use an external controller for the display (I presume you are already using 4 bit mode and grounding RW) but using an ATtiny chip or similar might be the best way to go (or just get a serial backpack if you don't want to do it yourself).



Jan 19, 2011, 11:54 pm Last Edit: Jan 19, 2011, 11:57 pm by joeinbend Reason: 1
Hey there Mowcius,
I'm wondering if you wouldnt mind having a look at my sunPos() function. I'm having some strange incidents where about once per hour, the calculation for Azimuth will actually be a lower number rather than higher. It is still within the same "degree", so it is not off by much, but I am not understanding why it is happening. For example, this is a snapshot from
MySQL database. you can see it has calculated a lower Azimuth:

Here is a link to my full code:

Looking at my records for yesterday, I cannot find any incidents of that happening, so I am wondering if this occurred as a result of moving those float's into the sunPos() function; what do you think?


No idea on that one... sorry


I went ahead and moved those floats out of the function and I'll see how it does over the next day. It was doing that about once an hour, but not on any exacting schedule, and it has not miscalculated since I moved those values out. Strange! I guess as long as it works... :)

I'll be working this weekend on getting my hardware to play nice together, and hope to have a functioning rig soon. Will update and post pictures!

Go Up