#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Initialize RTC and LCD
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
// Array for days of the week
char daysOfWeek[7][12] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
// Pin definition
const int triggerPin = 2; // Pin D2
// Timer variables
unsigned long triggerStartTime = 0; // Store when the pin was activated
bool isTriggered = false; // Ensure it triggers only once
bool isPinActive = false; // Track if the pin is currently active
void setup() {
Serial.begin(9600);
// Initialize LCD
lcd.init();
lcd.backlight();
// Set up trigger pin
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW); // Ensure it starts LOW
// Initialize RTC
if (!rtc.begin()) {
lcd.print("RTC NOT found!");
Serial.println("RTC module is NOT found");
while (1);
}
// Manually set the RTC with a specific date & time
rtc.adjust(DateTime(2025, 1, 28, 6, 34, 55)); // Set to 06:34:55 for testing
// LCD welcome message
lcd.setCursor(0, 0);
lcd.print("RTC Initialized!");
delay(2000);
lcd.clear();
}
void loop() {
// Get current time and date from RTC
DateTime now = rtc.now();
// Clear and display date on the first row
lcd.setCursor(0, 0);
lcd.print(" "); // Clear the first row
lcd.setCursor(0, 0);
lcd.print(now.year());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.day());
// Clear and display time and day on the second row
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second row
lcd.setCursor(0, 1);
lcd.print(daysOfWeek[now.dayOfTheWeek()]);
lcd.print(" ");
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
// Trigger pin D2 at 06:35:00 for 3 minutes
if (now.hour() == 6 && now.minute() == 35 && now.second() == 0 && !isTriggered) {
digitalWrite(triggerPin, HIGH); // Activate the pin
triggerStartTime = millis(); // Record the start time
isTriggered = true; // Ensure it only triggers once
isPinActive = true; // Mark the pin as active
Serial.println("Trigger ON: 06:35:00");
}
// Keep D2 HIGH for 1 minutes (60,000 ms)
if (isPinActive && millis() - triggerStartTime >= 60000) { // 60000 ms = 1 minutes
digitalWrite(triggerPin, LOW); // Deactivate the pin
isPinActive = false; // Reset pin active status
Serial.println("Trigger OFF: 1 minutes elapsed");
}
// Print to Serial Monitor (optional)
Serial.print("ESP32 RTC Date Time: ");
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(" (");
Serial.print(daysOfWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.println(now.second());
delay(1000); // Update every 1 second
}