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.
- Connecting the gas sensor to the Arduino Nano 33 IoT
- Writing code for the Arduino Nano 33 IoT to read data 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 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.

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.

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.

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
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.
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
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.
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.