Up to date guide for MQ-2 gas sensor usage with Raspberry Pi 4 model B.

George Soloupis
6 min readApr 22, 2022

Written by George Soloupis ML GDE.

This is an updated guide on how to use MQ-2 gas sensor with Raspberry Pi 4 model B. The whole effort was done to update the tutorial demonstrated here which has valuable information and with that and this guide you can use the gas sensor in a few steps and minimum effort with all the latest software available.

The MQ-2 gas sensor has a high detection scope and is used in gas leakage detecting equipments in family and industry. It is suitable for detecting LPG, i-butane, propane, methane ,alcohol, Hydrogen, smoke.

MQ-2 module.

It is recommend to use a sensor with a soldered PCB, because no further cabling is required and the use of resistors and capacitors is necessary. A simple diagram of the sensor is:

Sensor’s diagram.

All MQ-X sensors return analogue signals, which we can not easily read at the Raspberry Pi. In addition they output a digital signal but for our purpose is not useful as the digital output will give detection or not of the specific gas which it is defined by a threshold of a potensiometer. So the digital output will be on/off depend on a threshold we have set before. In our example the idea is to measure the analog signal based on the consentration of the gas and we will collect the raw value and the corresponded gas concentrations that are derived of the sensor’s diagram.

The construction on the breadboard will be like this:

Breadboard with MQ-2 sensor, a Logic Level Converter and an ADC.

At the above image you can see except from the gas sensor, a Logic Level Converter and an ADC. The output of the gas sensor is 0–5V. 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. You can find more details of the logic converted here. Except of the logic level converter we can use a voltage division setup by using resistors. You can find more info at this post. That way the 0–5V of the sensor’s output are converted to 0–3.3V by using the voltage divider formula:

Voltage divider formula.

R2 must be double the value of the R1 so the Vout to be 2/3 of the Vin and the circuit will be like:

Circuit for voltage division.

The image of such set up will be like:

MQ sensor with voltage division at the analog output.

In the above picture you can see that the sensor’s analog output (Ao) which is 0 to 5 Volts is followed by a resistor of 220Ω (R1) and then a resistor of 440 Ω (R2) that is double the value of the previous. In the middle we get voltage of 0 to 3.3 Volts maximum and we connect it with the MCP3008 ADC.

Another necessary component is the MCP3008 analog-to-digital converter. The MCP3008 is a low cost 8-channel 10-bit analog to digital converter. The precision of this ADC is similar to that of an Arduino Uno, and with 8 channels you can read quite a few analog signals from the Pi. This chip is a great option if you just need to read simple analog signals. The MCP3008 connects to the Raspberry Pi using a SPI serial connection.

MCP3008 adc

Once the chip is in the breadboard then you’re ready to connect it to the Pi. Each of the legs of the MCP3008 chip have the following names:

Names of the MCP3008 legs

To connect the MCP3008 to the Raspberry Pi you need to make the following connections:

  • MCP3008 VDD to Raspberry Pi 3.3V
  • MCP3008 VREF to Raspberry Pi 3.3V
  • MCP3008 AGND to Raspberry Pi GND
  • MCP3008 DGND to Raspberry Pi GND
  • MCP3008 CLK to Raspberry Pi pin 18
  • MCP3008 DOUT to Raspberry Pi pin 23
  • MCP3008 DIN to Raspberry Pi pin 24
  • MCP3008 CS/SHDN to Raspberry Pi pin 25

Except of the wiring you will need a library that has to be installed to take advantage of the MCP3008 usage. The latest software is the one that is uploaded here and you can follow the instructions there. If you need the full MCP3008 specifications you can find it at this pdf. Once you have the library installed and the adc wired up with your Pi then you are ready to read values from your Raspberry.

One last thing to remember is to enable the SPI interface on the Raspberry. SPI, or Serial Peripheral Interface, is used to control components with a master-slave relationship, though it is not as compact. It requires clock (SCLK), Master Out Slave In, and Master In Slave out pins to function. The pins do what their names imply, with SCLK regulating data speed, MOSI used to send orders from the Pi to attached devices, and MISO doing the opposite. Check the pPinout at this diagram. SPI can be enabled with these simple steps (I believe the Method 2 is the easiest one). Raspberry comes with spidev pre installed, but if it is not you can follow the instructions at the Step 3 at the above link to download and install it.

If the SPI is enabled, the MCP3008 is wired and its library is installed and the MQ-2 gas sensor is in place then you can test the sensor with this test file:

import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D5)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create an analog input channel on pin 0
chan = AnalogIn(mcp, MCP.P0)

print('Raw ADC Value: ', chan.value)
print('ADC Voltage: ' + str(chan.voltage) + 'V')

At the above we are assuming that the analog output of the sensor is wired at CH0 pin of the MCP3008.

For usage of the MQ-2 air sensor there must be a calibration step before. After the calibration you can read the values of the sensor with this python script.

You can view the whole code also at the Github repo.

Conclusion

By following the above tutorial you can plug the MQ-2 sensor at the breadboard and the Raspberry, enable the SPI interface, hook up the MCP3008 analog-to-digital converter and read values of the sensor with the latest Circuit Python library. For other MQ-x sensors you can do the same work, alter the file depend of the sensor’s diagram and by wiring it to a different MCP3008 channel you can even read values for more than one sensor at the same time!

--

--

George Soloupis

I am a pharmacist turned android developer and machine learning engineer. Right now I’m a senior android developer at Invisalign, a ML & Android GDE.