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:
- How to connect the MQ2 gas sensor with Raspberry Pi Pico
- How to program Raspberry Pi Pico to read data from the MQ2 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 | × | Breadboard | |
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) |
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.
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.
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code - Read value from DO pin
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.
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
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.
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.