Sun and Moon Times

Sun and Moon Times © GPL3+

The software library to calculate moon age, sunrise and sunset time.

  • 45 respects

Components and supplies

Apps and online services

About this project


Some Arduino projects may improve its functionality by calculating sunrise and sunsets times or current moon age. You can create the weather station or animated clock that starts an animation in the morning and stops it at sunset time. There was one software library for Arduino on the Internet that could calculate some astronomical events, but actually it is not compatible with the current Arduino IDE version and the Time library, which is very convenient to manipulate the time in Arduino sketches. The new sun-moon library can help you create such a interesting project.

Let's build simple schematics on the breadboard.

The component list is very short, you can use any Arduino board you like: Nano or Uno, the breadboard and wires. The most valuable component of this project is a real time clock module. The DS3231 module has a high accuracy and uses only two wires to connect to the Arduino through the I2C bus.

The data sheet of DS3231 module states that the module requires power supply of 5V. In this case you should use the backup accumulator LIR2032 which is not widely available and expensive. In any hand, the clock module is proven to work at 3.3v and in this case you can use the battery CR2032 for backup.

Do not put CR2032 battery in to the clock module in case of using 5V power supply, the battery can be damaged.

The sunMoon class of the software library has the following methods:

  • julianDay - return Julian day number for the time specified or for the current time if no argument specified;
  • sunRise and sunSet functions return the time of sunrise and sunset of the current day or the day specified by the time argument;
  • moonDay - return the moon age in the days;

The sketch gives the handful example of using these functions.

Hope this library helps you build creative projects.


The exampleArduino
Example how to use the library
#include <Time.h>
#include <TimeLib.h>
#include <DS3232RTC.h>
#include <sunMoon.h>

#define OUR_latitude    55.751244               // Moscow cordinates
#define OUR_longtitude  37.618423
#define OUR_timezone    180                     // localtime with UTC difference in minutes

sunMoon  sm;

void printDate(time_t date) {
  char buff[20];
  sprintf(buff, "%2d-%02d-%4d %02d:%02d:%02d",
  day(date), month(date), year(date), hour(date), minute(date), second(date));

void setup() {
  tmElements_t  tm;                             // specific time

  tm.Second = 0;
  tm.Minute = 12;
  tm.Hour   = 12;
  tm.Day    = 3;
  tm.Month  = 8;
  tm.Year   = 2016 - 1970;
  time_t s_date = makeTime(tm);
  setSyncProvider(RTC.get);                     // the function to get the time from the RTC
  if (timeStatus() != timeSet) 
    Serial.println("Unable to sync with the RTC");
    Serial.println("RTC has set the system time");
  sm.init(OUR_timezone, OUR_latitude, OUR_longtitude);

  Serial.print("Today is ");
  printDate(RTC.get()); Serial.println("");
  uint32_t jDay = sm.julianDay();               // Optional call
  byte mDay = sm.moonDay();
  time_t sRise = sm.sunRise();
  time_t sSet  = sm.sunSet();
  Serial.print("Today is "); Serial.print(jDay); Serial.println(" Julian day");
  Serial.print("Moon age is "); Serial.print(mDay); Serial.println("day(s)");
  Serial.print("Today sunrise and sunset: ");
  printDate(sRise); Serial.print("; ");
  printDate(sSet);  Serial.println("");

  Serial.print("Specific date was ");
  printDate(s_date); Serial.println("");
  jDay = sm.julianDay(s_date);
  mDay = sm.moonDay(s_date);
  sRise = sm.sunRise(s_date);
  sSet  = sm.sunSet(s_date);
  Serial.print("Specific date sunrise and sunset was: ");
  Serial.print("Julian day of specific date was "); Serial.println(jDay);
  Serial.print("Moon age was "); Serial.print(mDay); Serial.println("day(s)");
  printDate(sRise); Serial.print("; ");
  printDate(sSet);  Serial.println("");


void loop() {
  // put your main code here, to run repeatedly:



The schematics
The schematics is obvious, just connect ds3231 clock to arduino


Similar projects you might like

Tiny Calendar and Clock Featuring Moon Phase in a LED Matrix

Project showcase by LAGSILVA

  • 41 respects

Solar Panel Sun Tracker - Phone Charger

Project tutorial by FIELDING

  • 263 respects

Sun Tracking Solar System

Project tutorial by Mayur Rabadiya

  • 27 respects

Arduino Bluetooth Robot for Android Device

Project showcase by aip06

  • 23 respects
Add projectSign up / Login