edit

# Grove - Gas Sensor(MQ2)

The Grove - Gas Sensor(MQ2) module is useful for gas leakage detection (home and industry). It is suitable for detecting H2, LPG, CH4, CO, Alcohol, Smoke or Propane. Due to its high sensitivity and fast response time, measurement can be taken as soon as possible. The sensitivity of the sensor can be adjusted by potentiometer.

Note

The sensor value only reflects the approximated trend of gas concentration in a permissible error range, it DOES NOT represent the exact gas concentration. The detection of certain components in the air usually requires a more precise and costly instrument, which cannot be done with a single gas sensor. If your project is aimed at obtaining the gas concentration at a very precise level, then we don't recommend this gas sensor.

There are 4 kinds of gas sensors, every can detect different type of gas, here we use a table to illustrate.

Sensor Gas Type Get One Now
MQ2 Combustible Gas, Smoke
MQ3 Alcohol Vapor
MQ5 LPG, Natural Gas, Town Gas
MQ9 Carbon Monoxide, Coal Gas, Liquefied Gas

Tip

We've released the Seeed Gas Sensor Selection Guide, it will help you choose the gas sensor that best suits your needs.

## Features¶

• Wide detecting scope
• Fast response and High sensitivity

Tip

## Specification¶

Item Parameter Min Typical Max Unit
VCC Working Voltage 4.9 5 5.1 V
PH Heating consumption 0.5 - 800 mW
RH Heater resistance - 33 - Ω
Rs Sensing Resistance 3 - 30

## Platforms Supported¶

Arduino Raspberry ArduPy

Caution

The platforms mentioned above as supported is/are an indication of the module's software or theoritical compatibility. We only provide software library or code examples for Arduino platform in most cases. It is not possible to provide software library / demo code for all possible MCU platforms. Hence, users have to write their own software library.

## Application Ideas¶

• Gas leakage detection.
• Toys
• IoT Applications
• Smart Detections

## Getting Started¶

The output voltage from the Gas sensor increases when the concentration of gas increases. Sensitivity can be adjusted by rotating the potentiometer. For detail information about the MQ-2 sensor, please refer the data-sheet provided in Resources section.

Warning

Please note that the best preheat time for the sensor is above 24 hours.

### Play with arduino¶

#### Hardware¶

• Step 1. Prepare the below stuffs:
Seeeduino V4.2 Base Shield Grove-Gas Sensor-MQ2
Get One Now Get One Now Get One Now
• Step 2. Connect Grove-Gas_Sensor-MQ2 to port A0 of Grove-Base Shield.
• Step 3. Plug Grove - Base Shield into Seeeduino.
• Step 4. Connect Seeeduino to PC via a USB cable.

Connect the Grove - Gas Sensor(MQ2) to A0 port as shown in the picture above.

Note

If we don't have Grove Base Shield, We also can directly connect Grove-Gas_Sensor-MQ2 to Seeeduino as below.

Seeeduino Grove-Gas_Sensor-MQ2
5V Red
GND Black
Not Conencted White
A0 Yellow

#### Software¶

there are some examples Please copy and paste code below to a new Arduino sketch, and upload them respectively, If you do not know how to upload the code, please check how to upload code.

Basic example:Gas Detection

In this example, the sensor is connected to A0 pin. The voltage read from the sensor is displayed. This value can be used as a threshold to detect any increase/decrease in gas concentration.

void setup() {
Serial.begin(9600);
}

void loop() {
float sensor_volt;
float sensorValue;

sensor_volt = sensorValue/1024*5.0;

Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");
delay(1000);
}


Measurement : Approximation

These examples demonstrate ways to know the approximate concentration of Gas. As per the data-sheet of the MQx sensors, these equations are tested for standard conditions and are not calibrated. It may vary based on change in temperature or humidity.

Note

Please keep the Gas Sensor in clean air environment.

void setup() {
Serial.begin(9600);
}

void loop() {
float sensor_volt;
float RS_air; //  Get the value of RS via in a clear air
float R0;  // Get the value of R0 via in H2
float sensorValue;

// Get a average data by testing 100 times
for(int x = 0 ; x < 100 ; x++)
{
}
sensorValue = sensorValue/100.0;

sensor_volt = sensorValue/1024*5.0;
RS_air = (5.0-sensor_volt)/sensor_volt; // omit * RL
R0 = RS_air/9.8; // The ratio of RS/R0 is 9.8 in a clear air from Graph (Found using WebPlotDigitizer)

Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");

Serial.print("R0 = ");
Serial.println(R0);
delay(1000);

}


Then, open the serial monitor of Arduino IDE. Write down the value of R0 and this will be used in the next program. Please write down the R0 after the reading stabilizes.

Warning

