Raspberry Pi - Gas Sensor

This tutorial instructs you how to use Raspberry Pi and the MQ2 gas sensor to assess the quality of the air by measuring the levels of gases like LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×MQ2 Gas Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

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 is capable of detecting the presence of various gases such as LPG, smoke, alcohol, propane, hydrogen, methane, and carbon monoxide in the surrounding environment. It provides two options for output: a digital output pin and an analog output pin.

It's important to note that the MQ2 gas sensor does not provide information about each gas individually. Instead, it provides information about the combination of gases or the presence of gases as a whole.

By using the MQ2 sensor, we can determine if there is a gas leak or if the air quality is poor. This information is valuable in helping us take appropriate actions to ensure our safety, such as triggering an alarm or activating ventilation systems.

Pinout

The MQ2 gas sensor has four pins that serve different purposes:

  • VCC pin: This pin needs to be connected to a 5V power source (VCC).
  • GND pin: This pin needs to be connected to the ground (0V) for proper circuit operation.
  • DO pin: It is a digital output pin that indicates the presence of flammable gases. When the concentration of gases is detected, the pin is set to LOW. Conversely, if no gases are detected, the pin is set to HIGH. The threshold for detecting gas concentration can be adjusted using a built-in potentiometer.
  • AO pin: This is an analog output pin that generates a voltage proportional to the gas concentration. As the gas concentration increases, the voltage on this pin also rises. Similarly, when the gas concentration decreases, the voltage decreases accordingly.

In summary, the VCC and GND pins provide power and grounding, while the DO pin gives a digital indication of gas presence, and the AO pin generates an analog voltage that corresponds to the gas concentration.

MQ2 Gas Sensor Pinout

Additionally, the MQ2 gas sensor is equipped with two LED indicators:

  • PWR-LED indicator: This LED serves as a power indicator, illuminating to show that the sensor is receiving power.
  • DO-LED indicator: This LED is connected to the DO pin and provides a visual indication of the gas concentration. When gas concentration is detected and the DO pin is set to LOW, the DO-LED indicator turns on. Conversely, if no gas concentration is detected and the DO pin is set to HIGH, the DO-LED indicator turns off.

How It Works

Regarding the DO pin:

  • The MQ2 module includes a built-in potentiometer that allows you to adjust the sensitivity or threshold for gas concentration.
  • When the concentration of gases in the surrounding environment surpasses the set threshold value, the output pin of the sensor becomes LOW, and the DO-LED turns on.
  • Conversely, when the concentration of gases in the surrounding environment falls below the threshold value, the output pin of the sensor becomes HIGH, and the DO-LED turns off.

Regarding the AO pin:

  • As the gas concentration increases, the voltage on the AO pin also increases proportionally.
  • Conversely, as the gas concentration decreases, the voltage on the AO pin decreases accordingly.

It is important to note that the potentiometer does not affect the value observed on the AO pin.

The MQ2 Sensor Warm-up

The MQ2 gas sensor requires a warming-up process before it can be used effectively. Here are the details:

  • If the sensor has been stored for a long time (approximately a month or more) and you are using it for the first time, it needs to be warmed up for 24-48 hours. This extended warm-up time ensures accurate readings.
  • However, if the sensor has been recently used, it will only take around 5-10 minutes to reach its fully warmed-up state. During this warm-up period, the sensor may initially provide high readings, but they will gradually decrease until the sensor stabilizes.

To warm up the MQ2 sensor, simply connect its VCC and GND pins to a power supply or connect them to the VCC and GND pins of Raspberry Pi. Then, allow the sensor to remain in this connected state for the required period of time.

Wiring Diagram

Since the MQ2 gas sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • The wiring diagram between Raspberry Pi and the MQ2 gas sensor when using DO only.
The wiring diagram between Raspberry Pi and MQ2 gas sensor

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

Raspberry Pi Code - Read value from DO pin

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file gas_sensor.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-gas-sensor """ import RPi.GPIO as GPIO import time # Set up the GPIO mode GPIO.setmode(GPIO.BCM) # Set up the GPIO pin for reading the DO output DO_PIN = 7 # Replace with the actual GPIO pin number GPIO.setup(DO_PIN, GPIO.IN) try: while True: # Read the state of the DO pin gas_present = GPIO.input(DO_PIN) # Determine if gas is present or not if gas_present == GPIO.LOW: gas_state = "Gas Present" else: gas_state = "No Gas" # Print the gas state print(f"Gas State: {gas_state}") time.sleep(0.5) # Wait for a short period before reading again except KeyboardInterrupt: print("Gas detection stopped by user") finally: # Clean up GPIO settings GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 gas_sensor.py
  • Place the MQ2 gas sensor near the smoke/gas you want to detect
  • See the result on Terminal.
PuTTY - Raspberry Pi
No Gas No Gas No Gas No Gas Gas Present Gas Present Gas Present Gas Present

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Please keep in mind that if you notice the LED status remaining on constantly or off, you can adjust the potentiometer to fine-tune the sensitivity of the sensor.

Raspberry Pi Code - Read value from AO pin

To read the value from the AO pin, you need to use ADS1115 ADC Module since Raspberry Pi does not have any ADC pin. See how to use ADS1115 ADC Module with Raspberry Pi

Video Tutorial

Function References

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!