Introduction: How to Use MQ2 Gas Sensor - Arduino Tutorial

Picture of How to Use MQ2 Gas Sensor - Arduino Tutorial

The MQ series of gas sensors use a small heater inside with an electro-chemical sensor. They are sensitive for a range of gasses and are used indoors at room temperature. The output is an analog signal and can be read with an analog input of the Arduino.

The MQ-2 Gas Sensor module is useful for gas leakage detecting in home and industry. It can detect LPG, i-butane, propane, methane ,alcohol, hydrogen and smoke.

Some modules have a built-in variable resistor to adjust the sensitivity of the sensor.

Note: The sensor becomes very hot after a while, don't touch it!

In this tutorial we will use the serial monitor of Codebender (or Arduino IDE) to see how the sensor acts in variable gasses.

So, let's get started!

Step 1: What You Will Need

Picture of What You Will Need

For this tutorial you will need:

  • Arduino uno
  • Breadboard
  • MQ-2 Gas sensor module

Step 2: The Circuit

The connections are pretty easy:

  • Vcc to Arduino 5V pin
  • GNG to Arduino GND pin
  • Output to Arduino Analog A0 pin

Note: The sensor becomes very hot after a while, don't touch it!

Step 3: The Code

Here's the code, embedded using Codebender!

Try downloading the codebender plugin and clicking on the Run on Arduino button to program your Arduino with this sketch. And that's it, you've programmed your Arduino board! Press connect button to start serial communication with your Arduino board.

Now you can try this:

Take a lighter and press the button to release gas near the sensor. Observe values on serial monitor.

You can keep playing with that by clicking the "Edit" button and start making your own modifications to the code.

Step 4: Well Done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use the MQ-2 Gas sensor with Arduino.

Tip: You can use this tutorial to read values from all MQ gas sensors (MQ-2, MQ-3, MQ-4, MQ-5, MQ-6, MQ-7, etc)

I hope you liked this, let me know in the comments.
There will be more of them, so make sure to click Follow button!


mjonssonme made it! (author)2017-09-12

Pretty easy build. But, as one reads up on the sensor, there's more to it than just building and running.

Here are some basics:

And, some nice example code from the forums to play further

itnas19 (author)2017-08-29

Thanks very much, I've enjoy a lot making it.

From Madrid, Spain

TarekS30 (author)2017-05-13

Can i supply this sensor with a 9V battery?

SWETHAC2 (author)2017-02-07

How to use MQ2 sensor as alcohol detector?please give me tutorial

KutsalÖ (author)2017-01-20

hi. Can I use arduino nano. Please help me.

diy_bloke (author)KutsalÖ2017-01-22

yes can

KutsalÖ (author)diy_bloke2017-01-25


diy_bloke (author)KutsalÖ2017-01-27

my pleasure

diy_bloke (author)KutsalÖ2017-01-22

the nano just has another footprint but the pins the same

sailuchokan (author)2016-05-27

Hi there,

I am new to this gas sensors. Wants a clarity.

the output that is showing serial monitor is




and so on..

Are they ppm or concentration of the gas.

brndnlstr (author)sailuchokan2017-01-18

I replied above on another comment on how you would make sense of the numbers. They are not PPM but an analog voltage that relates to the range of detectable values. More details in my other comment.

francis.dominic.395 (author)2016-02-29

