#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Wire.h>
#define RTC_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Wire.begin(); // Initialize the I2C communication
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight of the LCD
}
void loop() {
Wire.beginTransmission(RTC_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(RTC_ADDRESS, 7); // Request 7 bytes of data
// Read data from the RTC module
int second = bcdToDec(Wire.read() & 0x7f); // Seconds (0-59)
int minute = bcdToDec(Wire.read()); // Minutes (0-59)
int hour = bcdToDec(Wire.read() & 0x3f); // Hours (0-23)
Wire.read(); // Day of the week (discard)
int day = bcdToDec(Wire.read()); // Day (1-31)
int month = bcdToDec(Wire.read()); // Month (1-12)
int year = bcdToDec(Wire.read()); // Year (0-99)
// Display the date and time on the LCD
lcd.setCursor(0, 0); // Set cursor to the first column of the first row
lcd.print("Date: ");
print2Digits(day);
lcd.print("-");
print2Digits(month);
lcd.print("-");
print2Digits(year);
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
lcd.print("Time: ");
print2Digits(hour);
lcd.print(":");
print2Digits(minute);
lcd.print(":");
print2Digits(second);
delay(1000); // Update once per second
}
int bcdToDec(int bcd) {
return ((bcd / 16) * 10) + (bcd % 16);
}
// Function to print a number with leading zero if necessary
void print2Digits(int number) {
if (number < 10) {
lcd.print("0");
}
lcd.print(number);
}