Moon Phase Calculator With OLED Display Using Arduino:
In this post, we will discuss How to make Moon Phase Calculator With OLED Display Using Arduino. This small project displays the moon phase on an 0.96″ OLED display. Eight phases are shown with a short description, the days remaining to the next full moon is also displayed.
This project uses a real-time clock RTC Module DS3231 or DS1307 to provide the date. This data is converted into a Julian date. By choosing a date that had a known full moon, in this case, 1st January 1972, the number of days from this starting point until now is easily calculated.
Why does the Moon have phases?
The Moon has phases because it orbits Earth, which causes the portion we see illuminated to change. The Moon takes 27.3 days to orbit Earth, but the lunar phase cycle (from new Moon to new Moon) is 29.5 days. The Moon spends the extra 2.2 days “catching up” because Earth travels about 45 million miles around the Sun during the time the Moon completes one orbit around Earth.
At the new Moon phase, the Moon is so close to the Sun in the sky that none of the side facing Earth is illuminated (position 1 in the illustration). In other words, the Moon is between Earth and Sun. In the first quarter, the half-lit Moon is highest in the sky at sunset, then sets about six hours later (3). At full Moon, the Moon is behind Earth in space with respect to the Sun. As the Sunsets, the Moon rises with the side that faces Earth fully exposed to sunlight (5).
Components Required:
1. 0.96″ OLED Display
2. Arduino UNO Board
3. RTC Module DS3231 or DS1307
4. Breadboard
5. Jumper Wires
Circuit Diagram & Connections:
Both the OLED and RTC Module DS3231 is powered by 5V Power Supply. The SCL and SDA Pins of OLED & DS3231 is connected to SCL and SDA pins of Arduino, i.e A4 & A5.
Designing the Moon Phase Calculator:
The moon takes 29.53 days to complete the cycle from one full moon to the next, so if we divide the number of days since 1st January 1972 by 29.53 the remainder will be the number of days in this lunar month so far.
The sketch divides the lunar month into eight parts, starting with a full moon then waning gibbous, last quarter, crescent old, new moon, crescent new, first quarter, waxing gibbous and back to a full moon. The number of days to the next full month is also shown.
Results & Display:
Once the code is uploaded to Arduino UNO board for Moon Phase Calculator With OLED Display Using Arduino, the moon phase will be shown and also the day will be displayed as 1, 2, 3, 4 ……..28 as shown in the figure below.
Source Code/Program:
First, add the two libraries, i.e U8glib Library & RTC Library else the code won’t compile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
#include "U8glib.h" #include <SPI.h> #include <Wire.h> #include "RTClib.h" U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C setup u8g object RTC_DS1307 RTC; static unsigned char full_moon_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x80, 0xff, 0x7f, 0x00, 0xe0, 0xff, 0xff, 0x01, 0xf0, 0xff, 0xff, 0x03, 0xf0, 0xff, 0xff, 0x03, 0xf8, 0xff, 0xff, 0x07, 0xfc, 0xff, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x0f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfc, 0xff, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x07, 0xf0, 0xff, 0xff, 0x03, 0xf0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x01, 0x80, 0xff, 0x7f, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00 }; static unsigned char waning_gibbous_bits[] = { 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x80, 0xff, 0xff, 0x00, 0xe0, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xff, 0xff, 0x0f, 0x00, 0xff, 0xff, 0x0f, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xf8, 0xff, 0x1f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf0, 0xff, 0x3f, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0xf8, 0x1f, 0x00 }; static unsigned char last_quarter_bits[] = { 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xff, 0x07, 0x00, 0x80, 0xff, 0x01, 0x00, 0xe0, 0xff, 0x01, 0x00, 0xf0, 0xff, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0x00, 0xf0, 0xff, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x80, 0xff, 0x01, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xf8, 0x0f, 0x00 }; static unsigned char crescent_old_bits[] = { 0x00, 0xf8, 0x07, 0x00, 0x00, 0xff, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xf8, 0x07, 0x00 }; static unsigned char new_moon_bits[] = { 0x00, 0xf8, 0x07, 0x00, 0x00, 0x07, 0x38, 0x00, 0x80, 0x00, 0x40, 0x00, 0x60, 0x00, 0x80, 0x01, 0x10, 0x00, 0x00, 0x02, 0x08, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x02, 0x60, 0x00, 0x80, 0x01, 0x80, 0x00, 0x40, 0x00, 0x00, 0x07, 0x38, 0x00, 0x00, 0xf8, 0x07, 0x00 }; static unsigned char crescent_new_bits[] = { 0x00, 0xf8, 0x07, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x7e, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0xf8, 0x07, 0x00 }; static unsigned char first_quarter_bits[] = { 0x00, 0xfc, 0x07, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xe0, 0x7f, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0xc0, 0xff, 0x03, 0x00, 0x80, 0xff, 0x07, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfc, 0x1f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfe, 0x3f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x07, 0x00, 0x80, 0xff, 0x07, 0x00, 0xc0, 0xff, 0x03, 0x00, 0xe0, 0xff, 0x01, 0x00, 0xe0, 0x7f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xfc, 0x07, 0x00 }; static unsigned char waxing_gibbous_bits[] = { 0x00, 0xfc, 0x07, 0x00, 0x00, 0xff, 0x3f, 0x00, 0xc0, 0xff, 0x7f, 0x00, 0xc0, 0xff, 0xff, 0x01, 0x80, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x03, 0xc0, 0xff, 0xff, 0x01, 0xc0, 0xff, 0x7f, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0xfe, 0x07, 0x00 }; // String nfm = ""; // days to next full moon void setup() { Serial.begin(9600); RTC.begin(); if (! RTC.isrunning()) { Serial.println("RTC is NOT running!"); } // following line sets the RTC to the date & time this sketch was compiled // un REM the line below to set clock, then re REM it // and upload this sketch again //RTC.adjust(DateTime(__DATE__, __TIME__)); // Wire.begin(); } void loop() { //draw loop u8g.firstPage(); do { drawMoon(); } while( u8g.nextPage() ); // rebuild the picture after some delay delay(50); } void drawMoon(void){ DateTime now = RTC.now(); u8g.setFont(u8g_font_profont12); u8g.setFont(u8g_font_5x7); u8g.drawStr(15,10, "Moon Phase Calculator"); u8g.drawLine(0,13,128,13); int mp = moon_phase(); u8g.setFont(u8g_font_profont15); switch (mp){ case 0: u8g.drawStr(15,61, " Full Moon "); u8g.drawXBM(45,18,30,30,full_moon_bits); break; case 1: u8g.drawStr(15,61, "Waning Gibbous"); u8g.drawXBM(45,18,30,30,waning_gibbous_bits); break; case 2: u8g.drawStr(15,61, " Last Quarter "); u8g.drawXBM(45,18,30,30,last_quarter_bits); break; case 3: u8g.drawStr(15,61, " Old Crescent "); u8g.drawXBM(45,18,30,30,crescent_old_bits); break; case 4: u8g.drawStr(15,61, " New Moon "); u8g.drawXBM(45,18,30,30,new_moon_bits); break; case 5: u8g.drawStr(15,61, " New Crescent "); u8g.drawXBM(45,18,30,30,crescent_new_bits); break; case 6: u8g.drawStr(15,61, " First Quarter"); u8g.drawXBM(45,18,30,30,first_quarter_bits); break; case 7: u8g.drawStr(15,61, "Waxing Gibbous"); u8g.drawXBM(45,18,30,30,waxing_gibbous_bits); break; } const char* newNfm = (const char*) nfm.c_str(); u8g.drawStr(110,30, newNfm); } int moon_phase(){ // calculates the age of the moon phase(0 to 7) // there are eight stages, 0 is full moon and 4 is a new moon DateTime now = RTC.now(); double jd = 0; // Julian Date double ed = 0; //days elapsed since start of full moon int b= 0; jd = julianDate(now.year(), now.month(), now.day()); //jd = julianDate(1972,1,1); // used to debug this is a new moon jd = int(jd - 2244116.75); // start at Jan 1 1972 jd /= 29.53; // divide by the moon cycle b = jd; jd -= b; // leaves the fractional part of jd ed = jd * 29.53; // days elapsed this month nfm = String((int(29.53 - ed))); // days to next full moon b = jd*8 +0.5; b = b & 7; return b; } double julianDate(int y, int m, int d){ // convert a date to a Julian Date} int mm,yy; double k1, k2, k3; double j; yy = y- int((12-m)/10); mm = m+9; if(mm >= 12) { mm = mm-12; } k1 = 365.25 *(yy +4172); k2 = int((30.6001 * mm) + 0.5); k3 = int((((yy/100) + 4) * 0.75) -38); j = k1 +k2 + d + 59; j = j-k3; // j is the Julian date at 12h UT (Universal Time) return j; } |
4 Comments
Runs well however, display is off by about 2 days! Shows First Qtr with 6b days to Full Mn, should be Wax Gibb with 5 days to Full Mn……………….
How can i increase number of phases from 8 to 16?
what do you mean? there are only 8 phases
You can add more phases(I have 12 – 6 before new and 6 after). Change 8 to 12 in last computing b int. It works.