Arduino - Gas Sensor

In this tutorial, we are going to learn how to use Arduino and MQ2 gas sensor to check the air quality by checking the concentrations of LPG, smoke, alcohol, propane, hydrogen, methane and carbon monoxide, or other flammable gasses. In detail, we will learn:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×MQ2 Gas Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

About MQ2 Gas Sensor

The MQ2 gas sensor is capable of detecting the presence of LPG, smoke, alcohol, propane, hydrogen, methane and carbon monoxide concentrations in the surrounding environment. The MQ2 gas sensor provides two options via a digital output pin and analog output pin.

Please note that the MQ2 gas sensor does not provide information about each gas separately. Instead, it gives information about the gas combination or the presence of gasses as a whole.

By using the MQ2 sensor, we can determine if there is a gas leak or if the air quality is not good. This information can help us take appropriate actions to stay safe, like triggering an alarm or turning on ventilation systems.

Pinout

The MQ2 gas sensor includes four pins:

  • VCC pin: It needs to be connected to VCC (5V).
  • GND pin: It needs to be connected to GND (0V).
  • DO pin: A digital output pin indicates the presence of flammable gasses. It is LOW if the gasses concentration is detected, and HIGH if otherwise. The threshold value for detecting gasses concentration can be adjusted using a built-in potentiometer.
  • AO pin: An analog output pin generates an analog output voltage that changes proportionally with the gas concentration. When the gas concentration increases, the voltage also rises, and when the gas concentration decreases, the voltage decreases accordingly.
MQ2 Gas Sensor Pinout

Furthermore, it has two LED indicators:

  • One PWR-LED indicator for power.
  • One DO-LED indicator for the gas concentration based of the value on DO pin: it is on when gas concentration is present and off if otherwise.

How It Works

For the DO pin:

  • The module has a built-in potentiometer for setting the gasses concentration threshold (sensitivity).
  • When the gasses concentration in the surrounding environment is above the threshold value, the output pin of the sensor is LOW, and the DO-LED is on.
  • When the gasses concentration in the surrounding environment is below the threshold value, the output pin of the sensor is HIGH, and the DO-LED is off.

For the AO pin:

  • When the gas concentration increases, the voltage also rises.
  • When the gas concentration decreases, the voltage decreases accordingly.

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

The MQ2 Sensor Warm-up

The MQ2 gas sensor needs to be warmed up before using.

  • When you first use the sensor after it has been stored for a long time (a month or more), it needs to be warmed up for 24-48 hours to ensure it works accurately.
  • If the sensor has been used recently, it will only take 5-10 minutes to fully warm up. During this warm-up period, the sensor may initially give high readings, but they will gradually decrease until it stabilizes.

To warm up the MQ2 sensor, just connect its VCC and GND pins to power supply, or connect them to VCC and GND of Arduino, and then keep it for a period of time.

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 and the MQ2 gas sensor when using DO only.
Arduino MQ2 gas sensor wiring diagram

This image is created using Fritzing. Click to enlarge image

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

This image is created using Fritzing. Click to enlarge image

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

This image is created using Fritzing. Click to enlarge image

Arduino Code - Read value from DO pin

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-gas-sensor */ #define DO_PIN 2 // Arduino's pin connected to DO pin of the MQ2 sensor void setup() { // initialize serial communication Serial.begin(9600); // initialize the Arduino'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"); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Place the MQ2 gas sensor near the smoke/gas you want to detect
  • See the result on 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 Code - Read value from AO pin

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-gas-sensor */ #define AO_PIN A0 // Arduino's pin connected to AO pin of the MQ2 sensor void setup() { // initialize serial communication 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); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Place the MQ2 gas sensor near the smoke/gas you want to detect
  • See the result on 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

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES