Introduction
This tutorial demonstrates how to detect smoke and gas levels using the MQ-2 gas sensor with an Arduino. The MQ-2 sensor is versatile, capable of detecting gases such as smoke, propane, methane, and alcohol. Its affordability and sensitivity make it ideal for safety systems, environmental monitoring, and IoT applications.
By the end of this guide, you will learn how to wire the MQ-2 sensor, read gas concentration values, and interpret results to implement in your own projects.
Required Components
- 1 x MQ-2 Smoke and Gas Sensor
- 1 x Arduino board (e.g., Arduino Uno, Mega, or Nano)
- Jumper wires (minimum of 3)
- 1 x Breadboard
- USB cable for Arduino
- Arduino IDE (for programming)
- Optional: Buzzer or LED for alert system
Tip: If you want a visual alert system, consider adding an LED or a buzzer that triggers at a specific smoke level.
Working Principle
The MQ-2 sensor operates using a gas-sensitive material that changes resistance when exposed to specific gases. A built-in heating element increases the sensitivity of the sensor. The Arduino reads the analog output voltage from the sensor, which corresponds to the gas concentration in the environment. By calibrating the sensor, you can detect specific gases with greater accuracy.
The sensor has two outputs:
- Analog Output (A0): Provides variable voltage based on the detected gas concentration.
- Digital Output (D0): Triggered when the gas concentration exceeds a pre-set threshold, adjustable via a potentiometer on the sensor module.
Circuit Wiring Instructions
To connect the MQ-2 sensor to the Arduino:
- VCC Pin: Connect the VCC pin on the MQ-2 sensor to the 5V pin on the Arduino.
- GND Pin: Connect the GND pin on the MQ-2 sensor to the GND pin on the Arduino.
- A0 Pin: Connect the A0 pin on the sensor to the analog pin A0 on the Arduino for analog readings.
- D0 Pin (Optional): Connect the D0 pin to a digital input pin on the Arduino if you want to use the threshold-based digital output.
Optional: Add an LED or buzzer between a digital pin and GND to serve as an alert system when smoke levels exceed a specific threshold.
Arduino Code
/*
* Smoke and Gas Detection with MQ-2 Sensor
*/
const int mq2Pin = A0; // MQ-2 sensor connected to analog pin A0
const int ledPin = 13; // Optional LED connected to digital pin 13
int smokeLevel = 0;
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
// Read analog value from MQ-2 sensor
smokeLevel = analogRead(mq2Pin);
// Print smoke level to serial monitor
Serial.print("Smoke Level: ");
Serial.println(smokeLevel);
// Trigger LED if smoke level exceeds threshold
if (smokeLevel > 300) { // Adjust threshold as needed
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}
delay(1000); // Wait for 1 second
}
Note: Adjust the threshold value in the code based on your sensor's environment and calibration.
Sensor Calibration
The MQ-2 sensor needs a warm-up period for accurate readings. After powering the sensor, wait for 20–30 seconds before taking measurements.
To calibrate the sensor:
- Expose the sensor to clean air and note the baseline reading (e.g., 50-100).
- Expose the sensor to smoke or the target gas and observe the change in readings.
- Use these readings to set thresholds for detecting specific gas concentrations.
Results
Once you upload the code and open the serial monitor, you will see the sensor's output values. These values represent the detected gas concentration. Higher values indicate higher smoke or gas levels.
If you connected an LED or buzzer, it will activate when the gas level exceeds the threshold.
Challenges and Solutions
Here are some common issues you might face and how to resolve them:
- Inconsistent readings: Ensure the sensor has had enough warm-up time and is not placed in a drafty area.
- High baseline values: Check the wiring and ensure the sensor is exposed to clean air during calibration.
- Low sensitivity: Adjust the potentiometer on the sensor module to fine-tune sensitivity.
Applications
- Fire alarms and safety systems
- Air quality monitoring devices
- IoT-based gas detection and reporting
- Automated ventilation systems
Conclusion
The MQ-2 sensor is an affordable and versatile tool for detecting smoke and gases. By following this tutorial, you can build your own gas detection systems for safety, monitoring, or automation purposes. Experiment with different setups and enhance your projects!