This project demonstrates how to use a real-time clock module to trigger actions at specific times and display the current time on an LCD screen, providing a basic automation system. We will create a simple system that automatically turns on an LED at 5:00 PM and off at 5:10 PM. The system will also display the current time on an LCD screen (with I2C module) using an RTC (Real-Time Clock) module.
Components Required:
- Arduino Board (e.g., Arduino Uno, Nano)
- LED
- 220-ohm Resistor (for LED)
- RTC Module (DS3231 or similar)
- I2C LCD Display (16×2 or similar)
- Jumper wires
- Breadboard (optional)
Wiring Overview:
- LED Setup:
- Connect the positive leg of the LED to pin 13 of the Arduino.
- Connect the negative leg to a 220-ohm resistor and then to GND.
- RTC Module (DS3231) Setup:
- VCC of the RTC to 5V on Arduino.
- GND of the RTC to GND on Arduino.
- SCL (Clock) of the RTC to A5 (SCL) on Arduino (for Uno).
- SDA (Data) of the RTC to A4 (SDA) on Arduino (for Uno).
- I2C LCD Setup:
- VCC of the LCD to 5V on Arduino.
- GND of the LCD to GND on Arduino.
- SCL (Clock) of the LCD to A5 (SCL) on Arduino.
- SDA (Data) of the LCD to A4 (SDA) on Arduino.
Libraries Required:
- Wire (for I2C communication, usually comes pre-installed with Arduino IDE).
- RTClib (for RTC communication).
- LiquidCrystal_I2C (for LCD control via I2C).
You can install the necessary libraries in Arduino IDE through Sketch > Include Library > Manage Libraries.
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)
// Define LED pin
const int ledPin = 13;
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
// Turn the LED on if the time is 17:00 PM
if (now.hour() == 17 && now.minute() == 00) {
digitalWrite(ledPin, HIGH); // Turn LED ON
}
// Turn the LED off if the time is 17: 10PM
if (now.hour() == 17 && now.minute() == 10) {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
delay(1000); // Update every second
}
Code Explanation:
- Libraries:
Wire.h
: Required for I2C communication between Arduino, RTC, and LCD.RTClib.h
: Handles communication with the RTC module.LiquidCrystal_I2C.h
: Controls the LCD using I2C protocol.
- RTC Initialization:
- The RTC is initialized with
rtc.begin()
. If the RTC is not found, the program enters an infinite loop.
- The RTC is initialized with
- Time Display:
- The current time is fetched from the RTC with
rtc.now()
. The time is displayed in the formatHH:MM:SS
on the LCD.
- The current time is fetched from the RTC with
- LED Control:
- The program checks the current time. If it’s 5:00 PM, the LED is turned ON. If it’s 5:10 PM, the LED is turned OFF.
- LCD Update:
- The time on the LCD updates every second, showing the current hour, minute, and second.
Setting the RTC:
If you’re using a fresh RTC module, you may need to set the current time first. You can do this by uncommenting the following line in the setup()
function:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
This sets the time to the compile date and time of the code.
Notes:
- Ensure the RTC module is properly connected to the Arduino and is powered.
- If using a different I2C address for the LCD (e.g., 0x3F), change
LiquidCrystal_I2C lcd(0x27, 16, 2)
to the correct address. - The code assumes you are using a 24-hour format for time (5:00 PM is 17:00).
Testing:
- Upload the code to your Arduino board.
- Verify that the LCD is showing the correct time.
- At 5:00 PM, the LED should automatically turn ON.
- At 5:10 PM, the LED should turn OFF.