Site icon Hackatronic

Interfacing DHT11 + Soil Moisture Sensor with Arduino & ESP32

Let’s learn how to build an Environment Monitoring System with Arduino, ESP32, Soil Moisture Sensor, and DHT11 Sensor. Monitoring environmental conditions such as soil moisture, temperature, and humidity is essential for applications in agriculture, gardening, and climate control. We’ll cover the working principles of these sensors, how to interface them with Arduino or ESP32, and how to visualize the data using serial communication on LCD display or over IoT platforms like Blynk or ThingSpeak.

Components Required

DHT11 Temperature and Humidity Sensor

The DHT11 sensor is widely used for measuring temperature and humidity. It uses a capacitive humidity sensor and a thermistor to sense environmental conditions and outputs the data as a digital signal. Let’s see the working of DHT11 sensor:

DHT11 Humidity & Temperature Sensor Module
DHT11 Humidity & Temperature Sensor

Humidity Sensing:

Temperature Sensing:

The temperature is measured using a thermistor (a resistor whose resistance changes with temperature). The sensor converts this resistance into a digital temperature reading.

Signal Processing:

The internal microcontroller of the DHT11 processes the raw data from the sensing elements (humidity and temperature) and outputs it as a digital signal using a single-wire communication protocol.

Capacitive Soil Moisture Sensor

A capacitive soil moisture sensor is a type of sensor used to measure the water content in soil. It works on the principle of capacitance, which changes based on the dielectric constant of the medium surrounding the sensor. Here’s a detailed explanation of how it works:

Capacitive Soil Moisture Sensor
Capacitive Soil Moisture Sensor

Key Components

Working Principle

Capacitance Basics:

Water Content Measurement:

Signal Conversion:

Capacitive Soil Moisture Sensor Specification

Interfacing DHT11 and Soil Moisture Sensor with Arduino

This system will monitor soil moisture levels, temperature, and humidity, displaying real-time data on an LCD screen.

Soil Moisture Sensor Arduino
Soil Moisture Sensor with Arduino

Circuit Diagram

Here is the wiring of above circuit:

Soil Moisture Sensor:

DHT11 Sensor:

LCD with I2C Module:

Code for Arduino

Below is the code for Interfacing DHT11 and Soil Moisture Sensor with Arduino:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

// Define DHT sensor
#define DHTPIN 7 // DHT11 data pin connected to Arduino pin 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Soil moisture sensor pin
const int soilPin = A0;

void setup() {
  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Initialize DHT sensor
  dht.begin();

  // Start serial communication (optional)
  Serial.begin(9600);
}

void loop() {
  // Read temperature and humidity from DHT11
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read soil moisture level
  int soilValue = analogRead(soilPin);
  float soilMoisture = map(soilValue, 0, 1023, 0, 100); // Convert to percentage

  // Display data on LCD
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" C");

  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
  lcd.print(humidity);
  lcd.print(" % ");

  delay(2000); // Hold the display for readability

  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("Soil: ");
  lcd.print(soilMoisture);
  lcd.print(" %");

  delay(2000); // Hold the display for readability

  lcd.clear();

  // Optional: Print data to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
  Serial.print("Soil Moisture: ");
  Serial.print(soilMoisture);
  Serial.println(" %");
}

Explanation of the Code

Libraries:

Initialization: The LCD and DHT11 sensor are initialized in the setup() function.

Data Reading: The loop() function reads data from the sensors and maps the soil moisture value to a percentage.

Display: The sensor data is displayed on the LCD and optionally printed to the Serial Monitor.

Testing the System

Enhancements

Interfacing DHT11 and Soil Moisture Sensor with ESP32

This environment monitoring system will visualize the soil moisture levels, temperature, and humidity data on Blynk IoT platform.

Soil Moisture Sensor ESP32
Soil Moisture Sensor with ESP32

Circuit Diagram

Here is the wiring of above circuit:

DHT11 sensor:

Soil moisture sensor:

Arduino IDE and Blynk IOT Configuration

Environment Monitoring with Soil Moisture, Temperature & Humidity | ESP32, DHT11, Blynk IoT