Replace the R0 below with value of R0 tested above.

Expose the sensor to any one of the gas listed above.

void setup() {
Serial.begin(9600);
}

void loop() {

float sensor_volt;
float RS_gas; // Get value of RS in a GAS
float ratio; // Get ratio RS_GAS/RS_air
sensor_volt=(float)sensorValue/1024*5.0;
RS_gas = (5.0-sensor_volt)/sensor_volt; // omit * RL

/*-Replace the name "R0" with the value of R0 in the demo of First Test -*/
ratio = RS_gas/R0;  // ratio = RS/R0
/*-----------------------------------------------------------------------*/

Serial.print("sensor_volt = ");
Serial.println(sensor_volt);
Serial.print("RS_ratio = ");
Serial.println(RS_gas);
Serial.print("Rs/R0 = ");
Serial.println(ratio);

Serial.print("\n\n");

delay(1000);

}


Now, we can get the concentration of gas from the figure below.

According to the graph, we can see that the minimum concentration we can test is 100ppm and the maximum is 10000ppm, in a other word, we can get a concentration of gas between 0.01% and 1%. However, we can't provide a formula because the relation between ratio and concentration is nonlinear.

### Play With Raspberry Pi (With Grove Base Hat for Raspberry Pi)¶

#### Hardware¶

• Step 1. Things used in this project:
Raspberry pi Grove Base Hat for RasPi Grove-Gas_Sensor-MQ2
Get ONE Now Get ONE Now Get ONE Now
• Step 2. Plug the Grove Base Hat into Raspberry.
• Step 3. Connect the Grove-Gas Sensor MQ2 to port A0 of the Base Hat.
• Step 4. Connect the Raspberry Pi to PC through USB cable.

Note

For step 3 you are able to connect the Grove-Gas Sensor MQ2 to any Analog Port but make sure you change the command with the corresponding port number.

#### Software¶

• Step 1. Follow Setting Software to configure the development environment.
• Step 2. Download the source file by cloning the grove.py library.
cd ~
git clone https://github.com/Seeed-Studio/grove.py

• Step 3. Excute below commands to write the code.
cd grove.py/grove
nano grove_gas_sensor_mq2.py


Then you should copy following code in this file and hit Ctrl+X to quit and save.

import math
import sys
import time

class GroveGasSensorMQ2:

def __init__(self, channel):
self.channel = channel

@property
def MQ2(self):
return value

Grove = GroveGasSensorMQ2

def main():
if len(sys.argv) < 2:
sys.exit(1)

sensor = GroveGasSensorMQ2(int(sys.argv[1]))

print('Detecting...')
while True:
print('Gas value: {0}'.format(sensor.MQ2))
time.sleep(.3)

if __name__ == '__main__':
main()

• Step 4. Excute below command to run the code
python grove_gas_sensor_mq2.py 0


Success

If everything goes well, you will be able to see the following result

pi@raspberrypi:~/grove.py/grove \$ python grove_gas_sensor_mq2.py 0
Detecting...
Gas value: 760
Gas value: 714
Gas value: 675
Gas value: 637
Gas value: 603
Gas value: 568
Gas value: 535
Gas value: 506
Gas value: 481
Gas value: 464
Gas value: 449
Gas value: 429
Gas value: 413
Gas value: 456
Gas value: 470
Gas value: 440
Gas value: 404
Gas value: 373
Gas value: 352
Gas value: 339
Gas value: 330
^CTraceback (most recent call last):
File "grove_gas_sensor_mq2.py", line 69, in <module>
main()
File "grove_gas_sensor_mq2.py", line 66, in main
time.sleep(.3)
KeyboardInterrupt


You can quit this program by simply press Ctrl+C.

Notice

You may have noticed that for the analog port, the silkscreen pin number is something like A1, A0, however in the command we use parameter 0 and 1, just the same as digital port. So please make sure you plug the module into the correct port, otherwise there may be pin conflicts.

### Play With Raspberry Pi (with GrovePi_Plus)¶

#### Hardware¶

• Step 1. Prepare the below stuffs:
Raspberry pi GrovePi_Plus Grove-Gas_Sensor-MQ2
Get One Now Get One Now Get One Now
• Step 2. Plug the GrovePi_Plus into Raspberry.
• Step 3. Connect Grove-Gas_Sensor-MQ2 to A0 port of GrovePi_Plus.
• Step 4. Connect the Raspberry to PC through USB cable.

#### Software¶

• Step 1. Follow Setting Software to configure the development environment.
• Step 2. Git clone the Github repository.
cd ~
git clone https://github.com/DexterInd/GrovePi.git

• Step 3. Execute below commands to use this sensor
cd ~/GrovePi/Software/Python
python grove_gas_sensor.py


