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
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
- ESP32
- MQ-2 Sensor
- Red LED (for danger status indication)
- Green LED (for safe status indication)
- Resistors (for LED connections)
- Jumper Wires
- Breadboard
Circuit Connections
To ensure the system functions correctly, follow these circuit connections:
- MQ-2 Sensor
- Connect the VCC pin of the MQ-2 to the 3.3V pin of the ESP32.
- Connect the GND pin of the MQ-2 to the GND pin of the ESP32.
- Connect the AOUT pin of the MQ-2 to GPIO 13 of the ESP32 (in the code, this is defined as MQ2_PIN).
- Red LED (LED-RED)
- Connect the Anode of the LED to GPIO 12 of the ESP32 (in the code, this is defined as LEDRED_PIN).
- Connect the Cathode of the LED to a resistor and then to the GND pin of the ESP32.
- Green LED (LED-GREEN)
- Connect the Anode of the LED to GPIO 14 of the ESP32 (in the code, this is defined as LEDGREEN_PIN).
- 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:
Downloads
Code Explanation
- In the setup() function:
- The ADC resolution of the ESP32 is set to 10 bits (range 0-1023).
- The I/O pins for the MQ-2 sensor and LEDs are configured.
- In the loop() function:
- The gas value is read from the MQ-2 sensor using analogRead().
- The gas value is compared with the predefined threshold (GAS_THRESHOLD), which is set to 600 in this case.
- If the gas value exceeds the threshold, the red LED is turned on, indicating a danger.
- 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:
- Increase the threshold (e.g., 800) to reduce the sensitivity.
- Decrease the threshold (e.g., 400) to increase the sensitivity.
Tips for Usage
- Ensure the MQ-2 sensor has sufficient power supply.
- Place the sensor in areas prone to smoke or flammable gases, such as kitchens or fire-prone areas.
- 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!