MQ2 Gas Sensor Tutorial: Measuring Gas Concentration with PictoBlox

MQ-2-Gas-Sensor-2
Description
Learn how to use the MQ2 gas sensor to measure the concentration of gases in the environment. This tutorial covers the four pins of the MQ2 gas sensor, equations to calculate resistance ratio and PPM, and how to use PictoBlox to detect gases in the environment.

Introduction

Gas sensors are designed to measure the concentration of gases in the environment. MQ2 gas sensor 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.

evive Notes Icon
Note: The sensor value only reflects the approximated trend of gas concentration in a permissible error range, it DOES NOT represent the exact gas concentration. The detection of certain components in the air usually requires a more precise and costly instrument, which cannot be done with a single gas sensor.

MQ series sensor uses a small heater inside with an electrochemical sensor in order to measure a different kind of gases combination. They can be calibrated, but, in order to do that, a known concentration of measured gas or gases is needed. In our case, we will test it, as it comes from the producer without any additional calibration. The MQ2 Gas sensor has a built-in variable resistor that changes its value according to the concentration of gas. If the concentration is High, the resistance decreases,  if the concentration is low, the resistance increases. Besides the built-in resistor, it is necessary to include load resistor, Load resistor serve to adjust sensor sensitivity and accuracy, it’s value can range from 2k ohm to 47 k ohm higher the value, the more sensitive sensor becomes.

Pin description

MQ2 Gas sensor has 4 pins

  • VCC
  • GND
  • AOUT  (Analog Output pin)
  • DOUT  (Digital Output pin)

Circuit diagram

  • Connect “VCC” of the MQ2 gas sensor with “VCC” of evive
  • Connect “GND” of MQ2 gas sensor with “GND” of evive
  • Connect “Aout ” pin of MQ2 gas sensor with ” A0″ pin of evive

Equations

MQ-2 Gas Sensor Sensitivity Characteristics:

Gas Sensor MQ2

The graph tells us the concentration of a gas in part per million (ppm) according to the resistance ratio of the sensor (RS/R0).

  1. RS is the resistance of the sensor that changes depending on the concentration of gas.
  2. R0 is the resistance of the sensor at a known concentration without the presence of other gases, or in the fresh air.

For air, RS/R0 = 9.8 for MQ2 gas sensor.

evive Notes Icon
Note: According to the graph, we can see that the minimum concentration we can test is 100ppm and the maximum is 10000ppm, in another word, we can get a concentration of gas between 0.01% and 1%.

Calculation of R0 for the Sensor

RS = [(Vin x RL) / Vout] - RL
  1. Vin is 5V in our case.
  2. RL is 10 kOhm
  3. Vout is the analog voltage reading from the sensor

We can simplify the above formula by omitting RL:

RS = (Vin - Vout) / Vout

From the graph, we can see that the resistance ratio in fresh air is a constant:

RS / R0 = 9.8

To calculate R0 we will need to find the value of the RS in the fresh air using the above formula. This will be done by taking the analog average readings from the sensor and converting it to voltage. Then we will use the RS formula to find R0.

R0 = RS / 9.8

Calculating PPM for a particular gas

Let’s analyze the graph:

  1. The scale of the graph is log-log. This means that in a linear scale, the behavior of the gas concentration with respect to the resistance ratio is exponential.
  2. The data for gas concentration only ranges from 200 ppm to 10000 ppm.
  3. Even though the relation between resistance ratio and gas concentration may seem linear, in reality, it is not.

First of all, we will treat the lines as if they were linear. This way we can use one formula that linearly relates the ratio and the concentration. By doing so, we can find the concentration of a gas at any ratio value even outside of the graph’s boundaries. The formula we will be using is the equation for a line, but for a log-log scale. The formula for a line is:

  y = mx + b

Where:

y: X value 
x: X value 
m: Slope of the line 
b: Y intercept

For a log-log scale, the formula looks like this:

  log(y) = m*log(x) + b
evive Notes Icon
Note: the log is base 10.

Okay, let’s find the slope. To do so, we need to choose 2 points from the graph.

In our case, we chose the points (200,1.6) and (10000,0.27) from the LPG line. The formula to calculate m is the following:

m = [log(y) - log(y0)] / [log(x) - log(x0)]

If we apply the logarithmic quotient rule we get the following:

m = log(y/y0) / log(x/x0)

Now we substitute the values for x, x0, y, and y0:

m = log(0.27/1.6) / log(10000/200)
m = -0.473

Now that we have m, we can calculate the y-intercept. To do so, we need to choose one point from the graph (once again from the LPG line). In our case, we chose (5000,0.46)

log(y) = m*log(x) + b
b = log(y) - m*log(x)
b = log(0.46) - (-0.473)*log(5000)
b = 1.413

Now that we have m and b, we can find the gas concentration for any ratio with the following formula:

log(x) = [log(y) - b] / m

However, in order to get the real value of the gas concentration according to the log-log plot we need to find the inverse log of x:

x = 10 ^ {[log(y) - b] / m}

In the table given below, you can find the value of m and b for different gases.

[table “” not found /]

PictoBlox (Scratch Program)

evive Notes Icon
Note: We will write the code in Stage Mode.

There are two steps to calculate PPM for the gas:

  1. First, we will calculate the value of R0. To calculate R0 we need to find out the value of Rs in the fresh air, this will be done by taking analog average readings from the sensor and converting it into the corresponding voltage value, then we will use above formula to calculate R0, wait until we get the stable value of R0. Make this script in the PictoBlox to get the value of R0.
    MQ2 Gas Sensor Script 1Download the program from here: Gas Sensor Calculating R0 value for MQ2 Sensor
    Run the code and find a stable value of R0. Usually, it takes 5-15 min to get a stable value.

    evive Alert
    Alert: Do not disconnect the device in-between.

    MQ2 Gas Sensor ReadingAfter running the script for 15 minutes the value stabilize to 0.4388

  2. After that, we will use the above-calculated value of R0 to find out the concentration of gases in ppm. If the ppm is higher than 2000, then the Pin 13 LED will glow.
    evive Notes Icon
    Note: You have to replace the value of R0 in the program with the value you have got in the previous program.

    Calculating PPM value MQ2 SensorDownload the program from here: Gas Sensor Calculating PPM for MQ2 Sensor and LPG Gas

PPM reading for MQ2 Sensor

evive Notes Icon
Note: If you want to detect other gases, change the value of b and m in the program according to the sensor from the table.

Conclusion

In this lesson, we explored the MQ2 gas sensor and used PictoBlox to program it. We learned about the four pins of the MQ2 gas sensor, the equations that can be used to calculate resistance ratio and PPM, and how to use PictoBlox to detect gases in the environment. We also saw how to use the sensor to detect the presence of LPG gas. With this knowledge, you can now use the MQ2 gas sensor to detect other gases as well.

Table of Contents