Code for ESP32

#define BLYNK_TEMPLATE_ID "TMPL3Kl4MBcEl"
#define BLYNK_TEMPLATE_NAME "Environment Monitoring System"
#define BLYNK_AUTH_TOKEN "yw6KwyHTdr4uQZcCF36SOYq5Cb345nOaoHjvGIi"

#define BLYNK_PRINT Serial
#include  
#include 

#include 

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "WiFi Username";  // type your wifi name
char pass[] = "WiFi Password";  // type your wifi password

BlynkTimer timer;

#define DHTPIN 2 //Connect Out pin to D2 in ESP32
#define DHTTYPE DHT11  
DHT dht(DHTPIN, DHTTYPE);
#define SensorPin 36 //Connect Out pin to VP/GPIO36 in ESP32
const int dryValue  = 2910;   //you need to replace this value with Value_1
const int wetValue = 1465;  //you need to replace this value with Value_2
//const int SensorPin = 36;

void sendSensor()
{
  /*int soilmoisturevalue = analogRead(A0);
   soilmoisturevalue = map(soilmoisturevalue, 0, 1023, 0, 100);*/
   int value = analogRead(SensorPin);
  //value = map(value,400,1023,100,0);
   int soilMoisturePercent  = map(value, dryValue, wetValue, 0, 100);
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
    Blynk.virtualWrite(V0, soilMoisturePercent);
    Blynk.virtualWrite(V1, t);
    Blynk.virtualWrite(V2, h);
    Serial.print("Soil Moisture : ");
    Serial.print(soilMoisturePercent);
    Serial.print("   Temperature : ");
    Serial.print(t);
    Serial.print("    Humidity : ");
    Serial.println(h);
}
void setup()
{   
  
  Serial.begin(115200);
  
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(100L, sendSensor);
 
  }

void loop()
{
  Blynk.run();
  timer.run();
 }

This is the code for Environment Monitoring System using ESP32 to measure and report soil moisture, temperature, and humidity. It sends the data to the Blynk platform for remote monitoring via a mobile or web app. Here’s a detailed breakdown:

Preprocessor Directives

Library Inclusions

WiFi and Blynk Credentials

Timer for Regular Data Sending

DHT Sensor Initialization

Soil Moisture Sensor Setup

Functions

setup()

loop()

Working Mechanism

Key Notes

Applications of Soil Moisture Sensor

  1. Agriculture: Automates irrigation and improves crop yield.
  2. Gardening: Waters plants in greenhouses or home gardens as needed.
  3. Environmental Monitoring: Tracks soil health and climate impact.
  4. Construction: Ensures soil stability for building projects.
  5. Education: Used in labs and for teaching soil-water relationships.

Applications of DHT11 Sensor

  1. Home Automation: Controls temperature and humidity in smart homes.
  2. Weather Monitoring: Collects real-time weather data.
  3. Industry: Maintains optimal conditions in warehouses and factories.
  4. Agriculture: Regulates greenhouse climate for plant growth.
  5. Health: Ensures proper indoor air quality in hospitals and labs.
  6. Gadgets: Built into air purifiers and climate devices.

Environment Monitoring System Applications

  1. Agriculture: Tracks soil, water, and air for better farming.
  2. Smart Cities: Monitors air quality and optimizes energy use.
  3. Disaster Management: Predicts floods, landslides, and wildfires.
  4. Industry: Tracks emissions and ensures environmental compliance.
  5. Research: Studies climate and ecosystems.
  6. Water Monitoring: Measures pollution and ensures clean water.
  7. Forestry: Monitors ecosystems and wildlife habitats.

Conclusion

By integrating the DHT11 and soil moisture sensor with Arduino or ESP32, you can create a compact and efficient environment monitoring system. Also, you can add water TDS sensor in your project to monitor water TDS. This project serves as a steppingstone for more advanced applications such as automated irrigation systems, greenhouse monitoring, or smart gardening solutions. With IoT integration, you can remotely monitor and analyze environmental conditions, paving the way for smarter, data-driven decision-making.

Water Quality Monitoring System: TDS Sensor + ESP32/Arduino

Exit mobile version