/*
#DigitalClockLcd24_2
This Sketch Displays Time, Date, Day, & Temp
on a 16x4 LCD
CH Smalll III
April 02, 2025
Modified February 06, 2026
Backlight Remote On/Off
Inspired by the Information Listed Below:
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-lcd-clock
Board: Arduino Mega or Mega 2560
Com Port: COM5 (Arduino Mega or Mega 2560)
*/
// Required Libraries
#include <Wire.h>
#include <TimeLib.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Create Objects
LiquidCrystal_I2C lcd(0x27, 16, 4);
RTC_DS3231 rtc;
// Array for Days of the Week
char daysOfTheWeek[7][12] = {"Sunday ", "Monday ", "Tuesday", "Wednesday", "Thursday ", "Friday ", "Saturday"};
// Variables for Backlight On/Off Control
const int relayPin = 10;
const int buttonPin = 12;
// Parameters for Backlight On/Off Control
bool pressed = false; // Buton Pressed
bool wasPressed = false; // Button Released
bool relayToggle = false; // Toggle Relay On/Off
void setup() {
rtc.begin(); // Initialize RTC
lcd.init(); // initialize LCD
lcd.backlight(); // Activate LCD Backlight
// Pin Modes for Relay and Button
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
/*
Automatically Sets the RTC to the Date & Time on PC this Sketch was Compiled
Only needs to be Run if RTC has Lost Power or Time Reset is Required
*/
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
lcd.clear();
}
void loop() {
latch(); // Latches Relay On
DateTime now = rtc.now();
int year = now.year();
int month = now.month();
int day = now.day();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
// Display Time in 24 Hour Format
lcd.setCursor(0, 0);
lcd.print("Time: ");
printDigits(hour);
lcd.print(":");
printDigits(minute);
lcd.print(":");
printDigits(second);
// Display Date in Month/Date/Year Format
lcd.setCursor(0, 1);
lcd.print("Date: ");
printDigits(month);
lcd.print("/");
printDigits(day);
lcd.print("/");
printDigits(year);
// Display Day of Week
lcd.setCursor(0, 2);
lcd.print("Day: ");
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Display Temperature in Degrees Fahrenheit
lcd.setCursor(0, 3);
lcd.print("Temp: ");
float Temp = (rtc.getTemperature()); // Create Variable for Temperature
lcd.print(Temp * 1.8 + 32); // Converts Temp from Celsius to Fahrenheit
lcd.print(" ");
lcd.print(char(223)); // Prints the Degree Symbol
lcd.print("F");
delay(1000); // Update Display every Second
}
void printDigits(int digits) {
// Utility Function for Digital Clock Display: Prints Leading 0
if (digits < 10)
lcd.print('0');
lcd.print(digits);
}
void latch() {
wasPressed = pressed;
pressed = digitalRead(buttonPin);
// Button Pressed
if (pressed && !wasPressed) {
relayToggle = !relayToggle;
}
digitalWrite(relayPin, relayToggle);
}