Labs / Bipes + MicroPython / 7 Sensors: Enable Programs to "Perceive the World"

7.11 How to Give Early Warning of Gas Leaks? Teach You to Detect Harmful Gases! ——MQ Series Gas Sensor Modules

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

Young makers, today let's unlock three "electronic noses that can smell odors"👃!

The smoke detectors at home can sense smoke, the traffic police's alcohol testers can detect alcohol, and air purifiers can pick up harmful gases—all of which rely on similar gas-sensitive sensors. The MQ series module we’re getting to know today acts like a development board’s "electronic nose", capable of accurately "sniffing out" smoke, alcohol, and harmful gases. It’s incredibly practical ✨!

1. Module Introduction

The MQ series are all semiconductor gas sensors, each featuring a core made of a semiconductor material sensitive to specific gases. When the target gas comes into contact with the sensor surface, the resistance of the semiconductor changes, and the module converts this change into an electrical signal, letting the development board know "whether the target gas is detected at present".

Model

Test Object

Life Scenarios

MQ-2 Smoke Sensor Module

Combustible gases such as smoke, methane, and liquefied petroleum gas

Smoke alarms and gas leak detectors

MQ-3 Alcohol Sensor Module

Alcohol, Ethanol, Volatile Organic Compounds

Drunk driving detection, breath alcohol test

MQ-135 Air Quality Sensor Module

Formaldehyde, ammonia, benzene, air quality

Indoor air quality monitoring and harmful gas alarm

Their common features:

Two reading modes are available:

In graphical programming, these blocks are commonly used:

  1. Set pin mode block: Set the pin connected to the sensor to "input mode" before you can read the voltage level;
  2. Read Number / ADC Input Block: Number reading checks "whether the value exceeds the threshold", while ADC reading measures "concentration level";
  3. Conditional judgment blocks: for example, "If smoke is detected, trigger the buzzer and LED flashing alarm";
  4. Delay block: Prevents signal jitter to avoid "repeated alarms triggered by minor smoke".

A Quick Science Note: Why Do We Need to Preheat?

Before using the MQ series sensors for the first time, please power them on and preheat for 2 to 3 minutes! The gas-sensitive element needs to reach a stable state to ensure accurate detection results, just like we need to warm up before exercising~

2. Wiring Instructions

To make a sensor "sniff" gas or smoke, first connect the wires correctly!

7.11 How to Give Early Warning of Gas Leaks? Teach You to Detect Harmful Gases! ——MQ Series Gas Sensor Modules

Connect the MQ2 module to the AIN0 interface on the development board, and make sure to memorize the specific pin correspondence:

Development Board Pins

Module Pins

Description

Pin 26

AIN (Analog Pin)

Sensor analog signal input pin

Pin 19

DIN (Digital Input Pin)

Digital signal input pin after digital comparison of sensor analog signals

📌 Note

3. Power-on preheating

Before burning the code, we first need to adjust the MQ series electrochemical gas sensor module the digital output threshold:

7.11 How to Give Early Warning of Gas Leaks? Teach You to Detect Harmful Gases! ——MQ Series Gas Sensor Modules

4. Example: Smoke Detection Triggers Alarm

Experimental Objective: Continuously monitor the surrounding gases. When the target gas is detected (for example, MQ-2 detects smoke, MQ-3 detects alcohol, and MQ-135 detects formaldehyde), print "MQ sensor triggered" on the terminal to intuitively experience the sensitivity of the "electronic nose".

Everyday Life Insight: The smoke alarm in your home beeps when it detects smoke, and this works on a similar principle. Now let's make a "mini gas detector" ourselves!

Taking MQ2 as an example, the core characteristics of this sensor are as follows:

The graphical program is as follows:

7.11 How to Give Early Warning of Gas Leaks? Teach You to Detect Harmful Gases! ——MQ Series Gas Sensor Modules

Building Block Assembly Detection (Step-by-Step Assembly Guide):

  1. Infinite Loop Monitoring
    • Drag in the repeat while true block to keep the program running continuously.
  2. Read digital output status
    • Drag in the if not read digital input block;
    • Selected Pin: Pin 25 / GPIO19;
    • Set Pull-up to false (since the DO outputs a low level when the gas exceeds the threshold, and not is used to identify the trigger state).
  3. Trigger an alarm prompt
    • When the condition is satisfied, add the print "Trigger MQ sensor" block;
    • Add the delay block and set it to 1 second → to avoid frequent printing.

Experimental results (taking MQ-2 as an example):

Graphical blocks will be automatically converted into MicroPython code, and the running result will be displayed:

7.11 How to Give Early Warning of Gas Leaks? Teach You to Detect Harmful Gases! ——MQ Series Gas Sensor Modules

XML files can be directly imported into the building blocks:

MQ2.xml 2 KB

Tips

  1. Is false triggering still occurring after calibration? Turn the adjustable resistor slightly clockwise (to raise the threshold) to reduce sensitivity to environmental interference.
  2. Want to test the concentration level? Replace "read digital input" with "read ADC input (AIN pin required)", and the terminal will print the specific value — the smaller the value, the higher the concentration;
  3. Want to upgrade the audible and visual alarm? Simply replace the print block with "continuous buzzer tone + fast LED flashing" for a more eye-catching early warning~

✨ Interactive Quick Question

  1. Think about it: Why do MQ series sensors require preheating? Will the detection results be more stable after preheating?
  2. Try it: Gently exhale a breath (containing a small amount of alcohol) toward the MQ-3 sensor to see if it triggers an alarm. Then use the MQ-135 to test the air in a newly renovated room to experience the difference in sensitivity.

Summary

  1. The MQ series sensors are semiconductor gas-sensitive modules that can accurately detect smoke, alcohol and harmful gases, serving as the "electronic nose" for development boards.
  2. Several modules feature completely identical wiring and programming, and only differ in the detection target, making them suitable for projects such as fire early warning, drunk driving detection, and air quality monitoring.
  3. The basic usage is to trigger an alarm via "digital output + loop detection", while the advanced usage is to read the analog value to display the gas concentration;
  4. Please pay attention to safety during use. Be sure to preheat the device before the first use to ensure stable test results.