Here is code of grove_gas_sensor.py:

#!/usr/bin/env python
#
# GrovePi Example for using the  Grove Gas Sensor
#
# The GrovePi connects the Raspberry Pi and Grove sensors.  You can learn more about GrovePi here:  http://www.dexterindustries.com/GrovePi
#
#
'''
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''

# NOTE:
# There are 5 gas sensors
# MQ2 - Combustible Gas, Smoke
# MQ3 - Alcohol Vapor
# MQ5 - LPG, Natural Gas, Town Gas
# MQ9 - Carbon Monoxide, Coal Gas, Liquefied Gas
# 02 - Oxygen
# The sensitivity can be adjusted by the onboard potentiometer
#
# https://www.seeedstudio.com/wiki/Grove_-_Gas_Sensor
# https://www.seeedstudio.com/wiki/Grove_-_Gas_Sensor(MQ5)
# https://www.seeedstudio.com/wiki/Grove_-_Gas_Sensor(O%E2%82%82)

import time
import grovepi

# Connect the Grove Gas Sensor to analog port A0
# SIG,NC,VCC,GND
gas_sensor = 0

grovepi.pinMode(gas_sensor,"INPUT")

while True:
try:
# Get sensor value

# Calculate gas density - large value means more dense gas
density = (float)(sensor_value / 1024.0)

print("sensor_value =", sensor_value, " density =", density)
time.sleep(.5)

except IOError:
print ("Error")


### Play With Wio Terminal (ArduPy)¶

#### Hardware¶

• Step 1. Prepare the below stuffs:
Wio Terminal Grove - Gas Sensor(MQ2)
Get One Now Get One Now
• Step 2. Connect Grove - Gas Sensor(MQ2) to the A0 Grove port of Wio Terminal.

• Step 3. Connect the Wio Terminal to PC through USB Type-C cable.

#### Software¶

• Step 1. Follow ArduPy Getting Started to configure the ArduPy development environment on Wio Terminal.

aip build
aip flash

• Step 3. Copy the following code and save it as ArduPy-mq2.py:
from machine import Pin, ADC
from machine import LCD
from machine import Sprite
import time

lcd = LCD()
spr = Sprite(lcd) # Create a buff

def main():
spr.createSprite(320, 240)
while True:
spr.setTextSize(2)
spr.fillSprite(spr.color.BLACK)
spr.setTextColor(lcd.color.ORANGE)
spr.drawFastHLine(40, 35, 240, lcd.color.DARKGREY)
spr.setTextColor(lcd.color.WHITE)
spr.drawString("- Current Level: ", 20, 50)
spr.pushSprite(0,0)
time.sleep_ms(500)

if __name__ == "__main__":
main()

• Step 4. Save the ArduPy-mq2.py in a location that you know. Run the following command and replace <YourPythonFilePath> with your ArduPy-mq2.py location.
aip shell -n -c "runfile <YourPythonFilePath>"
# Example:
# aip shell -n -c "runfile /Users/ansonhe/Desktop/ArduPy-mq2.py"

• Step 5. We will see the gas value display on terminal as below, and displaying on the Wio Terminal LCD screen.
ansonhe@Ansons-Macbook-Pro ~:aip shell -n -c "runfile /Users/ansonhe/Desktop/ArduPy-mq2.py"
Positional argument (/dev/cu.usbmodem1414301) takes precedence over --open.
Connected to ardupy
MQ2 Gas Sensor Reading is:  60
MQ2 Gas Sensor Reading is:  61
MQ2 Gas Sensor Reading is:  62
MQ2 Gas Sensor Reading is:  62
MQ2 Gas Sensor Reading is:  64
MQ2 Gas Sensor Reading is:  63
MQ2 Gas Sensor Reading is:  66
MQ2 Gas Sensor Reading is:  64
MQ2 Gas Sensor Reading is:  65
MQ2 Gas Sensor Reading is:  65
MQ2 Gas Sensor Reading is:  65
MQ2 Gas Sensor Reading is:  64


## Projects¶

Arduino And MQ2 Gas Sensor: The Grove Gas Sensor (MQ2) module is useful for gas leakage detection (home and industry). It is suitable for detecting H2, LPG, CH4 and CO.

Electronic nose to detect fruit ripening

ED-E: Home Automation and Monitoring System

Scalable Intelligent Air Quality Monitoring and Response Air Quality Monitoring that uses the Intel Edison Compute Module, Amazon AWS, Visualization through Kibana, and drones!

Octopod: Smart IoT Home Automation Project Octopod, a uniquely shaped full home automation system that allows you to monitor your home and keep security with AI and smart RFID locks.

## Tech Support¶

Please submit any technical issue into our forum.