Created
August 25, 2017 14:08
-
-
Save iotguider/66936f807b9c66d30b7468f72f099ca9 to your computer and use it in GitHub Desktop.
Code for interfacing DS3231 RTC Module in Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> //include Wire.h library | |
#include "RTClib.h" //include Adafruit RTC library | |
RTC_DS3231 rtc; //Make a RTC DS3231 object | |
//Set the names of days | |
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; | |
void setup () { | |
Serial.begin(9600); //Begin the Serial at 9600 Baud | |
//Print the message if RTC is not available | |
if (! rtc.begin()) { | |
Serial.println("Couldn't find RTC"); | |
while (1); | |
} | |
//Setup of time if RTC lost power or time is not set | |
if (rtc.lostPower()) { | |
//Sets the code compilation time to RTC DS3231 | |
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
} | |
} | |
void loop () { | |
//Set now as RTC time | |
DateTime now = rtc.now(); | |
//Print RTC time to Serial Monitor | |
Serial.println(now.year(), DEC); | |
Serial.print('/'); | |
Serial.print(now.month(), DEC); | |
Serial.print('/'); | |
Serial.print(now.day(), DEC); | |
Serial.print(" ("); | |
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); | |
Serial.print(") "); | |
Serial.print(now.hour(), DEC); | |
Serial.print(':'); | |
Serial.print(now.minute(), DEC); | |
Serial.print(':'); | |
Serial.print(now.second(), DEC); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment