Arduino Mega - Gas Sensor

This guide explains how to use an Arduino Mega and an MQ2 gas sensor to check air quality by measuring gases such as LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide, along with other flammable gases.

Arduino Mega Gas Sensor

Hardware Preparation

1×Arduino Mega
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

Or you can buy the following 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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of MQ2 Gas Sensor

MQ-2 gas sensor can sense LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide in the air. It has a digital output pin and an analog output pin to connect with other devices.

The MQ2 gas sensor does not give details for each gas separately. It only tells you if the gases are mixed together or if more than one gas is present.

We can use the MQ2 sensor to find out if there is a gas leak or if the air is bad. This helps us stay safe by triggering an alarm or starting the ventilation system.

Pinout

The MQ2 gas sensor has four pins.

  • VCC pin: Connect it to +5V power.
  • GND pin: Connect it to ground (0V).
  • DO pin: This is a digital output pin. It shows LOW when flammable gases are detected and HIGH when no gas is detected. You can adjust the gas detection level with a small adjustable part.
  • AO pin: This is an analog output pin. It gives a voltage that changes with the amount of gas. More gas means higher voltage, less gas means lower voltage.
MQ2 Gas Sensor Pinout

It also has two LED lights.

  • One PWR-LED light shows that power is on.
  • One DO-LED light shows gas presence based on the DO pin value: it lights up when there is gas and turns off when there is no gas.

How It Works

About the DO pin:

  • The device has an adjustment knob to change how sensitive the gas detector is.
  • If the gas level near the sensor is higher than the set limit, the sensor output goes low and the LED lights up.
  • If the gas level near the sensor is lower than the set limit, the sensor output goes high and the LED turns off.

About the AO pin:

  • If there is more gas, the voltage rises.
  • If there is less gas, the voltage falls.

The pot does not change the AO pin's value.

The MQ2 Sensor Warm-up

Warm up the MQ2 gas sensor before using it.

  • If you haven't used the sensor for a long time (more than one month), warm it up for 24 to 48 hours before using it to get accurate results.
  • If you used the sensor recently, it only needs 5 to 10 minutes to warm up. At first the readings may be high, but they will go down and become steady after a short time.

To warm up the MQ2 sensor, connect its VCC and GND pins to a power source or to the VCC and GND pins on an Arduino Mega, and leave it connected for a little while.

Wiring Diagram

The MQ2 gas sensor has two output pins. You can use one or both, depending on what you need.

The wiring diagram between Arduino Mega MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code - Read value from DO pin

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-gas-sensor */ #define DO_PIN 2 // The Arduino Mega 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"); }

Detailed Instructions

Follow these steps one by one:

  • Connect the Arduino Mega board to the gas sensor using the diagram that comes with it.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the right board (Arduino Mega) and the correct COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Put the MQ2 gas sensor near the smoke or gas you want to detect.
  • Check 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 remember: if the LED is always on or never turns on, you can turn the small knob (potentiometer) to change how sensitive the sensor is.

Arduino Mega Code - Read value from AO pin

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-gas-sensor */ #define AO_PIN A0 // The Arduino Mega 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); }

Detailed Instructions

  • Copy the code above and open it in the Arduino software.
  • Click the Upload button in the Arduino software to upload the code to the Arduino Mega.
  • Place the MQ2 gas sensor near the smoke or gas you want to detect.
  • Check the result in 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: 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  

Using the DO or AO readings, you can judge the air quality by your own standards, or trigger an alarm or turn on the ventilation system.

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!