How to use MQ2 gas sensor with Arduino | Step by step instructions

How to use MQ2 gas sensor with Arduino

Hello, welcome back to another tutorial from the SriTu Hobby. In this tutorial, we will learn what is the MQ2 sensor and how it works with an Arduino. We have already described the MQ3 sensor in a previous tutorial. Click this link for MQ3 senor. This sensor also works in the same way as the MQ3 sensor. Also, this MQ2 sensor can measure the amount of LP gas, smoke, carbon monoxide, and methane in the air. This tutorial explains how to make an LP Gas Measurement Project with Arduino step by step. We can buy this sensor in the market at a very low price

How to use MQ2 gas sensor with Arduino

The structure of this sensor

The sensor is made of 6 pins with a stainless steel mesh cover on the outside. This mesh is designed so that only air particles can pass through. Also, this iron mesh connects to the sensor body using a stainless steel ring. These sensors belong to the metal oxide semiconductor type. That is, when a gas enters the sensor, the concentration of the gas is detected according to the degree to which the resistance of the sensor element changes.

How to use MQ2 gas sensor with Arduino

Okay, now let’s talk about the internal structure of these sensors. The six legs here are connected to a sensor element from within. Also, two of these legs perform the function of heating this sensor’s element. These two legs are connected by a nickel-chromium coil. The other four legs are connected to the body of the sensor element using platinum wire. These give the signals.

How to use MQ2 gas sensor with Arduino

Next, let’s talk about the sensor element. This sensor element is made of ceramics using aluminum oxide ceramics. It also has a tin dioxide coating on the outside. This tin dioxide element is more sensitive to combustible gases.

How to use MQ2 gas sensor with Arduino

How does the MQ2 sensor work?

When this sensor is powered on, the tin dioxide heats up to a high temperature. At that point, oxygen is absorbed to the surface. Then, when the gas we are measuring is brought close to this sensor, it reacts with the tin dioxide to reduce the surface absorption of oxygen. After, the electrons are released into the tin dioxide. Then the current flows freely. In this case, the resistance difference can be received as an analog or digital value through the voltage divider circuit in the module to which this sensor is connected.

PIN structure of this sensor

How to use MQ2 gas sensor with Arduino

Okay, step by step we will learn how to connect this sensor with Arduino. The required components are as follows.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

How to use MQ2 gas sensor with Arduino

Step 3

Thirdly, let’s creates the program. It is as follows.

I2C library — Download

/*MQ2 sensor with Arduino.
 * http://srituhobby.com
 */
 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define LED 2
#define Buzzer 3
#define Sensor A1

void setup() {
  Serial.begin(9200);
  lcd.init();
  lcd.backlight();
  pinMode(LED, OUTPUT);
  pinMode(Buzzer, OUTPUT);
}

void loop() {
  int value = analogRead(Sensor);
  lcd.setCursor(0, 0);
  lcd.print("Value :");
  lcd.print(value);
  lcd.print("  ");

  if (value > 400) {
    digitalWrite(LED, HIGH);
    digitalWrite(Buzzer, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("GAS Detected!");
  } else {
    digitalWrite(LED, LOW);
    digitalWrite(Buzzer, LOW);
    lcd.setCursor(0, 1);
    lcd.print("             ");
  }
}

Code explanation

Firstly, the I2C library is included. Later, an object was created for this library, which included the LCD width and length.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

Secondly, the sensor, LED and buzzer pins are defined.

#define LED 2
#define Buzzer 3
#define Sensor A1

In the setup function. The LCD display is started. Also, the LED and buzzer pins are set as output pins.

void setup() {
  Serial.begin(9200);
  lcd.init();
  lcd.backlight();
  pinMode(LED, OUTPUT);
  pinMode(Buzzer, OUTPUT);
}

In the loop function, the sensor values are read and put into the integer variable. After, these values are printed on the LCD display.

  int value = analogRead(Sensor);
  lcd.setCursor(0, 0);
  lcd.print("Value :");
  lcd.print(value);
  lcd.print("  ");

Then, these values are checked using the IF condition. If the sensor value is greater than 400, the LED and buzzer are activated. Also, printed on the LCD as “Gas detected”. Otherwise the LED and buzzer will be deactivated.

if (value > 400) {
    digitalWrite(LED, HIGH);
    digitalWrite(Buzzer, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("GAS Detected!");
  } else {
    digitalWrite(LED, LOW);
    digitalWrite(Buzzer, LOW);
    lcd.setCursor(0, 1);
    lcd.print("             ");
  }

Step 4

Lastly, select board and port. Afterward, upload this code to the Arduino board.

OK, enjoy this tutorial. The full video guide is given below. So, we will meet in the next tutorial.

Similar Posts

Leave a Reply

Your email address will not be published.