#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Set the LCD address to 0x27 (or 0x3F depending on your module) for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
const int relayPin1 = 2;
const int relayPin2 = 3;
const int relayPin3 = 4;
const int relayPin4 = 5;
const int photoResistorPin = A0;
const int oneWireBus = 6; // Data wire is plugged into pin 6 on the Arduino
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
void setup() {
// Initialize the LCD with 20 columns and 4 rows
lcd.begin(20, 4);
// Turn on the backlight
lcd.backlight();
// Initialize the RTC
if (!rtc.begin()) {
lcd.setCursor(0, 0);
lcd.print("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
lcd.setCursor(0, 1);
lcd.print("RTC is NOT running");
// Uncomment the next line to set the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize relay pins as outputs
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
// Set all relays to off (HIGH is off for active low relay)
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
// Start the DS18B20 sensor
sensors.begin();
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
// Read the photoresistor value
int lightLevel = analogRead(photoResistorPin);
lcd.setCursor(0, 2);
lcd.print("Light: ");
lcd.print(lightLevel);
// Read temperature from DS18B20 sensor
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
lcd.setCursor(0, 3);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print(" C");
// Example logic to control relays based on light level
// Turn on relays if light level is below a threshold
int threshold = 500; // Adjust this value as needed
if (lightLevel < threshold) {
digitalWrite(relayPin1, LOW); // Turn on relay 1
} else {
digitalWrite(relayPin1, HIGH); // Turn off relay 1
}
if (lightLevel < threshold) {
digitalWrite(relayPin2, LOW); // Turn on relay 2
} else {
digitalWrite(relayPin2, HIGH); // Turn off relay 2
}
if (lightLevel < threshold) {
digitalWrite(relayPin3, LOW); // Turn on relay 3
} else {
digitalWrite(relayPin3, HIGH); // Turn off relay 3
}
if (lightLevel < threshold) {
digitalWrite(relayPin4, LOW); // Turn on relay 4
} else {
digitalWrite(relayPin4, HIGH); // Turn off relay 4
}
delay(1000);
}