💨 Determining the concentration of hydrocarbon gases using an MQ-2 sensor

💨 Determining the concentration of hydrocarbon gases using an MQ-2 sensor

Daniil Zhuk

One of the most important tasks in the issue of security of a smart home is gas leak detection. In order for the Arduino board to successfully solve problems of this kind, you need to connect the MQ-2 gas sensor to it. Sensor MQ-2 (fig. 4.24) will determine the concentration of hydrocarbon gases (propane, methane, n-butane), smoke (suspended particles resulting from combustion) and hydrogen in the environment. The sensor can be used to detect gas leaks and smoke. In the gas analyzer built-in heating element, which is necessary for a chemical reaction. Therefore, during operation, the sensor will be hot. To obtain a stable reading, a new sensor must be heated once (left on) for 24 hours. After this, stabilization after switching on takes about a minute.

Depending on the level of gas in the atmosphere, the internal resistance of the sensor changes. MQ-2 has an analog output, so the voltage at this output will vary in proportion to the level of gas in the environment. To determine the logical level also has a digital output. The sensor module has a built-in potentiometer that allows you to adjust the sensitivity of this sensor depending on how accurately you want to record the gas level.
Now about the units of measurement. On the territory of the former Soviet Union, indicators are usually measured in percent (%) or directly in mass to volume (mg / m3). In foreign countries, it applies such a figure as ppm.
Reducing ppm stands for parts per million (parts per million). For example, 1 ppm = 0.0001%.
Sensor measurement range:

• Propane: 200–5000 ppm;
• Bhutan: 300–5000 ppm;
• Methane: 500–20000 ppm;
• Hydrogen: 300–5000 ppm.

Consider connecting the MQ-2 sensor to the Arduino Mega board and the NodeMcu ESP8266 module.

4.5.1. Connecting the MQ-2 sensor to the Arduino Mega board

Connecting the MQ-2 sensor to the Arduino Mega board, we will produce via the analog input. We also take the power for the sensor from the Arduino board. The connection diagram is shown

Let's load onto the Arduino Mega board a sketch of receiving data from the MQ-2 sensor and output to the Arduino serial port. Procedures for determining data coming from an analog input:

• get_data_ppmpropan () - propane content in ppm;
• get_data_ppmmethan () - propane content in ppm;
• get_data_ppmsmoke () - smoke content.

#include <TroykaMQ.h>

#define INTERVAL_GET_DATA 2000  
#define MQ2PIN         A10
MQ2 mq2(MQ2PIN);

unsigned long millis_int1=0;
 
void setup() {
  Serial.begin(9600);
  mq2.calibrate();
  mq2.getRo();
}
 
void loop() {
   if(millis()-millis_int1 >= INTERVAL_GET_DATA) {
      float  propan= get_data_ppmpropan();
      Serial.print("propan=");
      Serial.print(propan);
      Serial.println(" ppm ");
      float  methan= get_data_ppmmethan();
      Serial.print("methan=");
      Serial.print(methan);
      Serial.println(" ppm ");
      float  smoke= get_data_ppmsmoke();
      Serial.print("smoke=");
      Serial.print(smoke);
      Serial.println(" ppm ");
      // старт интервала отсчета
      millis_int1=millis();
   }
}

float get_data_ppmpropan()  {

  Serial.println(mq2.readRatio());
  float value=mq2.readLPG();
  
  return value;
}

float get_data_ppmmethan() { Serial.println(mq2.readRatio()); float value=mq2.readMethane(); return value; }
float get_data_ppmsmoke() { Serial.println(mq2.readRatio()); float value=mq2.readSmoke(); return value; }

Download the sketch to the Arduino Mega board, open the serial port monitor and see the output of propane, methane and smoke content

4.5.2. Connecting the MQ-2 sensor to the NodeMcu ESP8266 module

Now consider the connection of the MQ-2 sensor to the NodeMcu ESP8266 module. The MQ-2 sensor is connected to the input of the y2 multiplexer. To select the analog input multiplexer, use the contacts D5, D7, D8 of the module NodeMcu.

Let's load on the NodeMcu module a sketch of receiving data from the MQ-2 sensor and output to the Arduino serial port. To select the analog input of the multiplexer y2, apply the low level signal LOW to the contacts D5, D8, and the high signal HIGH to the contact D7.
Procedures for determining data coming from an analog input:

• get_data_ppmpropan () - propane content in ppm;
• get_data_ppmmethan () - propane content in ppm;
• get_data_ppmsmoke () - smoke content.

#include <TroykaMQ.h>

#define INTERVAL_GET_DATA 2000  
#define MQ2PIN         A0
MQ2 mq2(MQ2PIN);

unsigned long millis_int1=0;
 
void setup() {
   pinMode(14,OUTPUT);
   pinMode(13,OUTPUT);
   pinMode(15,OUTPUT);  
  Serial.begin(9600);
  digitalWrite(14,LOW);
  digitalWrite(13,HIGH);
  digitalWrite(15,LOW);    
  mq2.calibrate();
  mq2.getRo();
}
 
void loop() {
   if(millis()-millis_int1 >= INTERVAL_GET_DATA) {
      digitalWrite(14,LOW);
      digitalWrite(13,HIGH);
      digitalWrite(15,LOW);    
      float  propan= get_data_ppmpropan();
      Serial.print("propan=");
      Serial.print(propan);
      Serial.println(" ppm ");
      float  methan= get_data_ppmmethan();
      Serial.print("methan=");
      Serial.print(methan);
      Serial.println(" ppm ");
      float  smoke= get_data_ppmsmoke();
      Serial.print("smoke=");
      Serial.print(smoke);
      Serial.println(" ppm ");
      millis_int1=millis();
   }
}

float get_data_ppmpropan()  {
  Serial.println(mq2.readRatio());
  float value=mq2.readLPG();
  return value;
}
float get_data_ppmmethan()  {
  Serial.println(mq2.readRatio());
  float value=mq2.readMethane();
  return value;
}
float get_data_ppmsmoke()  {
  Serial.println(mq2.readRatio());
  float value=mq2.readSmoke();
  return value;
}

Download the sketch to the NodeMCU module, open the serial port monitor and see the output of the data received from the MQ-2 sensor

Add a comment

* Comments must be approved before being displayed.