Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Multi-Gas Detection System with Threshold Alert LEDs

Image of ESP32-Based Multi-Gas Detection System with Threshold Alert LEDs

Circuit Documentation

Summary

The circuit in question is designed to monitor various gases using a series of MQ gas sensors and a DHT11 sensor for temperature and humidity readings. The core of the circuit is an ESP32 microcontroller, which reads analog signals from the gas sensors and a digital signal from the DHT11 sensor. The ESP32 also controls several LEDs that act as indicators for high gas levels based on predefined thresholds. The circuit is powered through the ESP32's VIN pin, and all components share a common ground.

Component List

  • ESP32: A microcontroller with Wi-Fi and Bluetooth capabilities. It is used to read sensor data and control LEDs.
  • MQ-7 Breakout: A carbon monoxide (CO) gas sensor.
  • MQ-9 Breakout: A sensor for detecting carbon monoxide (CO), methane (CH4), and LPG.
  • MQ135: A sensor for various gases including ammonia (NH3), benzene (C6H6), alcohol, smoke, and carbon dioxide (CO2).
  • MQ-2: A sensor for smoke, LPG, methane (CH4), carbon monoxide (CO), alcohol, and hydrogen (H2).
  • MQ-4: A sensor for methane (CH4) and natural gas.
  • MQ6: A sensor for LPG, butane, and propane.
  • DHT11: A sensor for measuring ambient temperature and humidity.
  • LED: Two Pin (red): Indicator LEDs used to signal high gas levels.

Wiring Details

ESP32

  • GND: Connected to the ground pins of all sensors and LEDs.
  • VIN: Connected to the VCC pins of all sensors.
  • D33: Connected to the A0 pin of MQ135.
  • D25: Connected to the AO pin of MQ-9 Breakout.
  • D26: Connected to the AO pin of MQ-7 Breakout.
  • D27: Connected to the A0 pin of MQ6.
  • D14: Connected to the A0 pin of MQ-4.
  • D12: Connected to the ANALOG pin of MQ-2.
  • D13: Connected to the DATA pin of DHT11.
  • D15, D2, D4, D5, D18, D19: Connected to the anode pins of the respective LEDs.

MQ-7 Breakout

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • AO: Connected to D26 of ESP32.

MQ-9 Breakout

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • AO: Connected to D25 of ESP32.

MQ135

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • A0: Connected to D33 of ESP32.

MQ-2

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • ANALOG: Connected to D12 of ESP32.

MQ-4

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • A0: Connected to D14 of ESP32.

MQ6

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • A0: Connected to D27 of ESP32.

DHT11

  • GND: Connected to the common ground.
  • VCC: Connected to the common power supply (VIN of ESP32).
  • DATA: Connected to D13 of ESP32.

LEDs (Red)

  • Cathode: Connected to the common ground.
  • Anode: Connected to the respective pins on the ESP32 (D15, D2, D4, D5, D18, D19).

Documented Code

ESP32 Code (sketch.ino)

#include <DHT.h>  // Include the DHT sensor library

#define DHT_PIN 13  // Pin connected to the DHT11 sensor
#define DHT_TYPE DHT11  // DHT type (DHT11 in this case)

#define MQ2_PIN 12   // Pin connected to MQ2 sensor (AO)
#define MQ4_PIN 14   // Pin connected to MQ4 sensor (AO)
#define MQ6_PIN 27   // Pin connected to MQ6 sensor (AO)
#define MQ7_PIN 26   // Pin connected to MQ7 sensor (AO)
#define MQ9_PIN 25   // Pin connected to MQ9 sensor (AO)
#define MQ135_PIN 33 // Pin connected to MQ135 sensor (AO)

DHT dht(DHT_PIN, DHT_TYPE);  // Initialize the DHT11 sensor

// Updated threshold calibration based on typical gas concentrations
int mq2_threshold = 400;   // Threshold for MQ2 (Smoke, LPG, Methane, Alcohol, CO)
int mq4_threshold = 500;   // Threshold for MQ4 (Methane, Natural Gas)
int mq6_threshold = 1000;  // Threshold for MQ6 (LPG, Butane, Propane)
int mq7_threshold = 200;   // Threshold for MQ7 (Carbon Monoxide)
int mq9_threshold = 150;   // Threshold for MQ9 (CO, Methane, LPG, Carbon Monoxide)
int mq135_threshold = 400; // Threshold for MQ135 (Ammonia, Benzene, Alcohol, Smoke, CO2)

#define mq2led 15
#define mq4led 2
#define mq6led 4
#define mq7led 5
#define mq9led 18
#define mq135led 19
void setup() {
  // Start serial communication for debugging
  Serial.begin(115200);

  // Start DHT sensor
  dht.begin();

  // Set MQ pins as input (optional, default is input)
  pinMode(MQ2_PIN, INPUT);
  pinMode(MQ4_PIN, INPUT);
  pinMode(MQ6_PIN, INPUT);
  pinMode(MQ7_PIN, INPUT);
  pinMode(MQ9_PIN, INPUT);
  pinMode(MQ135_PIN, INPUT);

   pinMode(mq2led,OUTPUT);
   pinMode(mq4led,OUTPUT);
   pinMode(mq6led,OUTPUT);
   pinMode(mq7led,OUTPUT);
   pinMode(mq9led,OUTPUT);
   pinMode(mq135led,OUTPUT);
}

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

  // Check if any readings failed
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read analog values from MQ sensors (raw values)
  int mq2_value = analogRead(MQ2_PIN);
  int mq4_value = analogRead(MQ4_PIN);
  int mq6_value = analogRead(MQ6_PIN);
  int mq7_value = analogRead(MQ7_PIN);
  int mq9_value = analogRead(MQ9_PIN);
  int mq135_value = analogRead(MQ135_PIN);

  // Print the sensor data to the Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C | Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  // Print the analog readings from MQ sensors
  Serial.print("MQ2 Sensor Value: ");
  Serial.println(mq2_value);
  
  Serial.print("MQ4 Sensor Value: ");
  Serial.println(mq4_value);
  
  Serial.print("MQ6 Sensor Value: ");
  Serial.println(mq6_value);
  
  Serial.print("MQ7 Sensor Value: ");
  Serial.println(mq7_value);
  
  Serial.print("MQ9 Sensor Value: ");
  Serial.println(mq9_value);
  
  Serial.print("MQ135 Sensor Value: ");
  Serial.println(mq135_value);

  // Add logic to interpret analog values based on a threshold for safety or monitoring
  // Check if the sensor values exceed the threshold for each sensor
  if (mq2_value > mq2_threshold) {
    Serial.println("Warning: High levels of gas detected by MQ2!");
    digitalWrite(mq2led,HIGH);
  }

  if (mq4_value > mq4_threshold) {
    Serial.println("Warning: High levels of gas detected by MQ4!");
      digitalWrite(mq4led,HIGH);
  }

  if (mq6_value > mq6_threshold) {
    Serial.println("Warning: High levels of gas detected by MQ6!");
      digitalWrite(mq6led,HIGH);
  }

  if (mq7_value > mq7_threshold) {
    Serial.println("Warning: High levels of gas detected by MQ7!");
      digitalWrite(mq7led,HIGH);
  }

  if (mq9_value > mq9_threshold) {
    Serial.println("Warning: High levels of gas detected by MQ9!");
      digitalWrite(mq9led,HIGH);
  }

  if (mq135_value > mq135_threshold) {
    Serial.println("Warning: High levels of gas detected by MQ135!");