Build a Low-Voltage Greenhouse Vent Controller
The Story
This is an educational climate-control demo, not a production crop controller. It keeps mains, irrigation dosing, and CO2 equipment out of scope.
Explain Like I'm 12
The ESP32 reads air temperature and humidity. If the air is too warm or wet, it tells a relay module to turn on a small fan.
Learning Support
- Recommended ageAges 13+
- Adult supervisionAdult supervision required around fans, power supplies, water, and relay wiring.
- Classroom useUse a small low-voltage fan or LED load to demonstrate hysteresis without mains wiring.
- Parent promptAsk why the fan has separate power and why the ESP32 pin cannot drive it directly.
- Screen-free activityDraw dry electronics, wet plant area, sensor, fan, relay, and shared ground.
- Next challengeAdd a soil moisture lesson only after waterproofing and sensor calibration are understood.
- Skills practiced
- DHT22 readings
- Relay control
- Hysteresis
- Water safety
- Learning outcomes
- Read DHT22 on GPIO4.
- Switch a relay input on GPIO26.
- Use hysteresis to avoid rapid cycling.
- Explain low-voltage load power and shared ground.
- Mini experiments
- Change temperature thresholds.
- Warm the sensor gently and observe fan state.
- Compare relay behavior with and without hysteresis.
Safety Standards
- Unplug USB before changing jumper wires. Recheck 3.3 V, 5 V, and GND before reconnecting power.
- Do not power motors, pumps, LED strips, or servos from the ESP32 3.3 V pin. Use a suitable external supply and common ground.
- Breadboards are for low-current prototypes. Move high-current or unattended builds to proper terminals, enclosure, strain relief, and fusing.
- Relay and mains-voltage projects require isolation, correct relay ratings, enclosed wiring, and qualified adult supervision.
What You Will Build
A low-voltage greenhouse ventilation prototype using DHT22 air readings, OLED status, and a relay-controlled fan.
Learning Objectives
- Read DHT22 on GPIO4.
- Switch a relay input on GPIO26.
- Use hysteresis to avoid rapid cycling.
- Explain low-voltage load power and shared ground.
Components List
- ESP32 DevKit boardUSB-programmable ESP32 board for the low-voltage logic side.
- DHT22 temperature and humidity sensorGreenhouse air sensor with a data pin on GPIO4.
- Relay moduleLow-voltage load switch; use only within its rated voltage and current.
- SSD1306 OLED displayOptional I2C status display on GPIO21/GPIO22.
- Low-voltage DC fan and matching supplyFan power is separate from ESP32 power; use relay within rating.
- Breadboard and jumper wiresFor dry bench testing.
Bill of Materials
| Part | Qty | Estimated Cost | Notes |
|---|---|---|---|
| ESP32 DevKit board | 1 | Varies | USB-programmable ESP32 board for the low-voltage logic side. |
| DHT22 temperature and humidity sensor | 1 | Varies | Greenhouse air sensor with a data pin on GPIO4. |
| Relay module | 1 | Varies | Low-voltage load switch; use only within its rated voltage and current. |
| SSD1306 OLED display | 1 | Varies | Optional I2C status display on GPIO21/GPIO22. |
| Low-voltage DC fan and matching supply | 1 | Varies | Fan power is separate from ESP32 power; use relay within rating. |
| Breadboard and jumper wires | 1 | Varies | For dry bench testing. |
Wiring
DHT22 DATA is GPIO4. Relay IN is GPIO26. OLED shares I2C GPIO21/GPIO22. Fan power is switched by the relay, not by ESP32 GPIO.
-
1
Unplug USB and fan supply before wiring.
-
2
Connect DHT22 VCC to 3.3 V, GND to GND, and DATA to GPIO4 with pull-up if needed.
-
3
Connect relay VCC/GND as required by the relay module and relay IN to GPIO26.
-
4
Connect OLED SDA to GPIO21 and SCL to GPIO22.
-
5
Wire only a low-voltage fan/load through the relay contacts.
-
6
Keep the electronics dry and separated from plant trays.
GPIO Mapping
| Signal | ESP32 Pin | Direction | Notes |
|---|---|---|---|
| DHT22 DATA | GPIO4 | Input | Use pull-up if using a bare sensor. |
| Relay IN | GPIO26 | Output | Assumes common active-LOW module. |
| OLED SDA | GPIO21 | I2C data | Optional status display. |
| OLED SCL | GPIO22 | I2C clock | Optional status display. |
Circuit Explanation
The ESP32 reads DHT22 data and drives the relay input. The relay switches external low-voltage fan power while sharing only the required control-side ground.
Engineering Explanation
Hysteresis uses separate on/off thresholds so the relay does not chatter near a boundary. Sensor errors turn the fan off in this educational default.
Libraries
- DHT sensor libraryInstall Adafruit DHT library and dependency.
- Adafruit SSD1306For optional OLED status.
Code
Copy into Arduino IDE. Install any libraries noted in the component guides first.
// ESP32 low-voltage greenhouse controller demo
// DHT22 air readings control a relay with hysteresis.
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
const int DHT_PIN = 4;
const int FAN_RELAY = 26;
const float TEMP_ON_C = 30.0;
const float TEMP_OFF_C = 28.0;
const float HUM_ON_PERCENT = 85.0;
const float HUM_OFF_PERCENT = 78.0;
DHT dht(DHT_PIN, DHT22);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
bool fanOn = false;
unsigned long lastReadMs = 0;
void setFan(bool on) {
fanOn = on;
digitalWrite(FAN_RELAY, fanOn ? LOW : HIGH); // common active-LOW relay module
}
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
dht.begin();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(WHITE);
pinMode(FAN_RELAY, OUTPUT);
setFan(false);
Serial.println("Greenhouse controller ready. Low-voltage loads only.");
}
void loop() {
if (millis() - lastReadMs < 2500) return;
lastReadMs = millis();
float tempC = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(tempC) || isnan(humidity)) {
setFan(false);
Serial.println("DHT22 read failed. Fan set off until readings recover.");
return;
}
if (!fanOn && (tempC >= TEMP_ON_C || humidity >= HUM_ON_PERCENT)) {
setFan(true);
} else if (fanOn && tempC <= TEMP_OFF_C && humidity <= HUM_OFF_PERCENT) {
setFan(false);
}
Serial.printf("Temp %.1f C Hum %.1f %% Fan %s\n", tempC, humidity, fanOn ? "ON" : "off");
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.printf("Temp: %.1f C\n", tempC);
oled.printf("Hum: %.1f %%\n", humidity);
oled.printf("Fan: %s\n", fanOn ? "ON" : "off");
oled.println("Hysteresis enabled");
oled.display();
}
Code Explanation
The loop reads every 2.5 seconds, validates sensor data, applies temperature/humidity hysteresis, and updates GPIO26 and the OLED.
Expected Output
Serial and OLED show temperature, humidity, and fan state. Warming the sensor above TEMP_ON_C turns the fan on; it turns off only below TEMP_OFF_C with humidity below HUM_OFF_PERCENT.
Troubleshooting
- DHT22 read failed Check data pin, pull-up, and power; wait at least two seconds between reads.
- Relay logic is backwards Confirm whether your module is active LOW or active HIGH and adjust setFan().
- Fan does not run Check separate fan supply, relay rating, and load wiring.
Common Mistakes
- Driving a fan directly from an ESP32 pin.
- Using mains loads on a breadboard.
- Putting electronics where water can drip.
- Omitting hysteresis and causing rapid relay cycling.
Testing Checklist
- Test the relay with an LED load first.
- Simulate warm air before connecting a fan.
- Confirm the fan remains off when DHT22 data fails.
Engineering Tips
- Mount sensors away from direct water spray.
- Use strain relief for fan wiring.
- Label threshold values in code.
Upgrade Ideas
- Add capacitive soil moisture monitoring.
- Add minimum fan runtime.
- Log readings to an MQTT dashboard.
Real-World Applications
- Grow-tent ventilation lesson
- Hysteresis control demo
- Low-voltage automation prototype
FAQs
Can this control a mains fan?
No. This beginner build is low-voltage only.
Why use hysteresis?
It prevents rapid on/off switching near the threshold.
Is it crop-safe?
No. It is an educational prototype, not a production grow controller.
Review, Testing, and References
Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-06-27. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.
Educational level: Intermediate. Estimated completion time: 75-90 min. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.
Project Complete!
You completed Build a Low-Voltage Greenhouse Vent Controller with matching wiring, code, tests, and limitations.
- DHT22 readings
- Relay control
- Hysteresis
