Skip to content

Instantly share code, notes, and snippets.

@elktros
Created August 29, 2018 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/0dcef8c76a3e9033c806dd02afbb2897 to your computer and use it in GitHub Desktop.
Save elktros/0dcef8c76a3e9033c806dd02afbb2897 to your computer and use it in GitHub Desktop.
Code for interfacing DS3231 RTC (Real Time Clock) Module with Arduino.
#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(" ");
}
@dthakurkm
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment