#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2 // Pin connected to DHT22 data pin
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD I2C address (0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD 20x4
#define RELAY_PIN_COOLING 3
#define RELAY_PIN_HUMIDIFIER 4
#define RELAY_PIN_PH_CONTROL 5
#define PH_PIN A0 // Potentiometer connected to analog pin A0
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
pinMode(RELAY_PIN_COOLING, OUTPUT);
pinMode(RELAY_PIN_HUMIDIFIER, OUTPUT);
pinMode(RELAY_PIN_PH_CONTROL, OUTPUT);
pinMode(PH_PIN, INPUT);
// Display static text on LCD for labels
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.setCursor(0, 2);
lcd.print("pH Level: ");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
// Update LCD with temperature and humidity values
lcd.setCursor(6, 0); // Set cursor to display temperature
lcd.print(t);
lcd.print(" C ");
lcd.setCursor(10, 1); // Set cursor to display humidity
lcd.print(h);
lcd.print(" % ");
// Control Cooling
if (t > 30) {
digitalWrite(RELAY_PIN_COOLING, HIGH); // Turn on cooling fan (simulate with LED)
} else {
digitalWrite(RELAY_PIN_COOLING, LOW); // Turn off cooling fan
}
// Control Humidifier
if (h < 50) {
digitalWrite(RELAY_PIN_HUMIDIFIER, HIGH); // Turn on humidifier (simulate with LED)
} else {
digitalWrite(RELAY_PIN_HUMIDIFIER, LOW); // Turn off humidifier
}
// Control pH (Simulated with potentiometer)
int phValue = analogRead(PH_PIN);
float phLevel = map(phValue, 0, 1023, 0, 14); // Simulate pH from 0 to 14
Serial.print("pH Level: ");
Serial.println(phLevel);
// Update LCD with pH value
lcd.setCursor(10, 2); // Set cursor to display pH level
lcd.print(phLevel);
lcd.print(" ");
if (phLevel < 3.5 || phLevel > 7.5) {
digitalWrite(RELAY_PIN_PH_CONTROL, HIGH); // Adjust pH with dosing pump (simulate with LED)
} else {
digitalWrite(RELAY_PIN_PH_CONTROL, LOW); // Turn off dosing pump
}
delay(2000); // Wait for 2 seconds before refreshing
}