Arduino library to calculate position of sun

This is coding from ken willmott. he uses a library called 'solar position.h'. on compiling I get error no such library. I have also found another library for setting the time and date 'timelib.h'. this library lets you set a time and date. I get the same error message on compiling.
I am thinking these libraries may not be on the standard arduino ide. You may have to find them and then download to the libraries.
Can anyone advise on this matter?

Please look at your code, the error is in line 42.

yes, sure

You have to find them and install to the Arduino IDE

The SolarCalculator library is quite easy to use.
Calculating sun elevation and azimuth only requires one line in loop().
calcHorizontalCoordinates(now, latitude, longitude, az, el);
SolarCalculator can be installed through the library Manager of the IDE.
"now" is "epoch", which you can get from NTP (internet) or a RTC.
Lat/Long (where you are) is the only other thing needed.

A working NTP clock with DST can be found here (click). Post#75 might be relevant.
I'm about to post an extension with sun angle, to dim the clock at night.
Leo..

Hi @petercl14 ,

in general there are two options to add libraries to the Arduino IDE:

  • Open the Library Manager, search for the appropriate lib and press the "install" button
  • Download a ZIP lib from e.g. github, use the IDE menue: Sketch/Include Library/Add ZIP Library

The first is the easiest possibility (in case the lib is listed):

image

The second option works likes this:

  • Search the internet for the lib (here an example for github):
    image

  • Press the green "code" button and choose to download as a ZIP file:
    image

  • Open the IDE menue

image

and click on Add .ZIP library

Open the library zip file you downloaded and it will be installed.

You may have to restart the IDE ...

Good luck!
ec2021

Hi ChatGPT

Tested solar position sketch, derived from internet time, with NodeMCU ESP8266.
Maybe it's useful.
Leo..

#include <ESP8266WiFi.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <SolarCalculator.h> // install all libraries through library manager
const float latitude = -45.53; // negative for southern hemisphere
const float longitude = 170.24; // positive for east of Greenwich
double az, el; // azimuth, elevation
unsigned long prevMillis, interval = 1e4; // 10 sec
bool reSync;
time_t now;

uint32_t sntp_update_delay_MS_rfc_not_less_than_15000 () {
  return 10 * 60 * 60 * 1000UL; // 10 hours
}

void time_is_set() { // sync callback
  Serial.println("TimeSync");
  reSync = true;
}

void setup() {
  Serial.begin(74880); // native baud rate
  Serial.println("\nESP8266 Sun Position Calculator");
  WiFi.mode(WIFI_STA);
  WiFiManager wm;
  wm.autoConnect("Portal"); // open, 192.168.4.1
  configTime(0, 0, "pool.ntp.org");
  settimeofday_cb(time_is_set); // enable callback
  while (!reSync) yield(); // wait here until a valid time string is received
}

void loop() {
  if (millis() - prevMillis > interval) {
    prevMillis = millis();
    calcHorizontalCoordinates(time(&now), latitude, longitude, az, el); // calculate sun angle
    Serial.print("Elevation: ");
    Serial.print(el);
    Serial.print("\tAzimuth: ");
    Serial.println(az);
  }
  delay(1);
}

Hi Wawa, It seems there is no solar position.h in the library but there is the solar calculator. Thanks for the coding. So you would need an internet connection to get a working NTP clock? I will go for a hardware RTC I think. Although you could use a Millis software program to give the time from the start of the program. This runs out in approximate 50 days and starts again. You might be able to use and 'if' statement to reset before 50 days. You can calculate your 'now' from this. Not sure how accurate the Millis would be.

Ken Willmott's Solar Position library.

Works well, very accurate and very easy to use.

1 Like

Most Arduinos don't have a real crystal clock, so millis() with those will drift.
A RTC needs to be set, but could be fairly accurate after that, if a DS3231 is used.
Internet time can be set and forget, and so is a GPS, and a GPS has lat/long available.
Any solar position calculator will work. I just picked one that was available through the IDE.
What is your project.
Leo..

No! That needs to be

uint32_t sntp_update_delay_MS_rfc_not_less_than_15000 () {
  return 10UL * 60 * 60 * 1000UL; // 10 hours
}

because 10 * 60 * 60 will exceed 32767 which is the maximum for an int, and so will wrap around and go negative. Or am I mistaken?

1 Like

int may be 16 bit - or not. Try Serial.println(sizeof(int)); to get the size in bytes for your platform.

Why these difficulties?
It's easier to immediately write as @odometer advised - it will work on any MCU

Sure you should. But if you never take a look closer look, what do you gain?

Taken from this site, tested, and it works as intended (10 hours).
I do understand int math and overflow.
In other clock code I pre-calculated it to "36e6UL",
but I left it here so OP could understand the calculation.
Leo..

No, it does not. The compiler is smart enough to promote all the constants in this expression, do the calculation, and load the correct, intended value for the function return.

return 10 * 60 * 60 * 1000UL; // 10 hours

It is easy enough to verify that for yourself, by experiment.

Hi Leo, Thats pretty complicated coding. I am looking for something simpler.

Build complicated things in stages. Move to the next stage when you understand. I think you should start with an Uno R3 and DC3132 RTC module, and see if you can display the time. Then it's only a matter of adding lat/long (where you are) and the sun position library.
Leo..

Hi Leo, seems to be a few good options here. GPS in particular looks good.
My project. I was going to use light dependant resistors and servo motors to move a solar panel to always face the sun. Then I thought it might be easier to track the position of the sun by using my position on the globe (latitude and longitude) and from this get the elevation and azimuth of the sun. These positions can then be mapped to software for the servo motor to move the solar panel to always face the sun.
You then don't need a LDR or other light sensitive device. You think this is feasible? Looking forward to comments from you smart guys on this forum. This method may already be used in some solar panel set ups. I would like to hear about those.

It has been done many times. Here is a solar concentrator (heliostat) example, that can also be used as a sun tracker.