Arduino UNO R4 - Gas Sensor

This guide will show you how to use Arduino UNO R4 and MQ2 gas sensor to monitor air quality by measuring levels of LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide, among other flammable gases.

Arduino UNO R4 Gas Sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×MQ2 Gas Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4
1×(Recommended) Power Splitter For Arduino UNO R4

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of MQ2 Gas Sensor

The MQ2 gas sensor can detect LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide levels in the area. It offers a digital output pin and an analog output pin for communication.

The MQ2 gas sensor does not give details for each gas individually. Instead, it provides information about the mix of gases or if there are gases present together.

We can use the MQ2 sensor to find out if there is a gas leak or if the air quality is poor. This helps us to act safely, such as setting off an alarm or starting ventilation systems.

Pinout

The MQ2 gas sensor has four pins:

  • VCC pin: Connect this pin to VCC (5V).
  • GND pin: Connect this pin to GND (0V).
  • DO pin: This is a digital output pin. It shows LOW when flammable gases are detected and HIGH if not. You can adjust the level at which gas is detected using a small adjustable component.
  • AO pin: This is an analog output pin. It produces a voltage that changes depending on the amount of gas. More gas makes the voltage go up, less gas makes it go down.
MQ2 Gas Sensor Pinout

It also has two LED lights.

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

How It Works

For the DO pin:

  • The module includes a potentiometer to adjust the sensitivity for detecting gas concentration.
  • If the gas concentration around is higher than the set level, the sensor’s output pin turns LOW and the DO-LED light turns on.
  • If the gas concentration around is lower than the set level, the sensor’s output pin turns HIGH and the DO-LED light turns off.

For the AO pin:

  • If there is more gas, the voltage goes up.
  • If there is less gas, the voltage goes down.

The potentiometer does not change the value on the AO pin.

The MQ2 Sensor Warm-up

The MQ2 gas sensor must be heated before use.

  • If the sensor hasn't been used for a long period (over a month), please warm it up for 24-48 hours before using it to get accurate results.
  • If the sensor was used not long ago, it requires only 5-10 minutes to warm up. At first, the readings might be high, but they will lower and become steady after a short while.

To heat up the MQ2 sensor, connect its VCC and GND pins to a power source or to the VCC and GND on an Arduino UNO R4, and leave it connected for some time.

Wiring Diagram

The MQ2 gas sensor module has two outputs. You can use one or both, depending on your needs.

The wiring diagram between Arduino UNO R4 MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code - Read value from DO pin

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

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the Arduino Uno R4 board to the gas sensor according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
  • Place the MQ2 gas sensor close to the smoke or gas you wish 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 that if the LED light stays on all the time or does not turn on at all, you can turn the small knob (potentiometer) to adjust the sensor's sensitivity.

Arduino UNO R4 Code - Read value from AO pin

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-gas-sensor */ #define AO_PIN A0 // The Arduino UNO R4 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 Arduino IDE
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4
  • Put the MQ2 gas sensor close to the smoke or gas that needs to be detected
  • Check 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: 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  

Based on the values from DO or AO, you can determine the air quality according to your standards, or activate an alarm or start the 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!