Configure and read out the Raspberry Pi gas sensor (MQ-X)
Sensors are necessary to detect the components of the air. These are used e.g. in smoke detectors. However, instructions for using these gas sensors at the Raspberry Pi are rare, which is why in this tutorial the general use of such MQ modules at the Raspberry Pi is shown. Thus, e.g. smoke detectors or air quality testers can be built.
How to configure any MQ sensor and read it out with the Pi will be shown on the example of the Raspberry Pi gas sensor MQ2 in this tutorial. All other sensors (MQ3, MQ-135, etc.) can also be adapted with a few additional steps.
All MQ-X sensors return analogue signals, which we can not easily read at the Raspberry Pi. One possibility would be to use an Arduino, but we can also use an analog-to-digital converter (ADC), which can be read out via the I2C bus. In addition, we also need a logic level converter.
- Analog-Digital Converter (8 Ports): US / UK
- 5V to 3.3V Logic Level Converter: US / UK
- Breadboard: US / UK
- Jumper wire: US / UK
These components are independent of the selected gas sensor. There are also many different sensors for the Raspberry Pi, which are already available for a few bucks and are suitable for different gases:
- MQ-2 (Methane, Butane, LPG, smoke): US / UK
- MQ-3 (Alcohol, Ethanol, smoke): US / UK
- MQ-4 (Methane, CNG Gas): US / UK
- MQ-5 (Natural gas, LPG): US / UK
- MQ-6 (LPG, butane gas): US / UK
- MQ-7 (Carbon Monoxide): US / UK
- MQ-8 (Hydrogen Gas): US / UK
- MQ-9 (Carbon Monoxide, flammable gasses): US / UK
- MQ-131 (Ozone): US / UK
- MQ-135 (Benzene, Alcohol, smoke): US / UK
- MQ-136 (Hydrogen Sulfide gas): US / UK
- MQ-137 (Ammonia): US / UK
- MQ-138 (Benzene, Toluene, Alcohol, Acetone, Propane, Formaldehyde gas): US / UK
- MQ-214 (Methane, Natural gas): US / UK
- MQ-216 (Natural gas, Coal gas): US / UK
- MQ-303A (Alcohol, Ethanol, smoke): US / UK
- MQ-306A (LPG, butane gas): US / UK
- MQ-307A (Carbon Monoxide): US / UK
- MQ-309A (Carbon Monoxide, flammable gasses): US / UK
- MG811 (Carbon Dioxide (CO2)): US / UK
- AQ-104 (air quality): US / UK
- AQ-2 (Flamable gasses, smoke): US / UK
- AQ-3 (Alcohol, Benzine): US / UK
- AQ-7 (Carbon Monoxide): US / UK
You can find the full list with additional information here.
I recommend to use a sensor with a soldered PCB, because no further cabling and the use of resistors and capacitors is necessary.
Details on the individual Raspberry Pi gas sensors can also be found in the corresponding data sheets. Simply google the name of the sensor including “datasheet”. There is also the voltage at which the sensor operates mentioned.
If someone wants to build an alcohol tester or something similar, you should also be aware that these modules are not absolutely accurate and can not compete with a professional measurement.
Connection between MQ-2 and Raspberry Pi
In this example, we use a 5V voltage as output. This is too much for the GPIOs, which is why we use a logic level converter (TTL) that cuts down the voltage. If you use a sensor other than the MQ-2 and it has a different voltage, the setup must of course be adjusted.
After the MCP3008 is correctly connected, we use port 0 and connect it to RX0 of the TTL. On the opposite side is RX1, which is connected to the analog pin (A0) of the MQ2 sensor. Also connect 3.3V from the Raspberry Pi (LV) and 5V (HV) to the TTL. And also 5V to the VCC pin of the gas sensor and GND from the Raspberry Pi comes to GND on the LV and HV side of the TTL, as well as to GND of the MQ2.
Schematically the whole looks as follows:
I use the 5V of the Raspberry Pi’s. However, an external power supply is recommended if other sensors and modules or input devices (keyboard, mouse, touchscreen) are used. For this, the sensor is simply supplied with current from the external source (HV side of the TTL) and the ground connection (Minus / GND) is connected to GND of the Raspberry Pi.
Configuration of the Raspberry Pi Gas Sensor – Preparation
The concentration of a gas is given in PPM (parts per million). One difficulty of the MQ-2 is that a single analog value is given with which the gas content in the air has to be calculated for the various supported gases. However, the sensor must be configured for this purpose. Since this manual is also applicable to another Raspberry Pi gas sensor, the procedure is as follows:
First, we have to see the data sheet of the respective module, which contains a diagram:
However, the scaling of the values is not linear but logarithmic to the base 10 (log). so, the first stroke on the X axis is 200, then 300, etc. The first stroke after 1000 is 2000, etc. The distance between is linear. The idea behind this script for calibration and reading is to create a straight line and calculate the amount of gas (in ppm). To do this, we need two points to calculate the slope.
Let us take the example of LPG. We therefore take the point P1 (x = 200, y = ~ 1.62) and P2 (x = 10000, y = ~ 0.26). To calculate the “real” values, we apply the ten logarithm. Using the two-point form, we can calculate the slope, which in our case is -0.47 (link to the calculation). With the slope and the calculated logarithm from the left point (x = 2.3, y = 0.21), we can now determine the straight line.
For the remaining gases the calculation is equivalent and can be carried out in the same way. Anyone who wants to have more reading material can do this here.
Calibration of the Raspberry Pi Gas Sensor – Code
Enough of the theory – we want to use the sensor now. For this purpose you can use the code I have customized, which is located in a GitHub repository. Also included is a class for reading the MCP3008. First we clone the directory:
git clone https://github.com/tutRPi/Raspberry-Pi-Gas-Sensor-MQ
Then we change to the directory and run the existing Python test file.
cd Raspberry-Pi-Gas-Sensor-MQ sudo python example.py
The calibration is started automatically during initialization. It is important that the sensor is in good / fresh air as smoke / other gases would falsify the calibration. The process takes a few seconds, but the gas content can already be measured thereafter (see video). Some sensors are quite hot, but this should not be a cause for concern.
Some users reported that the values become accurate only after some time. I can not verify this, because I do not have a professional measuring device. If someone can confirm or verify, since he has such a device at home, I would be pleased about feedback as a comment 🙂