Skip to main content

Grove - Gas Sensor(MQ2)

pir

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.

Sensor

Gas Type

Get One Now

MQ2Combustible Gas, Smoke
MQ3Alcohol Vapor
MQ5LPG, Natural Gas, Town Gas
MQ9Carbon Monoxide, Coal Gas, Liquefied Gas
tip
We've released the [Seeed Gas Sensor Selection Guide](https://wiki.seeedstudio.com/Sensor_gas/), it will help you choose the gas sensor that best suits your needs.

Features

  • Wide detecting scope
  • Stable and long lifetime
  • Fast response and High sensitivity
tip
More details about Grove modules please refer to [Grove System](https://wiki.seeedstudio.com/Grove_System/)

Specification

ItemParameterMinTypicalMaxUnit
VCCWorking Voltage4.955.1V
PHHeating consumption0.5-800mW
RLLoad resistanceadjustable
RHHeater resistance-33-Ω
RsSensing Resistance3-30

Platforms Supported

ArduinoRaspberryArduPy

pir

pir

pir

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.

danger
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.2Base ShieldGrove-Gas Sensor-MQ2

pir

pir

pir

Get One NowGet One NowGet 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.

pir

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.
SeeeduinoGrove-Gas_Sensor-MQ2
5VRed
GNDBlack
Not ConenctedWhite
A0Yellow

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;

sensorValue = analogRead(A0);
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 + analogRead(A0);
}
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.

danger
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
int sensorValue = analogRead(A0);
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.

pir

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 piGrove Base Hat for RasPiGrove-Gas_Sensor-MQ2

pir

pir

pir

Get ONE NowGet ONE NowGet 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.

pir

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
from grove.adc import ADC

class GroveGasSensorMQ2:

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

@property
def MQ2(self):
value = self.adc.read(self.channel)
return value

Grove = GroveGasSensorMQ2


def main():
if len(sys.argv) < 2:
print('Usage: {} adc_channel'.format(sys.argv[0]))
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
tip
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 piGrovePi_PlusGrove-Gas_Sensor-MQ2

pir

pir

pir

Get One NowGet One NowGet 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.

    pir

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
#
# Have a question about this example? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
#
'''
## License
The MIT License (MIT)
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2017 Dexter Industries
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
sensor_value = grovepi.analogRead(gas_sensor)

# 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:
Raspberry piGrovePi_PlusGrove-Gas_Sensor-MQ2

pir

pir

pir

Get One NowGet One NowGet 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.

    pir

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
#
# Have a question about this example? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
#
'''
## License
The MIT License (MIT)
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2017 Dexter Industries
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
sensor_value = grovepi.analogRead(gas_sensor)

# 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 TerminalGrove - Gas Sensor(MQ2)

pir

pir

Get One NowGet 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.

pir

Software

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

  • Step 2. Make sure that the ArduPy firmware is flashed into Wio Terminal. For more information, please follow here.

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

mq2 = ADC(Pin(13))
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.drawString("MQ2 Reading", 90, 10)
spr.drawFastHLine(40, 35, 240, lcd.color.DARKGREY)
spr.setTextColor(lcd.color.WHITE)
spr.drawString("- Current Level: ", 20, 50)
spr.drawNumber(mq2.read(), 220,50)
spr.pushSprite(0,0)
time.sleep_ms(500)

print("MQ2 Gas Sensor Reading is: ", mq2.read())

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

pir

Schematic Online Viewer

Resources

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 & Product Discussion

Upgradable to Industrial Sensors

With the SenseCAP S2110 controller and S2100 data logger, you can easily turn the Grove into a LoRaWAN® sensor. Seeed not only helps you with prototyping but also offers you the possibility to expand your project with the SenseCAP series of robust industrial sensors.

The IP66 housing, Bluetooth configuration, compatibility with the global LoRaWAN® network, built-in 19 Ah battery, and powerful support from APP make the SenseCAP S210x the best choice for industrial applications. The series includes sensors for soil moisture, air temperature and humidity, light intensity, CO2, EC, and an 8-in-1 weather station. Try the latest SenseCAP S210x for your next successful industrial project.

Loading Comments...