ESP32 - Gas Sensor

This tutorial will guide you through the process of utilizing 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 Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×MQ2 Gas Sensor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

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 these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

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

This image is created using Fritzing. Click to enlarge image

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

This image is created using Fritzing. Click to enlarge image

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

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Read value from DO pin

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-gas-sensor */ #define DO_PIN 14 // ESP32's pin GPIO14 connected to DO pin of the MQ2 sensor void setup() { // initialize serial communication 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"); }

Quick Instructions

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

ESP32 Code - Read value from AO pin

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-gas-sensor */ #define AO_PIN 36 // ESP32's pin GPIO36 connected to AO pin of the MQ2 sensor void setup() { // initialize serial communication Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); 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 Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • 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: 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.

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a gas sensor. The ESP32 ADC is good for projects that do NOT need high accuracy. However, for projects that need precise measurements, please note:

  • The ESP32 ADC is not perfectly accurate and might need calibration for correct results. Each ESP32 board can be a bit different, so you need to calibrate the ADC for each individual board.
  • Calibration can be difficult, especially for beginners, and might not always give the exact results you want.

For projects that need high precision, consider using an external ADC (e.g ADS1115) with the ESP32 or using an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to ESP32 ADC Calibration Driver

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES