# Interfacing MQ-4 Gas sensor with evive In this tutorial we will interface MQ-4 Gas sensor module with evive and if Gas concentration exceed specific concentration we will glow led
Tutorial Info

## Components Required

Image Component Quantity Available in Kit evive 1 USB A-B Cable 1 Male to Female Jumper Cable 20cm 1 MQ4 Gas Sensor 1
• None

## Introduction

Gas sensors are designed to measure the concentration of gases in the environment. There are many different types depending on what you want to measure. In our case, we will be using MQ-4 Gas sensor which can measure Liquefied Petroleum Gas(LPG), Hydrogen (H2), Carbon Monoxide (Co) and smoke concentrations. 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 MQ-4 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.

## Circuit diagram • Connect “VCC” of the MQ-4 gas sensor with “VCC” of evive
• Connect “GND” of MQ-4 gas sensor with “GND” of evive
• Connect “Aout ” pin of MQ-4 gas sensor with ” A0″ pin of evive ## Equations

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

### Calculation

`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

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

`RS / R0 = 4.4 ppm  `

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

Gas concentration for LPG is given by:

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

where x is the gas concentration, y is the RS/RO ratio, b = 1.133 and m = -0.318.

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}`

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

## Arduino IDE code

The code is divided into two parts

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

## Expected Result [tutorialRelatedProjects]