Arduino Nano ESP32 - Gas Sensor

This tutorial will guide you through the process of utilizing Arduino Nano ESP32 and the MQ2 gas sensor to assess air quality by examining the levels of various flammable gases such as LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide. We will cover the following aspects in detail:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×MQ2 Gas Sensor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Adapter 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. We appreciate your support.

Overview of MQ2 Gas Sensor

The MQ2 gas sensor can detect the presence of various gases like LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide in the surrounding environment. 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 specific information about each gas individually. Instead, it informs us about the combination of gases or the presence of gases as a whole.

By utilizing the MQ2 sensor, we can identify if there is a gas leak or if the air quality is poor. This information enables us to take appropriate actions to ensure our safety, such as activating an alarm or turning on ventilation systems.

Pinout

The MQ2 gas sensor consists of four pins with specific functions:

  • VCC pin: This pin needs to be connected to the VCC (5V).
  • GND pin: This pin needs to be connected to the GND (0V).
  • DO pin: It is a digital output pin that indicates the presence of flammable gases. When flammable gas concentration is detected, the pin outputs a LOW signal; otherwise, it outputs a HIGH signal. The threshold value for detecting gas concentration can be adjusted using a built-in potentiometer.
  • AO pin: It is an analog output pin that generates an analog voltage proportional to the gas concentration. When the gas concentration increases, the voltage output also increases, and when the gas concentration decreases, the voltage output decreases correspondingly.
MQ2 Gas Sensor Pinout

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

  • PWR-LED indicator: This LED serves as a power indicator, indicating that the sensor is receiving power. It is turned on when the sensor is powered and functioning.
  • DO-LED indicator: This LED is linked to the DO pin of the sensor. It provides a visual representation of the gas concentration based on the value received from the DO pin. When the gas concentration is present and the DO pin is set to LOW, the DO-LED indicator turns on. Conversely, if no gas concentration is detected and the DO pin is set to HIGH, the DO-LED indicator turns off.

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.
  • If the gas concentration in the surrounding environment exceeds the set threshold, the output pin of the sensor is set to LOW, and the DO-LED turns on.
  • Conversely, if the gas concentration falls below the set threshold, the output pin of the sensor is set to HIGH, and the DO-LED turns off.

Regarding the AO pin:

  • As the gas concentration increases, the voltage on the AO pin also increases proportionally.
  • Conversely, when the gas concentration decreases, the voltage on the AO pin decreases accordingly.

It's important to note that adjusting 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. Here are the details:

  • When using the sensor for the first time after a long period of storage (around a month or more), it is necessary to warm it up for 24-48 hours. This extended warm-up time ensures accurate operation.
  • If the sensor has been used recently, the warm-up time is significantly shorter. It typically takes only 5-10 minutes for the sensor to fully warm up. During this warm-up period, 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 ESP32. Allow the sensor to remain in this state for the required warm-up period.

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 ESP32 and the MQ2 gas sensor when powering via USB port.
The wiring diagram between Arduino Nano ESP32 and MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Nano ESP32 and the MQ2 gas sensor when powering via Vin pin.
The wiring diagram between Arduino Nano ESP32 and air quality

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code - Read value from DO pin

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-gas-sensor */ #define DO_PIN D2 // The Arduino Nano ESP32'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 ESP32'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

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
  • 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 ESP32 Code - Read value from AO pin

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-gas-sensor */ #define AO_PIN A5 // The Arduino Nano ESP32'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 ESP32
  • 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: 1938 MQ2 sensor AO value: 1954 MQ2 sensor AO value: 2000 MQ2 sensor AO value: 3002 MQ2 sensor AO value: 4014 MQ2 sensor AO value: 4017
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!