This project demonstrates how to use an Arduino to display the current time on an LCD screen. We’ll use the DS3231 RTC (Real-Time Clock) module, which keeps accurate time even when the Arduino is powered off. The time is displayed on a 16×2 I2C LCD display, which is easy to interface with the Arduino.
Components Required:
- Arduino board (e.g., Arduino Uno)
- LCD display (e.g., 16×2 LCD with I2C module)
- RTC module (e.g., DS3231)
- Jumper wires
- Breadboard (Optional)
Here’s a step-by-step guide to achieve this, along with the Arduino code:
Wiring the Components:
- RTC Module (DS3231):
- VCC to 5V
- GND to GND
- SDA to A4 (on Uno)
- SCL to A5 (on Uno)
- LCD (16×2 with I2C):
- VCC to 5V
- GND to GND
- SDA to A4 (on Uno)
- SCL to A5 (on Uno)
Arduino Code:
#include <Wire.h> // Include Wire library for I2C
#include <LiquidCrystal_I2C.h> // Include LCD library for I2C
#include <RTClib.h> // Include RTC library
RTC_DS3231 rtc; // Create RTC object
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD (I2C address 0x27, 16x2)
void setup() {
// Start I2C communication
Wire.begin();
// Initialize RTC
if (!rtc.begin()) {
lcd.clear();
lcd.print("Couldn't find RTC");
while (1); // Infinite loop if RTC is not connected
}
// Set RTC to the current date and time when the sketch is compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Print a welcome message on the LCD
lcd.begin(16, 2);
lcd.setBacklight(1); // Turn on backlight (optional)
lcd.clear();
lcd.print("RTC Initialized");
delay(2000);
}
void loop() {
// Get current time
DateTime now = rtc.now();
// Clear LCD and print current time
lcd.clear();
// Print time in HH:MM:SS format
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(now.hour(), DEC); // Print the hour
lcd.print(":");
lcd.print(now.minute(), DEC); // Print the minute
lcd.print(":");
lcd.print(now.second(), DEC); // Print the second
// Print date in DD/MM/YYYY format
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(now.day(), DEC); // Print the day
lcd.print("/");
lcd.print(now.month(), DEC); // Print the month
lcd.print("/");
lcd.print(now.year(), DEC); // Print the year
delay(1000); // Update every second
}
Explanation of the Code
- Libraries Used:
- Wire.h: This library is necessary for I2C communication between the Arduino and the RTC module.
- LiquidCrystal_I2C.h: This library allows you to control the LCD screen via I2C communication, making the wiring simpler.
- RTClib.h: This library allows communication with the DS3231 RTC module.
- RTC Initialization:
rtc.begin()
: Initializes the DS3231 RTC module. If the RTC is not found, the code will display an error message on the LCD and halt.
- Setting the RTC:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)))
: This sets the RTC to the date and time when the sketch was compiled. The__DATE__
and__TIME__
are preprocessor macros in Arduino that represent the current date and time when the code is compiled.
- Displaying the Time:
rtc.now()
fetches the current date and time from the RTC module. Thenow
object contains the hour, minute, second, day, month, and year.- The time is displayed in the format
HH:MM:SS
and the date is displayed asDD/MM/YYYY
.
- LCD Updates:
- The LCD display is updated every second (
delay(1000)
), so the displayed time is refreshed regularly.
- The LCD display is updated every second (
Conclusion:
This Arduino project demonstrates how to interface an RTC module with a 16×2 LCD to display the current time and date. With this setup, you can easily display real-time information on an LCD, making it useful for projects like clocks, timers, or data logging systems.