Arduino Nano - Gas Sensor

This tutorial instructs you how to use Arduino Nano and the MQ2 gas sensor to assess the quality of the air by measuring the levels of gases like LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×MQ2 Gas Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of MQ2 Gas Sensor

The MQ2 gas sensor is a module that can detect the levels of LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide in the air around it. It offers two output options: a digital output pin and an analog output pin.

It's important to note that the MQ2 gas sensor doesn't provide individual information about each gas. Instead, it gives us a general idea about the overall presence of gases or gas combinations.

Using the MQ2 sensor, we can determine if there is a gas leak or if the air quality is poor. This information is valuable for taking appropriate safety measures, such as activating an alarm or turning on ventilation systems.

Pinout

The MQ2 gas sensor consists of four pins that serve different functions:

  • VCC pin: This pin needs to be connected to the VCC (5V) power supply.
  • GND pin: This pin needs to be connected to the GND (0V) ground connection.
  • DO pin: This is a digital output pin that indicates the presence of flammable gases. When flammable gas concentrations are detected, the DO pin output is set to LOW. If no flammable gases are detected, the output is set to HIGH. The threshold for gas concentration detection can be adjusted using a built-in potentiometer.
  • AO pin: This is an analog output pin that generates an analog voltage output. The voltage changes proportionally with the gas concentration. When the gas concentration increases, the voltage also increases, and when the gas concentration decreases, the voltage decreases accordingly.
MQ2 Gas Sensor Pinout

Additionally, the MQ2 gas sensor is equipped with two LED indicators:

  • PWR-LED indicator: This LED indicates the power status of the sensor. It is typically on when the sensor is receiving power.
  • DO-LED indicator: This LED is directly linked to the DO pin of the sensor. It reflects the gas concentration based on the value of the DO pin. When gas concentration is detected, the LED turns on, and when there is no gas concentration, the LED turns off. The DO-LED serves as a visual indication of the presence or absence of gas concentration.

How It Works

Regarding the DO pin:

  • The MQ2 module features a built-in potentiometer that allows you to adjust the sensitivity or threshold for gas concentration.
  • When the gas concentration in the surrounding environment exceeds the set threshold value, the output pin of the sensor becomes LOW, and the DO-LED indicator turns on.
  • Conversely, when the gas concentration falls below the threshold value, the output pin of the sensor becomes HIGH, and the DO-LED indicator turns off.

Regarding the AO pin:

  • The voltage on the AO pin of the sensor increases as the gas concentration rises.
  • Conversely, as the gas concentration decreases, the voltage on the AO pin decreases accordingly.

It's important to note that the potentiometer does not affect the value observed on the AO pin.

Note that the potentiometer does not affect the value on the AO pin.

The MQ2 Sensor Warm-up

The MQ2 gas sensor requires a warm-up period before it can be used effectively.

  • If the sensor has been stored for a long time (around a month or more) without use, it needs to be warmed up continuously for 24-48 hours. This extended warm-up ensures the sensor operates accurately.
  • However, if the sensor has been used recently, it only requires a warm-up period of 5-10 minutes. During this time, the sensor may initially provide high readings, but these readings will gradually decrease until the sensor stabilizes.

To warm up the MQ2 sensor, simply connect its VCC and GND pins to a power supply, or connect them to the VCC and GND pins of an Arduino Nano board, and leave it for the specified warm-up duration.

Wiring Diagram

Since the MQ2 gas sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • The wiring diagram between Arduino Nano and the MQ2 gas sensor when using DO only.
The wiring diagram between Arduino Nano and MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Nano and the MQ2 gas sensor when using AO only.
The wiring diagram between Arduino Nano and air quality

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Nano and the MQ2 gas sensor when using both AO an DO.
The wiring diagram between Arduino Nano and smoke sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Read value from DO pin

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-gas-sensor */ #define DO_PIN 2 // Arduino Nano's pin connected to DO pin of the MQ2 sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano's pin as an input pinMode(DO_PIN, INPUT); Serial.println("Warming up the MQ2 sensor"); delay(20000); // wait for the MQ2 to warm up } void loop() { int gasState = digitalRead(DO_PIN); if (gasState == HIGH) Serial.println("The gas is NOT present"); else Serial.println("The gas is present"); }

Detailed Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano
  • Place the MQ2 gas sensor near the smoke/gas you want to detect
  • Check out the result on the Serial Monitor.
COM6
Send
The gas is NOT present The gas is NOT present The gas is NOT present The gas is NOT present The gas is NOT present The gas is present The gas is present The gas is present The gas is present The gas is present
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Please keep in mind that if you notice the LED status remaining on constantly or off, you can adjust the potentiometer to fine-tune the sensitivity of the sensor.

Arduino Nano Code - Read value from AO pin

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-gas-sensor */ #define AO_PIN A5 // Arduino Nano's pin connected to AO pin of the MQ2 sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); Serial.println("Warming up the MQ2 sensor"); delay(20000); // wait for the MQ2 to warm up } void loop() { int gasValue = analogRead(AO_PIN); Serial.print("MQ2 sensor AO value: "); Serial.println(gasValue); }

Detailed Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano
  • Place the MQ2 gas sensor near the smoke/gas you want to detect
  • Check out the result on the Serial Monitor.
COM6
Send
MQ2 sensor AO value: 135 MQ2 sensor AO value: 136 MQ2 sensor AO value: 136 MQ2 sensor AO value: 573 MQ2 sensor AO value: 674 MQ2 sensor AO value: 938 MQ2 sensor AO value: 954 MQ2 sensor AO value: 1000 MQ2 sensor AO value: 1002 MQ2 sensor AO value: 1014 MQ2 sensor AO value: 1017
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

From values read from DO or AO, you can infer the air quality based on your standard, or trigger an alarm or turn on ventilation systems.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!