Nowadays, sensors play a major role in the safety and security of many different apartments, homes and offices by detecting smoke, fire, or gas and providing an alarm to the concerned person. That is why in today's article we are going to talk about one such sensor which is commonly used in smoke and gas detectors, namely the MQ-2 Combustible Gas and Smoke Sensor. Don't be tricked by the name, because this sensor can not only detect gas or smoke, but can also detect LPG, Alcohol, Propane, Hydrogen, Methane, and Carbon Monoxide. So, let's learn how we can use it to give our projects the sense of smell.
MQ-2 Gas Sensor Pinout
The MQ-2 Gas detection sensor module has four pins VCC, GND, Aout, and Dout that can be used to get the needful information out of the sensor, The pinout of the MQ-2 Gas detection sensor is given below:
VCC is the power supply pin of the Gas Detection Sensor that can be connected to 5V of the supply.
GND is the ground pin of the board and it should be connected to the ground pin of the Arduino.
DOUT is the Digital output pin of the board, output low indicates gas or smoke is not present in the atmosphere and output high indicates gas or smoke is present in the atmosphere.
AOUT is the Analog output pin of the board that will give us an analog signal which will vary between vcc and ground based on the gas level detected
MQ-2 Gas and Smoke Sensor Module – Parts
In most Arduino projects, the MQ-2 sensor is used to detect hazardous or flammable gas or smoke and hence this sensor is popular among beginners. Also these are low cost, easy to use sensors featuring a wide sensing range that can be trimmed down to adjust the sensitivity. The parts markings of the MQ-2 Gas Sensor is given below-
Like all the other basic sensor modules, this MQ-2 Gas and smoke sensor module has four pins, two of which are for VCC and Gnd and the other two can simultaneously output analog and digital data. To power the circuit we are using the 5V pin of the arduino because the operating voltage range of this module is 5V with ±0.1% tolerance. As you can see in the image above the module has two onboard LEDs. The power LED turns on when power is applied to the board and the Dout LED turns on when the trigger value set by the potentiometer is reached. This board also has a comparator OP-Amp onboard that is responsible for converting the incoming analog signal from the gas sensor to a digital signal. We also have a sensitivity adjustment Trim-pot, with that we can adjust the sensitivity of the device. Finally we have some resistor capacitors for decoupling and filtering.
How Does the MQ-2 Gas Sensor Module Work?
The MQ-2 gas sensor needs a heating element in order to properly detect combustible glasses but, a heating element close to combustible gasses could be disastrous, so the sensor is manufactured with an anti-explosion network made out of two thin layers of stainless steel mesh as you can see in the image below. The heating element is placed inside this stainless steel mesh
This mesh structure also provides resistance against dust and other suspended particulars and it only lets in the gaseous elements from the atmosphere. If we decap the sensor we can see the sensor is made out of two major elements. First one is the heating element which is made out of nichrome wire and other is the sensing element that is made out of a platinum wire with a coating of tin dioxide. Now we don't want you to cut and damage you sensor, so we have done that for you, the below image shows the mesh decapped from the actual sensor
The sensor looks like something like this when the mesh is removed. As you can see in the above image we have cut the stainless steel mesh and we have placed it on the side of the sensor. Now you can also see the sensing element that we have talked about earlier.The star-shaped pins of the sensor are formed because of the structure of the actual sensing and heating element and its connected to the six legs of the sensor. You can also see the black base of the sensor that is made with Bakelite to improve thermal conductivity.
Pre-Heat Time for MQ-2 gas sensor:
When you are working with this type of gas sensor there is a pre heat time or stabilization time that is required for this device to work properly. And if you check on the datasheet of the device you can see it says it requires a pre-heat time of 24 hours. So, Does this mean that it needs to be powered on for 24 hours constantly before usage?
The obvious answer to this question is a big fat NO. It simply means that to obtain typical performance data that is shown in the datasheet, you need to run it constantly for 24 hours. And it's been measured after 24h in their lab. So, if you want to be within specification you must observe that 24h preheating time. Given the small size of the sensor, thermal equilibrium will be almost surely reached within 30 min. And it would probably take just a few minutes to be within a few percent of the data provided by the datasheet.
The 24 hours preheat time will only matter if you want a very accurate measure of gas concentration, and you also have to have a good calibration of your sensor, as well as some means to compensate for other environmental factors like temperature, humidity, etc.
The MQ-2 Gas sensor can be used for both Detecting the gas and also for Measuring Butane and Hydrogen Gas level in PPM. Do note that detecting the gas and measuring its concentration in PPM is completely two different things. This article mainly focuses on detecting the gas level and increase in its concentration. If you want to exactly calculate the gas level in PPM the procedure is different, however we will also slightly touch on it.
How to use MQ-2 Gas Sensor to Detect LPG, Butane or Hydrogen
As you can see in the above gif we have a can containing LPG gas when the gas is sprayed the gas concentration in the surrounding increases and when we spray that the second and the third time the concentration of gas increases again. When the gas concentration increases the output voltage from the sensor also increases you can observe this in the multimeter. And when it reaches a certain threshold (which can be set by the potentiometer) the green LED on the module lights up. You can observe this in the backside image of the module that we have attached for demonstration.
How to measure LPG, Butane or Hydrogen Gas concentration in PPM using MQ-2 sensor?
This is a very precise sensor that is calibrated to measure the PPM of a particular gas present in the atmosphere, to do so you first need to understand the sensitivity characteristics of the MQ-2 Gas sensor that you can find in the datasheet and it looks like something that is shown below.
In the logarithmic graph, RS is the sensing resistance during the presence of a particular gas. While R0 is the sense resistance in clean air. This sensor is designed to detect H2, LPG, CH4, CO so this sensor resistance will change depending on the concentration of H2 or LPG present in the atmosphere.
Let's take an example of the LPG curve which is the pink one and see how we can calculate the slope of the curve, for that lets start with the X and Y coordinates that is 200 and 1.8 approximately So, the first data point from the logarithmic scale is (log200, log2) which is (2.3,0.0.255). The point for the ending curve is X1 and Y1 that is 1000 and 0.18 that becomes (log1000, log0.18) thus it becomes (4, -0.744). To get the slope of the curve, the formula is
=(Y1- Y) / (X1-X) = (-0.744 - 0.255) / (4 - 2.3) =-0.587
That's how you can calculate the slope for this sensor. To know more about this formula calculation you can refer to our previous article on PPM Measurement using PIC Microcontroller which we did a little while back.
Commonly Asked Questions about MQ-2 Combustible Gas Sensor Module
How do I choose a gas sensor?
There are many different types of gas and smoke sensors available in the market. To choose one you need to refer to its datasheet and check if that particular sensor is meeting your requirements.
What types of gas can I detect with the MQ-2 Gas sensor?
If you check out the datasheet of the device you can observe that this sensor can measure many different types of gasses like Liquefied petroleum gas (LPG), Methane (CH4), Carbon Monoxide(CO), Alcohol, Smoke (CO2), and Propane but this sensor is mostly sensitive to Hydrogen.
What is ppm for gas?
Parts-per-million, or “ppm”, is commonly used as a fractional unit of measure for concentration. As an example, a methane (molecular) concentration of 2% means that 2 out of every 100 air molecules is methane. Similarly, a methane concentration of 2 ppm means that 2 out of every 1 million air molecules is methane.
Can MQ-2 detect CO2?
A short and simple answer to this question is yes, it can detect CO2. but before making any projects with this sensor please refer to the datasheet of the module to know all outer pareamerts and conditions that are required to measure CO2.
What is the difference between MQ-2 and MQ-5?
MQ-2 can sense methane(CH4) upto 20000 ppm whereas MQ5 can sense methane(CH4) only upto 10000 ppm.
Is this gas sensor analog or digital?
Most of the gas sensors which are available in the market are mostly analog, but it can be made digital with a Op-amp IC and a handful of digital components. You can check out the schematic down below for more clarification.
Internal Schematics for MQ-2 Gas Sensor Module
The Circuit Diagram for the MQ-2 Gas and Smoke Detection Sensor Module is shown below. The schematic for this module is very simple and needs a handful of components to build. If you are in a hurry to build this circuit, the schematic shown below will come in handy-
In the schematic, we have a LM393 op-amp which is a low-power low offset voltage op-amp that can be powered from a +5V supply. But powering the circuit with 3.3V is not an option because the minimum operating voltage of this MQ-2 sensor is 5V. The main job of this op-amp is to convert the incoming analog signal to digital signal. Other than that we also have an onboard 10K potentiometer that is used to adjust the sensitivity of the MQ-2 Gas Sensor module or the triggering voltage of the module.Other than that we have two LEDs. The first one is a power LED and the other one is the trigger LED. The power LED turns on when power is applied to the board and the trigger LED turns on when a certain set threshold is reached. Finally we have two decoupling capacitors that are used to reduce noise in the board.
MQ-2 Combustible Gas Sensor with Arduino UNO – Connection Diagram
Now that we have complete understanding of how the MQ-2 gas sensor works we can connect all the required wires to Arduino uno. This section of the article will be divided into two parts, one shows analog output and another one shows the digital output. Let's begin with analog circuitry
To work with the sensor we need to power the sensor first, for that we are using the 5V and GND pin of the Arduino UNO Board and we are connecting the output pin of the sensor to A0 pin of the Arduino.
As shown in the above schematic we have connected an LED to digital PIN 6 of the Arduino and the analog pin is connected to the A0 pin of the arduino, and the ground pin is common in between the led and the sensor. We will program the Arduino so that the brightness of the LED will change depending on the concentration of the gas present in the atmosphere.
Arduino Code for Interfacing MQ-2 Gas Sensor Module
The code for the Arduino mq-2 gas sensor module is very simple and easy to understand. We are just reading the analog data out of the sensor and changing the brightness of the LED according to the received data. Please do remember that we are only processing the analog data coming out of the sensor for the digital data you can see the onboard LED in the module lights up.
We initialize our code by declaring two macros, the first one is for the led where we will connect an LED and the second one is the sensorPin through which we are reading the data coming out of the sensor.
// Sensor pins pin D6 LED output, pin A0 analog Input #define ledPin 6 #define sensorPin A0
Next, we have our setup() function. In the setup function, we initialize the serial with 9600 baud. We also set the ledPin as output, and make the pin LOW. This way the pin will not float and turn the LED on.
void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); }
Next, we have our loop() function, in the loop function we print "Analog output:" as text on the serial monitor window and then we call the readSensor() function inside a Serial.println() function so that once the readSensor() function is executed, it returns the data and it also gets printed on the serial monitor window,
void loop() { Serial.print("Analog output: "); Serial.println(readSensor()); delay(500); }
Finally, we have our custom readSensor() function which returns the analog value that is read through the A0 pin of the Arduino. In the first line of this function, we have declared and defined a variable called sensorValue where we are putting the raw data that is read through the A0 in the Arduino. This data is in 10-bit format and it goes from 0 -1023 so to convert that 10-bit data to 8-bit data. That is why we have used the map function, once the map function outputs the data we have initiated another variable outputValue and put the mapped data inside that variable. Finally, we have used the built-in analogWrite(ledPin, outputValue) function of the Arduino to generate a PWM signal that is proportional to the input data read by the ADC of the arduino
This marks the end of the code portion of the Arduino MQ-2 Gas Sensor code. If you have any questions regarding the code do not hesitate to comment it down below.
// This function returns the analog data to calling function int readSensor() { unsigned int sensorValue = analogRead(sensorPin); // Read the analog value from sensor unsigned int outputValue = map(sensorValue, 0, 1023, 0, 255); // map the 10-bit data to 8-bit data if (outputValue > 65) analogWrite(ledPin, outputValue); // generate PWM signal else digitalWrite(ledPin, LOW); return outputValue; // Return analog moisture value }
Note: please note that the code is so written that the led will trigger after it reaches a certain value, but if your application requires this feature to be turned off then you can do that simply by removing the analog write function inside the if…else statement.
Working of the MQ-2 Gas Sensor Module
The gif down below shows the MQ-2 Gas Sensor in working. At first, you can see the intensity of the LED on the breadboard is low but when a little bit of water is added to the pot, the intensity of the LED increases, and with that, the onboard led of the module also lights up.
Projects using Arduino and MQ-2 Gas Sensor Module
Previously we have used this Gas sensor to build many interesting projects. If you want to know more about those topics, links are given below.
Build your very own PPM meter with gas sensing capabilities with Arduino, the sensor used in this project is a MQ6 so with its high resolution its perfect for making a PPM meter.
Make your very own smoke sensor with an Arduino and MQ-2 sensor, in this project you can learn how to calibrate the MQ-2 sensor to specifically react to smoke, other than that we have also built a custom PCB board for easy installation and application.
If you are trying to build a project where detection of alcohol, benzeen, NOx, NH3 becomes necessary, then this project is for you, in this project we have used a MQ-135 that specializes in sensing this type of gasses.
Build your own PPM meter with Ammonia Detection capability with Arduino and MQ-137 sensor. As we are using the MQ-137 sensor not only ammonia this sensor can also measure trimethylamine, and cholamine.
Supporting Files
// Sensor pins pin D6 LED output, pin A0 analog Input
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to calling function
int readSensor() {
unsigned int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
unsigned int outputValue = map(sensorValue, 0, 1023, 0, 255); // map the 10-bit data to 8-bit data
if (outputValue > 65)
analogWrite(ledPin, outputValue); // generate PWM signal
else
digitalWrite(ledPin, LOW);
return outputValue; // Return analog moisture value
}