#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// DHT settings
#define DHTPIN 12 // DHT22 data pin connected to GPIO 12
#define DHTTYPE DHT22 // Change to DHT11 if you're using DHT11
DHT dht(DHTPIN, DHTTYPE);
// Soil moisture sensor pin
#define SOIL_MOISTURE_PIN 13
// Ultrasonic sensor pins
#define TRIG_PIN 4 // D2
#define ECHO_PIN 5 // D1
// Relay pin
#define RELAY_PIN 26
// LDR sensor pin
#define LDR_PIN 34
// I2C LCD display settings
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);
// Water level thresholds
#define WATER_LOW_THRESHOLD 25 // 25% water level
#define WATER_HIGH_THRESHOLD 98 // 98% water level
// Calibration values for LDR sensor
const float LDR_CALIBRATION_FACTOR = 1.0; // Adjust this based on your calibration
const float LDR_CALIBRATION_INTERCEPT = 0.0; // Adjust this based on your calibration
void setup() {
Serial.begin(115200);
// Setup DHT sensor
dht.begin();
// Setup relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Turn off relay initially
// Setup ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize LCD display
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GreenhouseSystem");
delay(2000);
lcd.clear();
}
void loop() {
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Read soil moisture
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
float soilMoisturePercent = map(soilMoistureValue, 0, 4095, 0, 100);
// Read water level
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1;
// Calculate water level percentage based on distance
int waterLevelPercent = 0;
if (distance <= 20) {
waterLevelPercent = 100;
} else if (distance <= 60) {
waterLevelPercent = 75;
} else if (distance <= 200) {
waterLevelPercent = 50;
} else if (distance <= 300) {
waterLevelPercent = 25;
} else {
waterLevelPercent = 0;
}
// Control relay based on water level
if (waterLevelPercent < WATER_LOW_THRESHOLD) { // If water level is below 25%
digitalWrite(RELAY_PIN, HIGH); // Turn on pump
} else if (waterLevelPercent >= WATER_HIGH_THRESHOLD) { // If water level is 98% or more
digitalWrite(RELAY_PIN, LOW); // Turn off pump
}
// Read LDR sensor value
int ldrValue = analogRead(LDR_PIN);
// Convert LDR value to lux based on calibration
float lux = ldrValue * LDR_CALIBRATION_FACTOR + LDR_CALIBRATION_INTERCEPT;
// Display sensor values on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t, 1);
lcd.print("C ");
lcd.print("H:");
lcd.print(h, 1);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("S:");
lcd.print(soilMoisturePercent, 1);
lcd.print("% W:");
lcd.print(waterLevelPercent);
lcd.print("%");
// Wait 5 seconds before switching to the next display
delay(5000);
// Display light intensity (lux) on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Light Intensity:");
lcd.setCursor(0, 1);
lcd.print(lux);
lcd.print(" lux");
// Wait 5 seconds before switching to the next display
delay(3000);
// Display pump status on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pump Status:");
lcd.setCursor(0, 1);
if (digitalRead(RELAY_PIN) == HIGH) {
lcd.print("ON ");
} else {
lcd.print("OFF");
}
// Wait 5 seconds before updating the readings again
delay(3000);
}