Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

ESP32 DHT22: Automate Greenhouse Fan and Pump | ShillehTek

May 18, 2026 24 views

ESP32 DHT22: Automate Greenhouse Fan and Pump | ShillehTek
Project

Build an ESP32 smart greenhouse using a DHT22, soil moisture sensor, and 2-channel relay to automatically run a fan and pump with live readings via ShillehTek.

2 hr Intermediate6 parts

Project Overview

ESP32 smart greenhouse with DHT22 + soil moisture sensor: In this build, an ESP32 reads air temperature and humidity from a DHT22 and soil moisture from an analog probe, then switches a fan and water pump using a 2-channel relay based on simple thresholds.

You will also output live readings over Serial, and you can optionally stream the data to a phone dashboard (Blynk) or MQTT for remote monitoring.

  • Time: ~2 hours
  • Skill level: Intermediate
  • What you will build: An ESP32 monitoring air + soil, auto-controlling fan + pump, and streaming readings to Serial (with optional Blynk/MQTT dashboard).
ESP32 smart greenhouse project cover showing sensors and relay-controlled outputs
Smart greenhouse: sense, decide, act, broadcast.

Parts List

From ShillehTek

External

  • 5V mini water pump + tubing
  • 12V cooling fan
  • 12V power supply for fan + pump

Note: Always common-ground the microcontroller (ESP32/Arduino), relay module, and the relay-switched external supply.

Step-by-Step Guide

Step 1 - Wire the Sensors

Goal: Connect the DHT22 and the soil moisture module to the ESP32 so you can read air and soil conditions.

What to do: Wire the DHT22 data line to GPIO 4 (D4). Wire the soil moisture analog output to the ESP32 analog input on GPIO 36. Provide correct power and ground to both sensors.

ESP32 wired on a breadboard to a DHT22 on GPIO 4 and a soil moisture sensor analog output on GPIO 36
DHT22 on D4, soil moisture on A0 (GPIO 36 on ESP32).

Expected result: Your ESP32 has both sensors physically connected and ready for code.

Step 2 - Add Relays for the Pump and Fan

Goal: Add two relay channels so the ESP32 can switch a water pump and a cooling fan.

What to do: Connect relay IN1 to GPIO 26 for the pump and relay IN2 to GPIO 27 for the fan. Wire your pump and fan through the relay contacts appropriate for your power setup, and ensure all grounds are common between the ESP32, relay module, and the external supply.

Wiring diagram showing ESP32 connected to a 2-channel relay with IN1 on GPIO 26 for pump and IN2 on GPIO 27 for fan
2-channel relay: IN1 to GPIO 26 (pump), IN2 to GPIO 27 (fan).

Expected result: The ESP32 can control both relay inputs, and the pump and fan are ready to be switched by the relays.

Step 3 - Upload the Sketch

Goal: Flash the ESP32 with code that reads the DHT22 and soil sensor, then switches the pump and fan based on thresholds.

What to do: Update the Wi-Fi credentials in the sketch, compile, and upload to your ESP32. Open Serial Monitor at 115200 baud to view temperature, humidity, and soil readings.

Code:

#include <WiFi.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_PIN 36
#define PUMP 26
#define FAN 27
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  pinMode(PUMP, OUTPUT); pinMode(FAN, OUTPUT);
  WiFi.begin("YOUR_SSID","YOUR_PASS");
}
void loop() {
  float t = dht.readTemperature(), h = dht.readHumidity();
  int soil = analogRead(SOIL_PIN);
  // Lower soil reading = drier. Threshold ~2500/4095 for "dry".
  digitalWrite(PUMP, (soil > 2500) ? HIGH : LOW);
  digitalWrite(FAN,  (t > 30.0)   ? HIGH : LOW);
  Serial.printf("T=%.1fC H=%.1f%% soil=%d\n", t, h, soil);
  delay(5000);
}

Expected result: Every 5 seconds you see T/H/soil values in Serial, and the relays switch when the soil reading and temperature cross the set thresholds.

Step 4 - See It Run

Goal: Verify the automation logic with real sensor changes.

What to do: Test the soil sensor by changing moisture conditions and confirm the pump relay reacts. Warm the temperature sensor area (carefully) and confirm the fan relay triggers around the 30 C threshold used in the code.

ESP32 smart greenhouse running with relay module active as soil dries and fan control based on temperature
Relays click as soil dries; fan kicks in past 30 C.

Expected result: You can hear or see relay activity when conditions change, and Serial output confirms the readings and decisions.

Step 5 - Where to Take It Next

Goal: Extend the project with dashboards, logging, and extra sensors.

What to do: Consider these optional upgrades:

  • Push readings to Blynk for a phone dashboard
  • Add a BH1750 light sensor for grow-light scheduling
  • Log to ThingSpeak for long-term trend graphs
  • Add LoRa for an off-grid garden tower

Expected result: A clear plan for expanding the ESP32 greenhouse controller beyond the basic automation loop.

Conclusion

You built an ESP32 smart greenhouse controller that reads a DHT22 and soil moisture sensor, then switches a fan and pump through a 2-channel relay based on simple thresholds. This setup scales from a small plant enclosure to larger indoor garden monitoring with dashboards and logging.

Want the exact parts used in this build? Grab them from ShillehTek.com. If you want help customizing this project or building something for your product, check out our IoT consulting services.