Arduino Projects

Arduino gas sensor MQ2, Smoke Detector, Programming & Circuit



Gas Sensor MQ2- In this Tutorial, you will learn how to use the Smoke/Gas sensor Mq2 with Arduino. MQ2 Sensor is one of the most commonly used Sensors throughout the world. This Sensor is most commonly used for the Smoke and Gas detection.

gas sensor mq2

This Gas Sensor mq2 Module features an MQ-2 sensor. The MQ-2 sensor is a versatile gas sensor capable of detecting a wide range of gases including alcohol, carbon monoxide, hydrogen, isobutene, liquefied petroleum gas, methane, propane, and smoke. This module is provided with male header interface and can be easily interfaced with Arduino/Mega using male to female type jumper wires.

In this tutorial, we will cover.

  • MQ2 Sensor Pinout
  • MQ2 sensor interfacing with Arduino
  • Mq2 sensor Programming and finally
  • Testing

Amazon Links:

Arduino Nano USB-C Type (Recommended)

12v Adaptor:

MQ135 Gas Sensor:

Smoke sensor MQ-2:

Other Tools and Components:

ESP32 WiFi + Bluetooth Module (Recommended)

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines


Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


Gas Sensor MQ2 Pinout:

gas sensor mq2

The 4 male headers are labeled with

  • A0
  • D0
  • GND, and
  • VCC

A0 is the Analog output of the sensor and should be connected with the analog pin of the Arduino Uno or Mega.

D0 is the digital output in the form of 5v or Gnd. The level can be adjusted using the variable resistor.

GND pin of this module will be connected with GND of the Arduino or mega.

VCC should be connected with 5 volts of Arduino or mega.

Gas Sensor MQ2 Applications: 

They are used in gas leakage detecting equipment in family and industry, are suitable for detecting of LPG, i-butane, propane, methane, alcohol, Hydrogen and smoke. MQ2 Gas/Smoke sensor is a very user-friendly Sensor and it can be easily interfaced with the Arduino Uno or Mega using Male to Female type Jumper wires.

Gas Sensor MQ2 Interfacing with Arduino Uno:

gas sensor mq2

As you can see I have already connected 3 jumper wires, red is connected with VCC of the sensor module, black is connected with the Ground of the sensor module and white is connected with the analog output(A0) of the sensor Module. I selected A0 for the Smoke or Gas detection as this way I get more control over the MQ2 Gas/Smoke sensor. Because this way I can make accurate decisions depending on the threshold value.

gas sensor mq2

So connect the red wire with the Arduino’s 5V.

Connect the Black wire with the Arduino’s Ground.

Finally, connect the white wire with analog pin A1 of the Arduino Uno.

So that’s all about the Interfacing of the MQ-2 sensor with the Arduino. The same connections can also be used with the Mega board. One more thing that I want to share with you guys is that any program which is written for the Arduino can also run on Mega except some special cases which I will discuss in my future tutorials.

Gas Sensor MQ2 Arduino Programming:

This is a very basic program written for the SMOKE/GAS SENSOR MODULE MQ-2. The purpose of this program is to show you how you can read this sensor. I have also used the same sensor in some advance level projects which are given below.

Gas leakage detector and Automatic Solenoid Valve shut down using Arduino and MQ-2 Sensor

Arduino Gas leakage detection and SMS alert

First of all, lets start by defining a pin for the A0 pin of the MQ2 Sensor. I Selected A1 pin of the Arduino Uno.

A1 is the pin number and smokeS is the name of the A1. You can also give it any other name, even you can name it as l, m, n or x, y, z. but it’s a good programming practice to use such a name which has a meaning. The name smokeS clearly shows that, with pin A1 of  the Arduino or mega smoke sensor is connected.

Variable names follow some rules, for example

No numbers are allowed in the beginning of the variable name

No spaces are allowed in the variable names.

And we cannot use same variable names for different pins. A variable name should be unique, in simple words as your name is unique among your brothers and sisters.

data is a variable of the type integer and this will be used for storing the value coming from the MQ2 sensor.

As you know in Arduino and Mega we have an led on pin number 13 So we will be using this led as a signal that smoke is detected.

Every Arduino Uno or Mega program has at least two functions, which are the Void setup and void loop functions. Void means that these functions are not returning any values while the empty parenthesis means that these functions are not taking any arguments as the input.

Serial.begin() function is used to activate the serial communication, 9600 is the baud rate. So this is a function which is not  returning any value  but takes one argument as the input which is the baud rate or communication speed.

pinMode is also a function and it takes two arguments as the input, the Pin number or Pin name and the status which can be input or ouput. as in our case we are reading the sensor values so that’s why we are using it as an input.

As led is an output device so that’s why set to output.

In order to turn on or turn off a specific pin of the Arduino or Mega, we use the digitalWrite function. This function takes two arguments as the input, the pin which is led, and a high or low signal. So the above instruction turns off the led.

To read the Analog Pins of the Arduino Uno or Mega we use the analogRead function. So the above instruction simply reads the Smoke/Gas sensor and stores the value in variable data.

The purpose of these two instructions is to find the exact range of values, like for example

What values we get when there is no smoke, and what set of values we get when there is smoke.

This helps us in defining the threshold value which in my case is 230.

I tested it in my room, and I find out, that when there is no smoke the values remain below 200 and when there is smoke the values increases above 200. You can practically see this by watching video given at the end of this Article.

The following two if conditions are used to check if the Smoke or Gas is detected and Turns off and Turns on the Led accordingly.

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button