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:
- How to connect the MQ2 gas sensor to Arduino Nano
- How to program Arduino Nano to read value from the MQ2 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 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.
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.
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.
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.
This image is created using Fritzing. Click to enlarge image
Arduino Nano Code - Read value from DO pin
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.
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
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.
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.