Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Smart Irrigation System with DHT11 and Soil Moisture Sensor

View and Edit Project In Cirkit DesignerImage of ESP32-Based Smart Irrigation System with DHT11 and Soil Moisture Sensor

Smart Irrigation System Documentation

Summary

This document provides a detailed overview of a smart irrigation system designed using an ESP32 microcontroller. The system includes a DHT11 humidity and temperature sensor, a soil moisture sensor, a soil moisture module, a 1-channel relay, a water pump, a DC-DC converter, and a 12V battery. The ESP32 reads data from the sensors and controls the water pump via the relay based on the soil moisture level.

Component List

  1. ESP32 (30 pin)

    • Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities.
    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
  2. DHT11 Humidity and Temperature Sensor

    • Description: A sensor used to measure humidity and temperature.
    • Pins: VDD, DATA, NULL, GND
  3. Battery 12V

    • Description: A 12V battery used to power the system.
    • Pins: +, -
  4. Water Pump

    • Description: A pump used to irrigate the soil.
    • Pins: positive, negative
  5. Soil Moisture Sensor

    • Description: A sensor used to measure the moisture level in the soil.
    • Pins: VCC, GND, SIG
  6. Soil Moisture Module

    • Description: A module that interfaces with the soil moisture sensor.
    • Pins: VCC, GND, D0, A0
  7. 1 Channel Relay 5V

    • Description: A relay used to control the water pump.
    • Pins: VCC, GND, IN, NC, COM, NO
  8. DC-DC Converter

    • Description: A converter used to step down the voltage from the 12V battery to a suitable level for the ESP32 and other components.
    • Pins: +VCC IN, GND IN, +VCC OUT, GND OUT

Wiring Details

ESP32 (30 pin)

DHT11 Humidity and Temperature Sensor

Battery 12V

Water Pump

Soil Moisture Sensor

Soil Moisture Module

1 Channel Relay 5V

DC-DC Converter

Documented Code

#include <DHT.h>
#include <DHT_U.h>

// Pin definitions
#define DHT_PIN 4          // DHT11 data pin connected to D4 of ESP32
#define SOIL_MOISTURE_PIN 34 // Analog pin for soil moisture sensor connected to D34
#define RELAY_PIN 5         // Relay control pin connected to D5 of ESP32

// DHT sensor setup
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);

// Threshold for soil moisture (adjust based on testing)
#define MOISTURE_THRESHOLD 500

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  Serial.println("Initializing Smart Irrigation System...");

  // Initialize DHT11 sensor
  dht.begin();
  Serial.println("[INFO] DHT11 sensor initialized.");

  // Initialize relay pin
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Ensure pump is initially off
  Serial.println("[INFO] Relay initialized. Pump is OFF.");

  // Soil moisture sensor setup
  pinMode(SOIL_MOISTURE_PIN, INPUT);
  Serial.println("[INFO] Soil moisture sensor initialized.");
  Serial.println("========================================");
}

void loop() {
  Serial.println("Reading sensor data...");

  // Simulating dummy data for testing purposes:
  float temperature = 25.0;   // Dummy temperature value (in Celsius)
  float humidity = 60.0;      // Dummy humidity value (in %)
  
  // Simulating soil moisture data (random between 0-1023 for 10-bit ADC)
  int soilMoistureValue = random(0, 1024); 
  
  // Display the simulated data
  Serial.print("[DHT11] Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  Serial.print("[DHT11] Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  // Display soil moisture value
  Serial.print("[SOIL] Moisture Level: ");
  Serial.println(soilMoistureValue);

  // Control water pump based on soil moisture level
  if (soilMoistureValue < MOISTURE_THRESHOLD) {
    Serial.println("[RELAY] Soil is dry. Turning ON the pump...");
    digitalWrite(RELAY_PIN, HIGH); // Turn on relay (pump ON)
  } else {
    Serial.println("[RELAY] Soil is wet. Turning OFF the pump...");
    digitalWrite(RELAY_PIN, LOW); // Turn off relay (pump OFF)
  }

  Serial.println("----------------------------------------");
  // Wait for a few seconds before the next loop
  delay(2000);
}

This code initializes the sensors and relay, reads data from the DHT11 and soil moisture sensor, and controls the water pump based on the soil moisture level. The system prints the sensor data and pump status to the Serial Monitor for debugging and monitoring purposes.