Arduino LCD 16x2 and RTC DS1302

This is an Arduino Clock, using a 16x2 I2C LCD Display and an RTC DS1302

// ========================================================================================================
// --- Auxiliary Libraries ---
#include <Wire.h>
#include <virtuabotixRTC.h>                    // Library for the RTC DS1302 
#include <LiquidCrystal_I2C.h>                 // Library for the I2C LCD


// ========================================================================================================
// --- Hardware Mapping ---
#define CLK 6 // Define the port where the CLK (Clock) of your RTC is connected
#define DAT 7 // Define the port where the DAT (Data) of your RTC is connected
#define RST 8 // Define the port where the RST (Reset) of your RTC is connected

#define I2C_ADDR 0x27 // Define the address of the LCD. If unknown, use the I2C_Scanner. Default addresses are 0x27 and 0x3F.
#define LCD_COLS 16 // Define the number of columns of your display. Default is 16.
#define LCD_ROWS 2 // Define the number of rows of your display. Default is 2.


// ========================================================================================================
// --- Auxiliary Constants ---
#define   Sec        0 // Index for Seconds in RTC Time Array
#define   Min       18 // Index for Minutes in RTC Time Array
#define   Hr        22 // Index for Hours in RTC Time Array
#define   D_Week     7 // Index for Day of the week in RTC Time Array
#define   D_Month    16 // Index for Day of the month in RTC Time Array
#define   Month      3 // Index for Month in RTC Time Array
#define   Year     2024 // Index for Year in RTC Time Array


// ========================================================================================================
// --- Object Declarations ---
virtuabotixRTC   myRTC(CLK, DAT, RST);                 // Instantiate the RTC Object
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);   // Instantiate the LCD I2C Object

// ========================================================================================================
// --- Function Prototypes ---
void DS1302(); // Function prototype for main function
void week(int dayW); // Function prototype for weekday printing

// ========================================================================================================
// --- Initial Configurations ---
void setup()  
{   
  // Initialize the LCD
  lcd.init();
  lcd.backlight();

  // Setup RTC Time (Initial Load). Uncomment this line and load code once to set initial time,
  // then comment it out again and reload to avoid resetting time each time the code runs.
  //myRTC.setDS1302Time(Sec, Min, Hr, D_Week, D_Month, Month, Year);
  
} // End of Setup


// ========================================================================================================
// --- Infinite Loop ---
void loop()  
{
   DS1302(); // Call DS1302 function in loop
} // End of loop


// ========================================================================================================
// --- Function Development ---
void DS1302()
{
  myRTC.updateTime();         // Update RTC time

  // Print time information on the LCD
  week(myRTC.dayofweek); // Print weekday
  lcd.print(", ");
  if(myRTC.dayofmonth < 10) lcd.print("0");
  lcd.print(myRTC.dayofmonth);
  lcd.print("/");
  if(myRTC.month < 10) lcd.print("0");
  lcd.print(myRTC.month);
  lcd.print("/");
  lcd.print(myRTC.year);
  lcd.setCursor(0, 1);
  if(myRTC.hours < 10) lcd.print("0");
  lcd.print(myRTC.hours);
  lcd.print(":");
  if(myRTC.minutes < 10) lcd.print("0");
  lcd.print(myRTC.minutes);
  lcd.print(":");
  if(myRTC.seconds < 10) lcd.print("0");
  lcd.print(myRTC.seconds);
  delay(1000); // Wait 1 second before updating time
  lcd.print("        "); // Clear any remaining characters on LCD
} // End of function "DS1302"

 
void week(int dayW)
{ 
  switch(dayW)
  {
    case 0: lcd.print("Err"); break; // Error case (not expected)
    case 1: lcd.print("Sun"); break; // Sunday
    case 2: lcd.print("Mon"); break; // Monday
    case 3: lcd.print("Tue"); break; // Tuesday
    case 4: lcd.print("Wed"); break; // Wednesday
    case 5: lcd.print("Thu"); break; // Thursday
    case 6: lcd.print("Fri"); break; // Friday
    case 7: lcd.print("Sat"); break; // Saturday
  } // End of switch 
} // End of "Week" function

If you do not have the specified libraries, here are the library files

LiquidCrystal_I2C.zip (20,3,KB)
ArduinoRTClibrary-master.zip (15,0,KB)

What is the question?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.