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:
- How to connect the MQ2 gas sensor to Raspberry Pi
- How to program Raspberry Pi to read value from the MQ2 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 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.
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.
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 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:
- Create a Python script file gas_sensor.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Place the MQ2 gas sensor near the smoke/gas you want to detect
- See the result on Terminal.
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