ESP32-Based Environmental Monitoring and Water Management System
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit is designed to monitor and control various environmental parameters for an application such as a greenhouse or aquarium. It includes sensors for measuring temperature, humidity, light intensity, water level, pH, and total dissolved solids (TDS). Actuators in the circuit include relays for controlling lights, a fan, water pumps, and solenoid valves. The central control unit is an ESP32 microcontroller, which processes sensor data and manages the actuators accordingly.
Component List
- DHT22: A sensor for measuring temperature and humidity.
- PH Meter: A sensor for measuring the pH level of a solution.
- BH1750: A light intensity sensor.
- HC-SR04 Ultrasonic Sensor: A sensor for measuring water level by ultrasonic distance measurement.
- TDS Sensor: A sensor for measuring the total dissolved solids in water.
- Water Pump: A pump for moving water.
- 1-Channel Relay (5V 10A): A relay module for controlling high power devices.
- Green Light 220VAC: An indicator light for signaling system status.
- 220V Fan: A fan for air circulation.
- Plastic Solenoid Valve: A valve for controlling the flow of liquids.
- AC Supply: Provides alternating current power supply.
- HLK-PM12: A compact power module to convert AC to DC voltage.
- 6 Relays Module: A module with multiple relays for controlling various devices.
- ESP32 Wroom: A microcontroller with Wi-Fi capabilities for controlling and monitoring the system.
Wiring Details
DHT22
- VCC: Connected to 3.3V from ESP32.
- GND: Connected to ground.
- DAT: Connected to GPIO4 on ESP32.
PH Meter
- VCC: Connected to 3.3V from ESP32.
- GND: Connected to ground.
- Signal: Connected to GPIO34 on ESP32.
BH1750
- VCC: Connected to 3.3V from ESP32.
- GND: Connected to ground.
- SCL: Connected to GPIO22 (SCL) on ESP32.
- SDA: Connected to GPIO21 (SDA) on ESP32.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V from ESP32.
- TRIG: Connected to GPIO27 on ESP32.
- ECHO: Connected to GPIO12 on ESP32.
- GND: Connected to ground.
TDS Sensor
- Signal: Connected to GPIO32 on ESP32.
Water Pump
- VCC: Controlled by NO pin of a 1-Channel Relay.
- GND: Connected to ground.
1-Channel Relay (5V 10A)
- VCC: Connected to 5V from ESP32.
- GND: Connected to ground.
- Signal: Controlled by various GPIOs on ESP32.
Green Light 220VAC
- +: Controlled by NO pin of a 1-Channel Relay.
- -: Connected to AC Supply negative.
220V Fan
- L: Controlled by NO pin of a 1-Channel Relay.
- N: Connected to AC Supply negative.
Plastic Solenoid Valve
- Pin1: Controlled by NO pins of the 6 Relays Module.
- Pin2: Connected to ground.
AC Supply
- +ve: Connected to C pin of 1-Channel Relays and HLK-PM12 AC pin.
- -ve: Connected to ground and HLK-PM12 AC pin.
HLK-PM12
- +V0: Connected to VCC of 6 Relays Module.
- -V0: Connected to ground.
- AC: Connected to AC Supply.
6 Relays Module
- VCC: Connected to +V0 from HLK-PM12.
- GND: Connected to ground.
- IN1-IN6: Controlled by various GPIOs on ESP32.
- NO_1-NO_6: Control various actuators like solenoid valves and water pumps.
- COM_1-COM_6: Connected to +V0 from HLK-PM12.
Documented Code
#include "DHT.h"
#include <Wire.h>
#include <BH1750.h>
// DHT22 Sensor
#define DHTPIN 4 // Pin where the DHT22 is connected (GPIO 4)
#define DHTTYPE DHT22 // DHT22 sensor type
DHT dht(DHTPIN, DHTTYPE);
// Light Intensity Sensor
BH1750 lightMeter;
#define LIGHT_RELAY 17 // Relay pin for light bulbs
// Water Level Sensor
#define TRIGPIN 27 // Ultrasonic sensor trigger pin
#define ECHOPIN 12 // Ultrasonic sensor echo pin
#define WATER_RELAY 18 // Relay pin for water level
// pH Sensor
#define PH_PIN 34 // Analog pin for pH sensor
#define PH_RELAY 13 // Relay pin for pH control
// TDS Sensor
#define TDS_PIN 32 // Analog pin for TDS sensor
#define TDS_RELAY_1 25 // Relay pin for solenoid valve 1
#define TDS_RELAY_2 26 // Relay pin for solenoid valve 2
// Main Water Pump
#define PUMP_RELAY 14 // Relay pin for main water pump
// Temperature and Humidity Thresholds
float tempThreshold = 30.0; // Temperature threshold for fan
float humidityThreshold = 40.0; // Humidity threshold for misters
// Light Intensity Threshold
float lightThreshold = 200.0; // Light intensity threshold (lux)
// Water Level Threshold
const float WATER_LEVEL_THRESHOLD = 300.0; // Water level threshold (mm)
// pH Threshold
const float PH_THRESHOLD = 6.5; // pH threshold value
// TDS Threshold
const int thresholdTDS = 300; // TDS threshold value
// Main Pump Timing
unsigned long previousMillis = 0;
const long onInterval = 2000; // Pump ON time (ms)
const long offInterval = 3000; // Pump OFF time (ms)
bool pumpState = false; // Main pump state
void setup() {
Serial.begin(115200);
// Initialize DHT22
dht.begin();
// Initialize Light Sensor
Wire.begin();
lightMeter.begin();
// Relay Pins Setup
pinMode(LIGHT_RELAY, OUTPUT);
pinMode(WATER_RELAY, OUTPUT);
pinMode(PH_RELAY, OUTPUT);
pinMode(TDS_RELAY_1, OUTPUT);
pinMode(TDS_RELAY_2, OUTPUT);
pinMode(PUMP_RELAY, OUTPUT);
// Set relays to OFF (assume active LOW relays)
digitalWrite(LIGHT_RELAY, HIGH);
digitalWrite(WATER_RELAY, HIGH);
digitalWrite(PH_RELAY, HIGH);
digitalWrite(TDS_RELAY_1, HIGH);
digitalWrite(TDS_RELAY_2, HIGH);
digitalWrite(PUMP_RELAY, HIGH);
// Water Level Sensor Pins
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop() {
// --- DHT22 Sensor ---
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temp) && !isnan(humidity)) {
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(humidity);
// Fan control based on temperature
if (temp > tempThreshold) {
Serial.println("Fan ON");
digitalWrite(PUMP_RELAY, LOW); // Fan ON (active low)
} else {
Serial.println("Fan OFF");
digitalWrite(PUMP_RELAY, HIGH); // Fan OFF
}
// Misters control based on humidity
if (humidity < humidityThreshold) {
Serial.println("Misters ON");
digitalWrite(PUMP_RELAY, LOW); // Misters ON (active low)
} else {
Serial.println("Misters OFF");
digitalWrite(PUMP_RELAY, HIGH); // Misters OFF
}
}
// --- Light Intensity Sensor ---
float lux = lightMeter.readLightLevel();
Serial.print("Light Intensity: ");
Serial.println(lux);
if (lux < lightThreshold) {
Serial.println("LED Bulbs ON");
digitalWrite(LIGHT_RELAY, LOW); // Turn on LED bulbs
} else {
Serial.println("LED Bulbs OFF");
digitalWrite(LIGHT_RELAY, HIGH); // Turn off LED bulbs
}
// --- Water Level Sensor ---
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(30);
digitalWrite(TRIGPIN, LOW);
long duration = pulseIn(ECHOPIN, HIGH);
float distance = (duration / 2.0) * 0.343;
Serial.print("Water Level Distance: ");
Serial.println(distance);
if (distance > WATER_LEVEL_THRESHOLD) {
Serial.println("Water Level Low: Solenoid ON");
digitalWrite(WATER_RELAY, LOW