Arduino Nano 33 IoT - Gas Sensor

This guide will show you how to use the Arduino Nano 33 IoT and the MQ2 gas sensor to check air quality. You will learn how to measure the amount of different flammable gases, including LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide. We will explain each part in detail.

Arduino Nano 33 IoT MQ2 gas sensorm

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×MQ2 Gas Sensor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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 different gases like LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide nearby. It provides two types of output: a digital output and an analog output.

Keep in mind that the MQ2 gas sensor doesn't show details about each gas separately. Instead, it tells us about the mix of gases or if any gas is present overall.

Using the MQ2 sensor, we can tell if there is a gas leak or if the air is bad. This helps us take the right steps to stay safe, like sounding an alarm or turning on fans.

Pinout

The MQ2 gas sensor has four pins, and each one serves a different purpose.

  • VCC Pin: Connect this pin to the power source (5V).
  • GND Pin: Connect this pin to the ground (0V).
  • DO Pin: This digital output tells you if there is flammable gas nearby. When gas is detected, it shows a LOW signal; when no gas is detected, it shows a HIGH signal. You can adjust the gas detection level with the built-in knob.
  • AO Pin: This analog output gives a voltage that changes with the gas level. When the gas level goes up, the voltage goes up; when the gas level goes down, the voltage goes down.
MQ2 Gas Sensor Pinout

Also, the MQ2 gas sensor has two LED lights:

  • PWR-LED indicator: This light shows that the sensor has power. It turns on when the sensor is working.
  • DO-LED indicator: This light is connected to the sensor’s DO pin. It shows the gas level by the number it receives from the DO pin. When there is gas and the DO pin is LOW, the light turns on. When there is no gas and the DO pin is HIGH, the light stays off.

How It Works

About the DO pin:

  • The MQ2 module has a built-in dial that lets you change how sensitive it is to gas. When there's more gas than the set limit, the sensor's output goes low and the DO LED turns on. When there's less gas than the set limit, the sensor's output goes high and the DO LED turns off.

About the AO pin:

  • When the gas level goes up, the voltage on the AO pin goes up too.
  • When the gas level goes down, the voltage on the AO pin goes down too.

Remember that turning the potentiometer does not change the value on the AO pin.

The MQ2 Sensor Warm-up

The MQ2 gas sensor needs time to warm up before it works well. Here are the details:

  • If you use the sensor for the first time after keeping it stored for a long time (about a month or more), let it warm up for 24-48 hours. This long warm-up period makes sure it works correctly.
  • If the sensor has been used recently, the warm-up time is much shorter. It only needs about 5-10 minutes to warm up completely. When warming up, the sensor might show high readings at first, but these readings will slowly go down until it works steadily.

To heat up the MQ2 sensor, just connect its power (VCC) and ground (GND) pins to a power source or to the matching pins on an Arduino Nano 33 IoT. Leave it connected for the necessary warm-up time.

Wiring Diagram

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

  • Wiring diagram for connecting the Arduino Nano 33 IoT to the MQ2 gas sensor using the USB port for power.
The wiring diagram between Arduino Nano and 33 IoT MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

  • A wiring diagram showing how to connect the Arduino Nano 33 IoT and the MQ2 gas sensor using the Vin power pin.
The wiring diagram between Arduino Nano and 33 IoT air quality

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication:

  • This can affect analog readings, so it is recommended to avoid using these pins with any devices/sensors that relies on ADC.
  • Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do NOT use internal or external pull-down resistors for these pins.

Arduino Nano 33 IoT Code - Read value from DO pin

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-gas-sensor */ #define DO_PIN 2 // The Arduino Nano 33 IoT's pin connected to DO pin of the MQ2 sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano 33 IoT'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

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and open it using the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano 33 IoT.
  • Place the MQ2 gas sensor close to the smoke or gas you want to detect.
  • View the result in 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  

Remember, if the LED stays on or off all the time, you can turn the knob to change how sensitive the sensor is.

Arduino Nano 33 IoT Code - Read value from AO pin

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-gas-sensor */ #define AO_PIN A5 // The Arduino Nano 33 IoT's pin connected to AO pin of the MQ2 sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. 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

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and open it using the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT.
  • Put the MQ2 gas sensor close to the smoke or gas you want to detect.
  • Look at 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: 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  

By reading values from DO or AO, you can see if the air quality meets your standard and then either start the ventilation system or trigger an alarm.

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!