## 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.

**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:

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

- RS is the resistance of the sensor that changes depending on the concentration of gas.
- 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.

**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

- Vin is 5V in our case.
- RL is 10 kOhm
- 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:

- 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.
- The data for gas concentration only ranges from 200 ppm to 10000 ppm.
- 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

**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.

Gas | Value at 200 | Value at 10000 | Value at 5000 | m | b |
---|---|---|---|---|---|

H2 | 2.1 | 0.33 | 0.46 | -0.47305447 | 1.412572126 |

LPG | 1.6 | 0.27 | 0.37 | -0.454838059 | 1.25063406 |

Methane | 3 | 0.7 | 0.94 | -0.372003751 | 1.349158571 |

CO | 5.1 | 1.35 | 1.8 | -0.33975668 | 1.512022272 |

Alcohol | 2.8 | 0.65 | 0.85 | -0.373311285 | 1.310286169 |

Smoke | 3.4 | 0.6 | 0.95 | -0.44340257 | 1.617856412 |

Propane | 1.7 | 0.28 | 0.385 | -0.461038681 | 1.290828982 |

## PictoBlox (Scratch Program)

**Note:**We will write the code in Stage Mode.

There are two steps to calculate PPM for the gas:

- 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.

Download 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.**Alert:**Do not disconnect the device in-between.After running the script for 15 minutes the value stabilize to 0.4388

- 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.
**Note:**You have to replace the value of R0 in the program with the value you have got in the previous program.

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

**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.