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:
- Establishing the connection between the gas sensor and Arduino Nano ESP32
- Programming Arduino Nano ESP32 to retrieve readings from the gas sensor
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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.
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.
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.
This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code - Read value from DO pin
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.
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
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.
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 sensor or component. The Arduino Nano ESP32's ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, keep the following in mind:
- The Arduino Nano ESP32's ADC is not perfectly accurate and might require calibration for correct results. Each Arduino Nano ESP32 board can vary slightly, so calibration is necessary for each individual board.
- Calibration can be challenging, especially for beginners, and might not always yield the exact results you want.
For projects requiring high precision, consider using an external ADC (e.g ADS1115) with the Arduino Nano ESP32 or using another Arduino, such as the Arduino Uno R4 WiFi, which has a more reliable ADC. If you still want to calibrate the Arduino Nano ESP32's ADC, refer to the ESP32 ADC Calibration Driver.