Arduino Project: Auto On/Off LED with RTC and LCD Display

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:

  1. Arduino Board (e.g., Arduino Uno, Nano)
  2. LED
  3. 220-ohm Resistor (for LED)
  4. RTC Module (DS3231 or similar)
  5. I2C LCD Display (16×2 or similar)
  6. Jumper wires
  7. Breadboard (optional)

Wiring Overview:

  1. 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.
  2. 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).
  3. 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:

  1. Wire (for I2C communication, usually comes pre-installed with Arduino IDE).
  2. RTClib (for RTC communication).
  3. 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:

  1. 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.
  2. RTC Initialization:
    • The RTC is initialized with rtc.begin(). If the RTC is not found, the program enters an infinite loop.
  3. Time Display:
    • The current time is fetched from the RTC with rtc.now(). The time is displayed in the format HH:MM:SS on the LCD.
  4. 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.
  5. 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:

  1. Upload the code to your Arduino board.
  2. Verify that the LCD is showing the correct time.
  3. At 5:00 PM, the LED should automatically turn ON.
  4. At 5:10 PM, the LED should turn OFF.