Created
August 29, 2018 07:11
-
-
Save elktros/0dcef8c76a3e9033c806dd02afbb2897 to your computer and use it in GitHub Desktop.
Code for interfacing DS3231 RTC (Real Time Clock) Module with Arduino.
This file contains 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 <LiquidCrystal.h> | |
#include "RTClib.h" | |
DateTime now; | |
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | |
RTC_DS3231 rtc; | |
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (rs, e, d4, d5, d6, d7) | |
void showDate(void); | |
void showTime(void); | |
void showDay(void); | |
void setup () | |
{ | |
Serial.begin(9600); | |
lcd.begin(16,2); | |
//delay(2000); | |
if (! rtc.begin()) | |
{ | |
Serial.println("Couldn't find RTC Module"); | |
while (1); | |
} | |
if (rtc.lostPower()) | |
{ | |
Serial.println("RTC lost power, lets set the time!"); | |
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
} | |
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
} | |
void loop () | |
{ | |
now = rtc.now(); | |
showDate(); | |
showDay(); | |
showTime(); | |
} | |
void showDate() | |
{ | |
lcd.setCursor(0,0); | |
lcd.print(now.day()); | |
lcd.print('/'); | |
lcd.print(now.month()); | |
lcd.print('/'); | |
lcd.print(now.year()); | |
} | |
void showDay() | |
{ | |
lcd.setCursor(11,0); | |
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]); | |
} | |
void showTime() | |
{ | |
lcd.setCursor(0,1); | |
lcd.print("Time:"); | |
lcd.print(now.hour()); | |
lcd.print(':'); | |
lcd.print(now.minute()); | |
lcd.print(':'); | |
lcd.print(now.second()); | |
lcd.print(" "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I was using this code but I found one observation when the date changes from 31/1/2022 to 1/2/2022, LCD shows the last digit as it is like this 1/1/20222.
Plz help me with this.