Author Topic: sun position calculation - how to make it more accurate  (Read 1233 times)


  • New Member
  • *
  • Posts: 6
    • View Profile
sun position calculation - how to make it more accurate
« on: March 17, 2014, 02:16:20 PM »
How about using unsigned long numbers instead of int in the HourAngleArray? This way you get many more significant digits into the calculation.

I tried different tools and for the 16th of March 2014 at 14:47 GMT + 1 (which is 13:47 UTC) and for the coordinates 846' E und 4925' N I get the following results:

28,291 - 240,184     helios library for arduino from
31,5     - 220,1
31,44  - 220,1
30.54  - 219.91     old arduino library from

So the last one is pretty close but still up to a degree off.

I know the Mega version is more precise but I would like to stick to the Uno specs.

Does it help to create an HourAngleArray for my lattitude and longitude?


  • Administrator
  • Hero Member
  • *****
  • Posts: 655
    • View Profile
Re: sun position calculation - how to make it more accurate
« Reply #1 on: March 18, 2014, 08:14:30 AM »
A lot of the numbers in the hour angle array are negative, so if you switch to unsigned you would lose that information. The hour angle array does cause some of the error because it is averaged over a period of many years. Ideally, you should have a new array for each year. I experimented with this though, and it didn't seem to make enough of a difference to be worth the trouble.

Most of the error seems to be due to the fact that trig functions are super accurate on the Arduino. For example, if I take these two equations and plug the same numbers into both the Arduino and a computer, I get slightly different answers. Those two equations are the source of most of the error it seems. Unfortunately, I don't think there isn't really any way around them.

     altitude = (asin(sin(latitude) * sin(delta) + cos(latitude) * cos(delta) * cos(h)))*180/pi;
     azimuth = ((atan2((sin(h)),((cos(h) * sin(latitude)) - tan(delta) * cos(latitude)))) + (northOrSouth*pi/180)) *180/pi;

The hour angle array is the same for every location. It's technically only accurate at Greenwich England, but it is made accurate for your location with this line of code:
h = h + longitude + (timezone * -1 * 15); 

You may have already seen this, but I did do some error checking for these calculations.
Depending on what you are doing, they may be accurate enough.

Hope that helps!