MQ-2 Gas Sensor - Educational
MQ-2 Gas Sensor - Educational
The MQ 2 gas sensor is used to detect or monitor the concentration and/or presence of
combustible gases in the air. It features a simple drive circuit, stable, long life, fast response, and
a wide scope.1 Due to its high sensitivity to hydrogen, LPG (liquid petroleum gas), methane,
carbon monoxide, alcohol, smoke, and propane; this sensor is traditionally used to help detect
gas leaks in many family and industrial practices. This walkthrough will describe how the MQ 2
sensor functions and how to set up the sensor for use on a Raspberry Pi.
1. Why is the MQ-2 sensor sensitive to LPG, methane, carbon monoxide, alcohol, smoke,
and propane?
3. Can the MQ-2 sensor tell the difference between LPG, methane, carbon monoxide,
alcohol, smoke, and propane?
Connecting the MQ 2
A0
GND
VCC
MQ-2
5. How many data streams can be connected on the MCP3008 and CYT1076 sensors?
Coding for the MQ-2 sensor depends entirely on how it’s going to be used. To use it for
detection only requires a very simple code that will not be covered here, but can be found at Last
Minute Engineers for the Arduino.3 This walk-through will cover how to code the MQ-2 to
calculate ppm concentrations of gas using a fresh air calibration.
The concentration (ppm) of gases is calculated based of the resistance ratio (RS/R0). RS
is the measured change in resistance when the sensing mechanism detects gas and R0 is the
stable sensor resistance in fresh air or no gas presence. Using Ohm’s law and the sensor
𝑉𝐶−𝑅𝐿
schematic, 𝑅𝑆 = 𝑉𝑜𝑢𝑡 − 𝑅𝐿. VC is the voltage current (in this case 5V from the pi), Vout is
the output voltage (measured analog/digital value), and RL is the load resistance (this set up is at
10K). R0 can then be calculated using this equation, R0 = RS/Fresh air ratio value from
datasheet.
In order to convert the digital signal to concentration units, the datasheet chart is used
again. A simple calibration line has y = mx + b, however, the MQ-2 gas sensor is not linear. It
follows a log-log scale so a bit more calculation is needed. So the y = mx + b equation can be
converted to log(y) = m*log(x) + b. Now using the chart, the slope and intercept can be
𝑦
𝑙𝑜𝑔( ⁄𝑦0)
calculated, where 𝑚 = and 𝑏 = log(𝑦) − 𝑚 ∗ log(𝑥). Once these values are obtained,
𝑙𝑜𝑔(𝑥⁄𝑥0)
the concentration of gas can now be calculated as
[log(𝑦)−𝑏]⁄
𝑥(𝑝𝑝𝑚) = 10 𝑚. (Remember that y is
equal to RS/R0.
7. Calculate the concentration of those gases if the RS/R0 value is equal to 1, 6, and 10.
Once all the parameters have been calculated, coding for the sensor can begin. The first part will
be to code for the fresh air calibration to obtain the RS and R0 value in clean air. The second
part of the code will detect the presence of gas and output a ppm concentration reading. Coding
is as follows in the Thonny IDE:
These are the libraries needed to run the MCP3008 and to calculate the ppm concentrations of
the gas. (These libraries should already be installed on Raspberry Pi when purchased).
In order to obtain the digital output values, the correct location must be set in the code. The MQ-
2 sensor was connected to the first channel on the MCP3008, which is set up as channel 0.
The sensorValue is the digital signal value coming from the MQ-2 sensor readings. In order to
calibrate in fresh air and average of 500 readings is taken. The initial value starts at 0 to allow
for addition of each reading to one another.
This part of the code averages the readings and converts to the RS value in air. From this voltage
we can calculate the R0 in fresh air.
Using the R0 value calculated from above, this part of the code measures in real time for gas.
Once the ratio is obtained, the concentration of gas can be calculated.
To see the values, simply use a print statement. The sensor can cycle as many times as desired.
Homework:
Create a code to allow three MQ-2 sensors to run simultaneously and have them calibrated for
three different gases. Have the sensors take a measurement every 10 minutes for 6 hours and
save the ppm results to a spreadsheet. Test your sensors with a lighter to see if there is an
increase in any of the gases.
References
(1) Arduino Playground - MQGasSensors https://playground.arduino.cc/Main/MQGasSensors
(accessed Feb 7, 2019).
(2) Chemiresistor. Wikipedia; 2019.
(3) lmewp1908. In-Depth: How MQ2 Gas/Smoke Sensor Works? & Interface It with Arduino.
Last Minute Engineers, 2018.
(4) Is it Bakelite? https://www.realorrepro.com/article/Is-it-Bakelite (accessed Feb 6, 2019).
(5) EngineersGarage. Insight - Learn the Working of a Gas Sensor
https://www.engineersgarage.com/insight/how-gas-sensor-works (accessed Feb 6, 2019).