A smart greenhouse ventilation system using fan control with a CO2 sensor maintains optimal air quality and temperature for maximum plant growth. Carbon dioxide concentration, temperature, and humidity all critically affect photosynthesis rates and crop yields in enclosed growing environments. This guide covers building an automated greenhouse ventilation controller with an MQ-135 or SCD30 CO2 sensor, ESP32, and relay-controlled exhaust fans.
Table of Contents
- Why CO2 and Ventilation Control Matters
- CO2 Sensor Options for Greenhouses
- Components Required
- Circuit and Wiring
- ESP32 Control Code
- CO2 Enrichment vs Ventilation Strategy
- Indian Greenhouse Applications
- Frequently Asked Questions
Why CO2 and Ventilation Control Matters
Plants consume CO2 during photosynthesis. In a sealed greenhouse, CO2 levels can drop from ambient 400 ppm to below 200 ppm within hours on sunny days, severely limiting growth. Simultaneously, without ventilation, temperatures can spike 10-15 degrees C above ambient, causing heat stress. Proper ventilation control:
- Maintains CO2 above 350 ppm for good photosynthesis
- Keeps temperature within crop optimal range (18-28 degrees C for most vegetables)
- Controls relative humidity (50-70% ideal) to prevent fungal diseases
- Reduces cooling costs by 30-50% compared to always-on fans
- Enables CO2 enrichment (up to 1200 ppm) when fans are off for 20-30% yield boost
CO2 Sensor Options for Greenhouses
| Sensor | Technology | Range | Accuracy | Price |
|---|---|---|---|---|
| MQ-135 | MOS (metal oxide) | 10-300 ppm | ±30% | Rs 80 |
| MHZ-19B | NDIR (infrared) | 0-5000 ppm | ±50 ppm | Rs 900 |
| SCD30 | NDIR | 400-10000 ppm | ±30 ppm | Rs 2500 |
For serious greenhouse use, the MHZ-19B NDIR sensor offers the best accuracy-to-cost ratio. Avoid the MQ-135 for CO2 specifically — it measures air quality broadly and is not selective for CO2.
Components Required
Products from Zbotic
- GY-BME280 Temperature and Humidity Sensor — for temp/humidity monitoring alongside CO2
- 5V/12V Relay Control Module — for fan and actuator control
- Capacitive Soil Moisture Sensor — for substrate monitoring in hydroponics bays
Full components list:
- ESP32 development board
- MHZ-19B CO2 sensor (UART interface)
- BME280 temperature and humidity sensor
- 4-channel relay module (5V)
- 2x AC exhaust fans (12-inch, 230V)
- Motorised louvre/window controller (optional, 12V actuator)
- 16×2 LCD with I2C adapter
- Buzzer for alerts
- 12V or 5V power supply
Circuit and Wiring
Key connections:
- MHZ-19B TX -> ESP32 GPIO16 (RX2), RX -> GPIO17 (TX2)
- BME280 SDA -> GPIO21, SCL -> GPIO22
- Relay IN1 -> GPIO26 (Fan 1 low speed)
- Relay IN2 -> GPIO27 (Fan 2 high speed)
- Relay IN3 -> GPIO25 (Window actuator open)
- Relay IN4 -> GPIO33 (Buzzer/alert)
- LCD SDA -> GPIO21, SCL -> GPIO22 (I2C shared with BME280)
Important: AC fan relay wiring must be done by a qualified electrician. Use 16A relays for 230V AC loads. Enclose all 230V wiring in a proper IP54-rated electrical panel.
ESP32 Control Code
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <LiquidCrystal_I2C.h>
#include <HardwareSerial.h>
Adafruit_BME280 bme;
LiquidCrystal_I2C lcd(0x27, 16, 2);
HardwareSerial co2Serial(2); // UART2
// Relay pins (active LOW)
#define FAN_LOW 26
#define FAN_HIGH 27
#define WIN_OPEN 25
#define BUZZER 33
// Thresholds
const int CO2_LOW_THRESHOLD = 600; // ppm - turn fans off
const int CO2_MED_THRESHOLD = 900; // ppm - low fan speed
const int CO2_HIGH_THRESHOLD = 1200; // ppm - high fan speed
const float TEMP_MAX = 28.0; // C - override fans on regardless of CO2
const float TEMP_IDEAL = 24.0;
int currentCO2 = 400;
float currentTemp = 25.0;
float currentHumidity = 60.0;
int readMHZ19B() {
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
co2Serial.write(cmd, 9);
delay(500);
if (co2Serial.available() >= 9) {
byte response[9];
co2Serial.readBytes(response, 9);
if (response[0] == 0xFF && response[1] == 0x86) {
int co2 = (response[2] << 8) | response[3];
return co2;
}
}
return -1;
}
void updateRelays(int co2, float temp) {
// Temperature override: if too hot, always run fans regardless of CO2
if (temp >= TEMP_MAX) {
digitalWrite(FAN_LOW, HIGH); // Low fan OFF
digitalWrite(FAN_HIGH, LOW); // High fan ON
digitalWrite(WIN_OPEN, LOW); // Open window
return;
}
// CO2-based fan control
if (co2 < CO2_LOW_THRESHOLD) {
// CO2 low: fans off (conserve heat/enable CO2 enrichment)
digitalWrite(FAN_LOW, HIGH);
digitalWrite(FAN_HIGH, HIGH);
digitalWrite(WIN_OPEN, HIGH); // Close window
} else if (co2 < CO2_MED_THRESHOLD) {
// Moderate CO2: low speed ventilation
digitalWrite(FAN_LOW, LOW);
digitalWrite(FAN_HIGH, HIGH);
digitalWrite(WIN_OPEN, LOW);
} else {
// High CO2: full ventilation
digitalWrite(FAN_LOW, LOW);
digitalWrite(FAN_HIGH, LOW);
digitalWrite(WIN_OPEN, LOW);
if (co2 > CO2_HIGH_THRESHOLD + 200) {
// Alert: very high CO2 (possible combustion issue)
digitalWrite(BUZZER, LOW);
delay(200);
digitalWrite(BUZZER, HIGH);
}
}
}
void setup() {
Serial.begin(115200);
co2Serial.begin(9600, SERIAL_8N1, 16, 17);
for (int p : {FAN_LOW, FAN_HIGH, WIN_OPEN, BUZZER}) {
pinMode(p, OUTPUT);
digitalWrite(p, HIGH); // All off initially
}
Wire.begin(21, 22);
bme.begin(0x76);
lcd.init();
lcd.backlight();
lcd.print("Greenhouse Ctrl");
delay(2000);
}
void loop() {
int co2 = readMHZ19B();
if (co2 > 0) currentCO2 = co2;
currentTemp = bme.readTemperature();
currentHumidity = bme.readHumidity();
updateRelays(currentCO2, currentTemp);
// Display
lcd.setCursor(0, 0);
lcd.print("CO2:");
lcd.print(currentCO2);
lcd.print("ppm T:");
lcd.print(currentTemp, 0);
lcd.print("C ");
lcd.setCursor(0, 1);
lcd.print("RH:");
lcd.print(currentHumidity, 0);
lcd.print("% ");
bool fan1 = !digitalRead(FAN_LOW);
bool fan2 = !digitalRead(FAN_HIGH);
lcd.print("Fan:");
lcd.print(fan2 ? "HI" : (fan1 ? "LO" : "OF"));
Serial.printf("CO2: %d ppm | T: %.1fC | RH: %.1f%%n",
currentCO2, currentTemp, currentHumidity);
delay(30000); // Check every 30 seconds
}
CO2 Enrichment vs Ventilation Strategy
The most efficient greenhouse operation uses a dual mode strategy:
- Night / cool hours: Close vents, optionally inject CO2 from cylinder to maintain 800-1000 ppm. Plants benefit from elevated CO2 even without bright light during respiration recovery.
- Peak sunlight hours: Allow CO2 to rise to 1000-1200 ppm if sunny (plants use CO2 faster). Ventilate only when CO2 exceeds 1200 ppm or temperature exceeds 28 degrees C.
- Transition periods: Pre-vent 30 minutes before peak temperature to prevent heat spikes.
Indian Greenhouse Applications
Smart ventilation is particularly valuable in India’s diverse climates:
- Himachal Pradesh / Uttarakhand: Low outside temperatures make ventilation critical for temperature balance in polyhouses growing exotic vegetables
- Punjab / Haryana: Summer temperatures above 45 degrees C require active cooling beyond passive ventilation
- Nashik / Pune: Grape nethouses benefit from humidity and CO2 control during berry swelling stage
- Kerala: Humid climate makes humidity control as important as CO2 management
Frequently Asked Questions
Is the MQ-135 suitable for greenhouse CO2 measurement?
No. The MQ-135 responds to many gases and is not selective for CO2. It cannot distinguish CO2 from ammonia, alcohol vapours, or other air quality indicators. Use an NDIR sensor like the MHZ-19B for reliable CO2 measurement in greenhouses.
What CO2 level should I target for maximum crop yield?
Most vegetables show maximum yield benefit at 800-1200 ppm CO2 (2-3x ambient). Above 1500 ppm, benefits plateau and some crops show negative effects. Leafy greens (lettuce, spinach) benefit most from elevated CO2; root crops benefit least.
Can I use a single BME280 for temperature control in a large greenhouse?
For greenhouses over 200 square metres, use at least 3 BME280 sensors (one at each end and one in the middle) as temperature gradients can exceed 5 degrees C. Average the readings or use the hottest sensor to trigger ventilation.
Do I need an electrician to connect AC fans to relays?
Yes. 230V AC wiring requires a licensed electrician in India. The relay module safely isolates the low-voltage ESP32 circuit from the 230V mains, but all mains wiring must comply with IS 732 and be inspected before operation.
Add comment