MQ2: gas and smoke detector (Arduino)

The MQ2 is a sensor that allows gas or smoke detector at concentrations of 300 ppm to 10,000 ppm. After calibration, the MQ-2 can detect various gases such as LPG, i-butane, propane, methane, alcohol, hydrogen and fumes. It is designed for indoor use at room temperature.

Supplied in 5V, it has an analog output and a potentiometer sensitivity adjustment. You can also find it with a Grove interface, including the SeeedStudio manufacturer for your Intel Edisson projects. You can buy MQ2 from €2.80

Caution, do not use this detector to carry out applications that could endanger the safety of people (medical, industrial, etc.)

Warning. The sensor heats up after a few minutes of use. This is normal, it is the principle of operation of all physicochemical sensors. Take into account in designing your project.

Hardware needed to test the MQ-2 on an Arduino

For this project, you will need the following materials:


mq-2 mq2 sensor arduinoYou may run into problems installing your MQ-2 on a breadboard. Indeed, the manufacturers often quite often solder the angled connectors a little too far from the edge (to let the appearances appear) which prevents to use a breadbord. In this case, not the choice, it will be necessary to use Jumpers Dupont M/F.

Connect the VCC and GND pins of the sensor to the Arduino 5V and GND

Connect the Pin D0 of the sensor to the analog input A0 of the Arduino

Basic code

Here is the code for testing your MQ-2. By default, the analog output of the MQ-2 is connected to input A0 of the Arduino.

Calibrate an MQ2 to detect different gases

The above code simply reads the analog value returned by the detector. If you want to know more precisely the value of the emission level of the gas, we will have to carry out a calibration. The manufacturer has published two curves in the technical documentation to enable us to carry out the calibration.

mq2 mq-2 sensibilite capteur pour gpl co alcool propane fumées hydrogene

The second curve allows the calibration to be adjusted as a function of temperature and humidity.

mq2 mq-2 correction sensibilite fonction humidite temperature

To calibrate, we do not need to re-invent the wheel, we will use the code provided by Sandbox Electronics to calibrate the sensor to detect the presence of LPG, CO (carbon monoxide) or smoke in the surrounding atmosphere . Create an Arduino project and paste the code below. This program performs the calibration of resistance R0 alone. To do this, make sure that the sensor is placed in clean air when you start the program.

The other sensors of the MQ-xx family

The MQ-2 belongs to a family of sensors dedicated to the detection of gases. It is the most versatile detector, the others are specialized in one or two molecules. They can be obtained separately but they can also be found as an assembly of several sensors. You can use the same code as before to test them, it will be necessary to adjust the calibration according to the chosen sensor. For more information on the MQ sensor family, please see this article.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.


  • Nur Tika

    the coding has a erro at the void…line 46

    • Hello Nur. I checked the code but did not find the error on line 46. There may be an error while copying the code in the arduino editor.

  • Sebastian Lange

    Hi all,
    first off thanks for the awesome tutorial. One question though, in line 166 the function log is used which is base e and pow(10, …) which is base 10. Is there a reason for this? Should both not be using the same base? Switching to log10 leads to more plausible results in my case although I have not been able to verify since I do not possess any real CO measurement equipment.

    Best regards,


  • On Jian Ye

    Hi sir, may i know how to get the value LPGCurve[3] = {2.3,0.21,-0.47}; ——–2.3 and 0.21?

DIY Projects