ESP32 Smoke Detector Project With MQ-2 Sensor

by devadiy99 in Circuits > Arduino

122 Views, 1 Favorites, 0 Comments

ESP32 Smoke Detector Project With MQ-2 Sensor

P1030432.JPG

ESP32 Smoke Detector Project with MQ-2 Sensor and Arduino IDE Code

Introduction

Gas and smoke detection is crucial in our daily lives, especially in situations requiring safety, such as fire prevention in homes or industrial areas. With current technology, we can build an affordable and easy-to-use smoke and gas detection system using the ESP32 and the MQ-2 sensor, which is designed to detect smoke, LPG, and other flammable gases.

In this tutorial, you will learn how to create a smoke detector using the ESP32 and the MQ-2 sensor, along with the Arduino IDE code to help you get started with this DIY project, which you can further customize.

Supplies

Required Components

  1. ESP32
  2. MQ-2 Sensor
  3. Red LED (for danger status indication)
  4. Green LED (for safe status indication)
  5. Resistors (for LED connections)
  6. Jumper Wires
  7. Breadboard


Circuit Connections

To ensure the system functions correctly, follow these circuit connections:

  1. MQ-2 Sensor
  2. Connect the VCC pin of the MQ-2 to the 3.3V pin of the ESP32.
  3. Connect the GND pin of the MQ-2 to the GND pin of the ESP32.
  4. Connect the AOUT pin of the MQ-2 to GPIO 13 of the ESP32 (in the code, this is defined as MQ2_PIN).
  5. Red LED (LED-RED)
  6. Connect the Anode of the LED to GPIO 12 of the ESP32 (in the code, this is defined as LEDRED_PIN).
  7. Connect the Cathode of the LED to a resistor and then to the GND pin of the ESP32.
  8. Green LED (LED-GREEN)
  9. Connect the Anode of the LED to GPIO 14 of the ESP32 (in the code, this is defined as LEDGREEN_PIN).
  10. Connect the Cathode of the LED to a resistor and then to the GND pin of the ESP32.


Code Arduino IDE


Below is the code you can use to get started with your project:

/*
Link: https://www.devadiy.com
*/
// Pin connections
#define MQ2_PIN 13 // Analog input pin for MQ-2 sensor
#define LEDRED_PIN 12 // Digital output pin for red LED
#define LEDGREEN_PIN 14 // Digital output pin for green LED

// Threshold value for gas detection
#define GAS_THRESHOLD 600 // Gas threshold value for warning

void setup() {
// Start Serial Monitor
Serial.begin(115200);
analogReadResolution(10); // Set ADC resolution to 10-bit
Serial.println("MQ-2 Sensor Initialized!");

// Set I/O pin modes
pinMode(MQ2_PIN, INPUT);
pinMode(LEDRED_PIN, OUTPUT);
pinMode(LEDGREEN_PIN, OUTPUT);
digitalWrite(LEDRED_PIN, LOW);
digitalWrite(LEDGREEN_PIN, LOW);
}

void loop() {
// Read sensor value from MQ-2 sensor
int gasValue = analogRead(MQ2_PIN);
Serial.print("Gas Value: ");
Serial.println(gasValue);

// Check gas value against threshold
if (gasValue > GAS_THRESHOLD) {
Serial.println("Warning! High gas or smoke levels detected!");
digitalWrite(LEDRED_PIN, HIGH); // Turn on red LED
digitalWrite(LEDGREEN_PIN, LOW); // Turn off green LED
} else {
digitalWrite(LEDRED_PIN, LOW); // Turn off red LED
digitalWrite(LEDGREEN_PIN, HIGH); // Turn on green LED
}

// Small delay
delay(500);
}

Downloads

Code Explanation


  1. In the setup() function:
  2. The ADC resolution of the ESP32 is set to 10 bits (range 0-1023).
  3. The I/O pins for the MQ-2 sensor and LEDs are configured.
  4. In the loop() function:
  5. The gas value is read from the MQ-2 sensor using analogRead().
  6. The gas value is compared with the predefined threshold (GAS_THRESHOLD), which is set to 600 in this case.
  7. If the gas value exceeds the threshold, the red LED is turned on, indicating a danger.
  8. If the value is below the threshold, the green LED is turned on, indicating a safe condition.


Testing and Adjusting the GAS_THRESHOLD


The GAS_THRESHOLD value is initially set to 600, which is a middle-ground value for detection. To adjust the sensitivity:

  1. Increase the threshold (e.g., 800) to reduce the sensitivity.
  2. Decrease the threshold (e.g., 400) to increase the sensitivity.


Tips for Usage


  1. Ensure the MQ-2 sensor has sufficient power supply.
  2. Place the sensor in areas prone to smoke or flammable gases, such as kitchens or fire-prone areas.
  3. For real-world applications, it is recommended to calibrate the sensor to ensure accurate readings.


Conclusion


The ESP32 and MQ-2 sensor project is a practical example of using IoT technology for safety applications. You can further develop this project by adding Wi-Fi alerts or mobile app notifications to improve its functionality and suitability for different scenarios.

We hope this project helps you understand how to use the ESP32 with a smoke and gas detection sensor. Enjoy creating and enhancing your DIY projects!