# MQ3 Gas Sensor Tutorial: Measuring Gas Concentration with PictoBlox

Description
Learn how to use the MQ3 gas sensor to measure the concentration of gases in the environment. Understand the sensor’s working principle, equations to calculate R0, and how to adjust its sensitivity. Follow our PictoBlox tutorial to write two programs to calculate R0 and the concentration of gas in ppm.

## Introduction

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

MQ3 Gas sensor has 4 pins

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

## Circuit Diagram

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

## Equations

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

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 = 60 for MQ3 gas sensor.

Note: According to the graph, we can see that the minimum concentration we can test is 0.1ppm and the maximum is 10ppm. Values between 0.1 to 10ppm will be more accurate for the sensor.

### 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 = 60`

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 / 60`

### 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 0.1 to 10 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`
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 (0.1,51) and (10,14) 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(14/51) / log(10/0.1)
m = -0.33```

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 (6,16)

`log(y) = m*log(x) + b`
`b = log(y) - m*log(x)`
`b = log(16) - (-0.33)*log(6)`
`b = 2.426`

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.

## PictoBlox (Scratch Program)

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.
Download the program from here: Gas Sensor Calculating R0 value for MQ3 Sensor
Run the code and find a stable value of R0. Usually, it takes 5-10 min to get a stable value.

Alert: Do not disconnect the device in-between.

After 10 minutes the value is stabilized to 0.0507.

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

## Conclusion

In this lesson, we learned how to use the MQ3 gas sensor to measure the concentration of gases in the environment. We learned how the sensor works and how to use equations to calculate the R0 value of the sensor and the concentration of gas in parts-per-million (ppm). We also wrote two PictoBlox programs to calculate the R0 value of the sensor and the concentration of gas in ppm. Finally, we learned how to adjust the sensitivity of the sensor by changing the value of the load resistor.