Raspberry Pi Pico - Gas Sensor

This tutorial instructs you how to monitor air quality using a Raspberry Pi Pico and MQ2 gas sensor. This allows to measure levels of various flammable gases such as LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide. In detail, we will learn:

Raspberry Pi Pico gas sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×MQ2 Gas Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 identify various gases like LPG, smoke, alcohol, propane, hydrogen, methane, and carbon in the area. It has a digital output pin and an analog output pin for sending signals.

The MQ2 gas sensor doesn't provide specific details for individual gases. It only gives information about a mixture of gases or if different gases are present at the same time.

We can use the MQ2 sensor to detect gas leaks or check if the air quality is bad. This allows us to respond properly, like triggering an alarm or turning on air systems.

Pinout

The MQ2 gas sensor comes with 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 displays LOW when it detects flammable gases and HIGH when no gases are detected. You can change the detection level using a small adjustable part.
  • AO pin: This is an analog output pin. It gives out a voltage that varies with the gas quantity. More gas increases the voltage, less gas decreases it.
MQ2 Gas Sensor Pinout

It has two LED lights as well.

  • One PWR-LED light indicates the power is on.
  • One DO-LED light displays the gas levels according to the DO pin value: it lights up if gas is present and turns off if there is no gas.

How It Works

Regarding the DO pin:

  • The module has a knob to change how sensitive it is to gas levels.
  • When the gas level is higher than what is set, the sensor's output pin goes LOW and the DO-LED light turns on.
  • When the np gas level is lower than what is set, the sensor's output pin goes HIGH and the DO-LED light turns off.

For the AO pin:

  • When there is more gas, the voltage increases.
  • When there is less gas, the voltage decreases.

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

The MQ2 Sensor Warm-up

The MQ2 gas sensor needs to be warmed up before using it.

  • If you haven't used the sensor for more than a month, please let it warm up for 24-48 hours before using it to ensure accurate results. If you used the sensor recently, it only needs 5-10 minutes to warm up. Initially, the readings may be high, but they will decrease and stabilize soon.

To warm up the MQ2 sensor, connect its VCC and GND pins to a power source or to the VCC and GND on a Raspberry Pi Pico, and let it stay connected for a while.

Wiring Diagram

The MQ2 gas sensor module provides two outputs. You can use one or both, based on your requirements.

The wiring diagram between Raspberry Pi and Pico MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Read value from DO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-gas-sensor """ from machine import Pin import utime # For timing functions DO_PIN = Pin(28, Pin.IN) # The Raspberry Pi Pico pin GPIO28 connected to the DO pin of the MQ2 gas sensor module while True: gas_state = DO_PIN.value() # Read the digital value from the pin if gas_state == 1: print("The gas is NOT present") else: print("The gas is present") utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the MQ2 gas sensor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Put the MQ2 gas sensor near the smoke or gas that needs detection.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If the LED light remains on always or doesn't turn on, you can adjust the sensor's sensitivity by turning the small knob.

Raspberry Pi Pico Code - Read value from AO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-gas-sensor """ from machine import ADC, Pin import utime # For timing functions # Define the Raspberry Pi Pico pin pin connected to the AO pin of the MQ2 gas sensor module AO_PIN = ADC(Pin(26)) # GPIO26 (ADC0) as an analog input pin while True: gas_value = AO_PIN.read_u16() # Read the analog value (0-65535) print(gas_value) # Print the analog value utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your Raspberry Pi Pico.
  • Click the green Run button (or press F5) to execute the script.
  • Position the MQ2 gas sensor near the smoke or gas you want to detect.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

You can use the values from DO or AO to check if the air quality meets your standards, to trigger an alarm, or to turn on 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!