I connected my MQ2 gas sensor ( got results as follows






...How do i interpret them

Whats should be the position of resistor pot? here its pointing at "1"

The arduino is reporting analog voltage levels, represented by numbers ranging from 0 (0 VDC) to 1023 (5 VDC) in increments of 0.0049 VDC. (

If you want to get PPM, you will need to remap values from 0-1023 to the appropriate ppm. According to the datasheet for the MQ2, it is sensitive to 300-10000 PPM. A rough approximation would be the analog read value (79, 100, 67, ...) * (10000-300)/1024. Or, approximately analog read value * 9.5. So given the values you reported, the estimated PPM of combustible gases is between 330 and 950 PPM.

The Lower Explosive Limit (LEL) of propane for example is 2.1%, or 21000 PPM, so you are well below that threshold. However, the permissible exposure level is 1000 PPM, so I am almost certain that your potentiometer needs to be adjusted. How much is impossible to say.

To calibrate, you would need to enclose the sensor in some sort of chamber, and then introduce a known concentration of a gas into the chamber (dangerous if you don't know what you are doing). The safer method is to compare readings with a known working meter, which provides a ppm readout (expensive).

Bear in mind, that without calibration, these values may be off. These are cheap sensors, so quality control is questionable. You should not rely on this for actual life safety. You should also be aware that the MQ2 is sensitive to several gases, so while propane has an LEL of 2.1%, methane has an LEL of 5%.

Hey, I'm getting the same values. Is there a way to interpret them at all?

Mustafa tai (author)2016-11-16

How to set program
When sensore active blink red light (13 to GND )LED

Amvi (author)2016-11-08

Hi there, thanks for this. I got a new edison and some MQ sensors. I tried this code and it is now giving me some data ranging from 90s to 200.

I want to be able to measure various pollutants like CO2, CO, LPG, Smoke etc present indoors using these sensors and my edison board.

What are next steps? Could you please advise?

BallscrewBob (author)2016-10-26

You might want to update the article soon as codebender is closing down...

You could try the Arduino CREATE as that lets you share code too

MJames007 (author)2016-08-16

so does this code applies to MQ-138. I'm having trouble with this sensor. They pretty do look like the same with 4 pins

jerrey made it! (author)2016-08-06

it is very interesting

captainteknics (author)2016-07-21

Arduino is fun..

jogbons made it! (author)2016-07-18

Hey Everyone,

I am Using MQ-2 gas sensor with four pins and a raspberry pi. I have tried several codes and connection options. Could anyone help me with materials and python code, please? Here is my e-mail:

jogbons made it! (author)2016-07-18

Hey Everyone,

I am Using MQ-2 gas sensor with four pins and a raspberry pi. I have tried several codes and connection options. Could anyone help me with materials and python code, please? Here is my e-mail:

krishnak65 (author)2016-02-19

i want a circuit diagram for connection....

diy_bloke (author)krishnak652016-04-27

I guess that:

  • Vcc to Arduino 5V pin
  • GNG to Arduino GND pin
  • Output to Arduino Analog A0 pin

really shouldnt be so hard to understand that you can't draw a circuit diagram for yourself if you need one

_Idee_ (author)diy_bloke2016-04-27

diy_bloke gave a good explanation, as I have made a similar project you can take a look at

If you like this project then

Do vote for it !! :)

Thank you !!

fizahsarkawi93 (author)2016-03-26

can i have the schematic sketch pls ?

SujanD1 (author)2016-03-01

do we have to heat the sensor externally for some time before taking reading???

LukasS8 (author)2015-08-11

Hey could one arduino power all MQ Sensors (MQ-2,3,4,5,6,7..) or would it need to much current?

codebender_cc (author)LukasS82015-08-15

I don't know... maybe you will need an external power source.

JAK101 (author)codebender_cc2016-02-26

You can only use an external power source to power the digital IO pins. External power for analog is not recommended due to noise issues.

TheMake (author)codebender_cc2015-11-24

maybe you could us a voltage regulator or same what you said...... :)

JAK101 (author)LukasS82016-02-26

Your best bet is to buy a sensor shield and use it with your Uno or Mega (I prefer the mega because there are more IO pins). However, you should note that no matter which board you choose it can only supply so much current to each sensor. You reach a point where you run out. Adding an external power supply doesn't work either with the sensor shields. External power isn't applied to the analog pins due to noise issues. Basically, you are limited on the amounts of analog sensors you can use.

TheMake (author)LukasS82015-11-24

it could power any of these with 5v

TheMake (author)LukasS82015-11-24

it could power any of these with 5v

francis.dominic.395 (author)2016-02-26


I am using sensor from

I get random values while I place the sensor in various positions...How do I fix them.

How do I program it such that it can sensor various gases??

Pls help....

himanshu6611 (author)2016-02-03

How to get the composition of gases detected by the sensor?

This code above will only give a 10bit A2D converted value which is of no use until the % or any amount of gases is not known.

Please elaborate....i am looking for this for a while!


aleee (author)2015-09-14

Arduino: 1.6.6 Hourly Build 2015/09/09 11:42 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Sketch uses 2,646 bytes (1%) of program storage space. Maximum is 253,952 bytes.
Global variables use 200 bytes (2%) of dynamic memory, leaving 7,992 bytes for local variables. Maximum is 8,192 bytes.
avrdude: ser_open(): can't open device "\\.\COM1": The system cannot find the file specified.

avrdude: ser_drain(): read error: The handle is invalid.

Problem uploading to board. See for suggestions.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

BallscrewBob (author)aleee2016-02-01

I know this comment is a little old now but the problem is more likely to be with the IDE you are using. Just posting this to let others know that there have been and apparently still are issues with most of the newer IDE's from 1.6.6 upward. More so if you use ESP boards too. Check the Arduino forums on a regular basis to see which are the most stable. Lots of people went back to 1.6.5 but only after a major clean up of prev installs.

codebender_cc (author)aleee2015-09-22

Install drivers and plugin of Codebender, select com port and your Arduino board and then press the green button "Run on Arduino".

AsimI3 (author)2016-01-16

How to us eit with a buzzer

BallscrewBob (author)2015-10-11

What about the fourth pin ?

pfeerick (author)BallscrewBob2016-01-03

The forth pin (the unused one in this project) will be the digital/TTL pin, that will either be high (1) or low (0), and the trigger point is set using the potentiometer on the other side. Useful when you want to simply trigger when a set amount of gas is present.

TheMake (author)2015-11-24

I like the project

MohamedM30 (author)2015-09-22

thank u

ssingh176 (author)2015-06-24


samuel123abc (author)2015-06-21

Great tutorial. Thanks for sharing!

dmwatkins (author)2015-06-19

Thanks! I've been wanting to get one of these....
I had an idea that would make a great gift for my buddy's wife. He's got this talent for creating rather potent digestion byproducts, often without auditory warning . An MQ and uC embedded between vehicle seat bottom and back could be used to trigger a relay to automatically drop the power windows.

j450nn014n (author)dmwatkins2015-06-20

qtechnow has a methane (fart) module for sale using the MQ-4 sensor. I'm hoping to use it for a robot in a daycare centre that response when it gets close to the diaper change table.

billbillt (author)2015-06-19

Yes, Thanks!.... I have been looking for a tutorial about these inexpensive gas sensors...

About This Instructable




Bio: Arduino Tutorials by Team
More by codebender_cc:How to use a magnetic door switch / sensor with ArduinoHow to Measure LUX With ArduinoHow to Control an RGB LED Strip - Arduino Tutorial
Add instructable to: