Introduction
The MQ2 sensor is a commonly used gas sensor that can detect a variety of gases such as propane, methane, and carbon monoxide, as well as smoke. It works by measuring the change in resistance of its sensitive material when it comes into contact with the gas or smoke. This makes it a valuable tool for monitoring air quality in various applications such as indoor air quality control, gas leakage detection, and fire prevention.
As part of a larger prototype to detect high pollution levels for Philadelphia’s playgrounds, this tutorial shows you how to build a sensor circuit and set a threshold for smoke detection that lights up a LED. In this demo, we are measuring smoke concentration.
Background Information
As part of this kit available on Amazon, the MQ2 sensor is a device that can detect different gases and smoke in the air. It is composed of a ceramic substrate with a tin dioxide sensitive layer that changes its resistance when exposed to gas or smoke. The sensor’s response time and sensitivity are improved by heating the SnO2 layer. When it detects a gas or smoke, it sends a signal to the computer that can be read on the serial monitor (if you are using Arduino’s IDE). The stronger the gas or smoke, the stronger the signal. The application of this sensor is mostly to detect gas leaks or indoor fire.
The MQ2 sensor is capable of detecting a range of gases, including:
- LPG (liquefied petroleum gas)
- Propane
- Methane
- Butane
- Alcohol
- Hydrogen
- Smoke
- Carbon monoxide (CO)
The sensitivity of the sensor to each gas may vary, and the sensor can be calibrated for specific gases to improve its accuracy.
The MQ2 sensor has several basic uses, including:
- Gas leak detection: The MQ2 sensor can detect the presence of flammable gases such as propane, butane, and methane, making it useful for gas leak detection in homes, offices, and industrial settings.
- Smoke detection: The MQ2 sensor can also detect smoke, making it useful for fire detection and prevention in homes and buildings.
- Air quality monitoring: The MQ2 sensor can detect gases such as carbon monoxide (CO) and alcohol, which are harmful to human health in high concentrations. This makes it useful for indoor air quality monitoring and control.
- Environmental monitoring: The MQ2 sensor can also be used for environmental monitoring of gas emissions from factories, vehicles, and other sources.
Wiring Diagram
List of Parts & Assembly
- Methane, Butane, LPG and Smoke Gas Sensor – MQ-2 (Link)
2. ELEGOO UNO R3 Board ATmega328P with USB Cable(Arduino-Compatible) for Arduino and USB cable (Link)
3. BreadBoard – Half Size or Full Size (Link)
4. LED – Basic Red 5mm (Link)
5. 220 Ohm Resistor (Link)
6. Jumper Wires Pack – M/M (Link)
Refer to this table for correct pins when using the code from this tutorial.
- Using a bread board, put the red LED onto it, and connect the positive pin to the digital pin 6 on the Arduino Uno board, and the negative pin to ground. The negative pin should be connected to a resistor as shown in the diagram and picture.
- The MQ2’s Analog output should be connected to A0 on the Arduino Uno board using a Male to Female jumper wire. The VCC should be connected to 5V, and ‘Gnd’ to ground on the Arduino Uno board.
Arduino Code
The Arduino code for this tutorial was built on code from “How Does MQ-2 Flammable Gas and Smoke Sensor Work with Arduino?” and “How MQ2 Gas/Smoke Sensor Works? & Interface it with Arduino“.
The threshold of ‘100’ was used for demonstration purposes and can be based on further research. The detection range for the sensor depends on the gas, and for smoke it depends on various factors. Generally, over 400 is considered dangerous, but this value can be calibrated based on the environment.
Note: These units are in ‘ppm’ or parts per million. In the context of the MQ2 sensor, ppm refers to the number of gas molecules per million air molecules. For example, if the MQ2 sensor detects 100 ppm of propane, it means that for every one million air molecules, there are 100 propane molecules.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* Project: Working with MQ-2 Sensor Description: Using a MQ-2 Sensor (a smoke and gas sensor) to detect smoke levels in the area, and alert with an LED once the smoke level is crossed. Name: Simran Arora Date: 4.6.2023 This sketch is built on code from https://circuitdigest.com/microcontroller-projects/interfacing-mq2-gas-sensor-with-arduino and https://lastminuteengineers.com/mq2-gas-senser-arduino-tutorial/. The sensor (and similar ones) can be bought from this link: https://www.amazon.com/BONATECH-Arduino-Modules-Project-Detection/dp/B07D9H74LT/ref=sr_1_15?crid=30Y1PVDIGF0U2&keywords=arduino+air+quality+sensor&qid=1680130171&sprefix=arduino+air%2Caps%2C104&sr=8-15 */ #define ledPin 6 // Pin 6 for LED output #define sensorPin A0 // Pin A0 for analog input #define MQ2pin 0 // Defining MQ2pin as the 'sensed' value void setup() { Serial.begin(9600); // Setting 9600 baud pinMode(ledPin, OUTPUT); // Output red for LED pin digitalWrite(ledPin, LOW); // Off position for LED when it starts } void loop() { Serial.print("Analog output: "); // Printing the output Serial.println(readSensor()); // 'Sensed' reading int sensorValue = analogRead(MQ2pin); // Setting threshold for 'smoke detected' as 100. if (sensorValue > 100) // This threshold was based on some experiments done at home, but can be changed based on more research. The value of 100 is purely for demontration purposes. Serial.println(" | Smoke Detected"); // Warning text delay(500); // Sensing smoke at every 1/2 second } // Functions // This function returns the analog data to calling function int readSensor() { int sensorValue = analogRead(MQ2pin); // Setting variable for 'sensed' smoke value if (sensorValue > 100) analogWrite(ledPin, sensorValue); // generate PWM signal else digitalWrite(ledPin, LOW); return sensorValue; // Return analog moisture value } |
Serial Monitor Readings
The following image shows readings in a normal state, when there isn’t any smoke. The LED is switched off at this point.
The following image shows readings when smoke is around the sensor. The ‘smoke detected’ test is a warning added for when the smoke crosses the 100 level.
Demo Video
In the demo, using the code above, I let the sensor warm up for about 10 minutes. I had used the sensor previously, so the warm up time was lower. When using for the first time, let it warm up for 30 min. Once the readings started appearing on the serial monitor, I waited for them to gradually decrease (since this is how the sensor works: it starts with high readings). I used a candle and burnt some paper in the candle’s jar to create smoke and let the sensor hover around it. At this point, the LED turns on, and the serial monitor shows ‘Smoke Detected’ with the smoke levels >100. I move the sensor back to the original position and out of the jar’s way; the LED then switches off.
References
Datasheet: Microsoft Word – MQ-2 new.doc (pololu.com)
How Does MQ-2 Flammable Gas and Smoke Sensor Work with Arduino?
How MQ2 Gas/Smoke Sensor Works? & Interface it with Arduino
Guide for MQ-2 Gas/Smoke Sensor with Arduino