The Greenhouse Control System is designed to monitor and control various environmental parameters within a greenhouse setting. The system utilizes an Arduino UNO microcontroller to read sensor data and control actuators via relays. The sensors include a soil moisture module, a light-dependent resistor (LDR) module, and a DHT11 temperature and humidity sensor. Actuators in the system include a fan, a water pump, and a 12V white LED strip for lighting. The system's logic is programmed to activate the fan, water pump, and LED strip based on temperature, soil moisture, and ambient light levels, respectively.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Define pin connections
#define DHTPIN 2 // DHT11 data pin connected to D2
#define DHTTYPE DHT11 // Defining the DHT sensor type as DHT11
#define SOIL_MOISTURE_PIN A0 // Soil moisture sensor connected to A0
#define LDR_PIN A1 // LDR sensor connected to A1
#define RELAY_FAN_PIN 3 // Relay controlling fan connected to D3
#define RELAY_PUMP_PIN 4 // Relay controlling water pump connected to D4
#define RELAY_LED_PIN 5 // Relay controlling LED strip connected to D5
// Initialize DHT sensor with the correct pin and type
DHT dht(DHTPIN, DHTTYPE);
// Initialize LCD (I2C address, columns, rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Start DHT sensor
dht.begin();
// Initialize LCD display (16 columns, 2 rows)
lcd.begin(16, 2);
lcd.backlight();
// Set pin modes
pinMode(SOIL_MOISTURE_PIN, INPUT);
pinMode(LDR_PIN, INPUT);
pinMode(RELAY_FAN_PIN, OUTPUT);
pinMode(RELAY_PUMP_PIN, OUTPUT);
pinMode(RELAY_LED_PIN, OUTPUT);
// Ensure all relays (fan, pump, LED) are off initially
digitalWrite(RELAY_FAN_PIN, LOW);
digitalWrite(RELAY_PUMP_PIN, LOW);
digitalWrite(RELAY_LED_PIN, LOW);
// Welcome message on LCD
lcd.setCursor(0, 0);
lcd.print("Greenhouse Sys");
delay(2000);
lcd.clear();
}
void loop() {
// Read and map soil moisture value to percentage
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
int soilMoisturePercent = map(soilMoistureValue, 1023, 0, 0, 100);
// Read and map light level (LDR) to percentage
int ldrValue = analogRead(LDR_PIN);
int lightLevel = map(ldrValue, 0, 1023, 0, 100);
// Read temperature and humidity values from DHT11 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Display temperature and humidity on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C Hum: ");
lcd.print(humidity);
lcd.print("%");
// Display soil moisture and light level on LCD
lcd.setCursor(0, 1);
lcd.print("Soil: ");
lcd.print(soilMoisturePercent);
lcd.print("% Light: ");
lcd.print(lightLevel);
lcd.print("%");
// Control fan based on temperature
if (temperature > 30) {
digitalWrite(RELAY_FAN_PIN, HIGH); // Turn on fan
} else {
digitalWrite(RELAY_FAN_PIN, LOW); // Turn off fan
}
// Control water pump based on soil moisture
if (soilMoisturePercent < 30) {
digitalWrite(RELAY_PUMP_PIN, HIGH); // Turn on water pump
} else {
digitalWrite(RELAY_PUMP_PIN, LOW); // Turn off water pump
}
// Control LED strip based on light level
if (lightLevel < 40) {
digitalWrite(RELAY_LED_PIN, HIGH); // Turn on LED strip