Design a smoke detector through a raspberry pi board and MQ-2 smoke sensor


Design a smoke detector through a raspberry pi board and MQ-2 smoke sensor

This tutorial is to design a smoke detector through a raspberry pi board and MQ-2 smoke sensor.It can detect the concentration of the smoke in the environment.The gas-sensing materials the MQ-2 sensor used is SnO2 material,which is lower conductivity in clear air, the conductivity of sensor would go up with the increase of the smoke concentration.The sensor can convert the different conductivity to the correspondent output signal of the smoke concentration.The MQ-2 gas sensor has high sensitivity to liquefied petroleum gas,propane and hydrogen.




Raspberry Pi3 x1
MQ-2 sensor x1
Analog to digital convertor(ADC) x1
Breadboard x1
male to male jumper wire x若干
male to female umper wire x若干


As only the digital signal can be processed by raspberry pi, we need to add a analog to digital converter (ADC) to process the analog signal from MQ-2 sensor.It can detect the smoke in the air according to the valtage value. MCP3008 as ADC chip is very common and recommended highly.

The MQ-2 sensor can output the analog and digital signal when the work voltage is 2.5V-5V. When the CO gas is detected by sensor, the signal light turns on and the digital signal output the low-level valtage. In additon, there is a adjustable resistor to change the sensor sensitivity.
The schematic diagram is as follows:

Pay more attention to the anode and cathode, otherwise it would burnout your raspberry pi board and sensor.You can connect the GND of the sensor to OV, VCC to 3.3V. You could visit the article for your referenc if you want to learn more about the raspberry pi IO port. : How to read Raspberry Pi i/o pin diagram (GPIO pin graph)


You could choose the connect the raspberry pi to monitor, or login in pi via SSH.

1)Write the code

Add new file named under the /home/pi file path via nano editor (the name is arbitrary)

sudo nano

Write the sample code in new file, the code can be obtained by executing shell commands.

sudo wget --no-check-certificate

2)Execute python program

sudo python ./


When the programme start, there is no signal output within 20s.The sensor would preheat 20s firstly,then output after the data remain stable.It is normal for sensor to stay hot slightly.It can produce the smoke using the lighter.When you light the lighter in front of sensor,there is a signal output of “Gas lealage” word and the valtage value, the signal light turns on . If these the phenomenon above doesn’t appear, you could swing clockwise to adjust the resistor until the above phenomenon appear.


About the Author

amber administrator

2 Comments so far

BaltazarPosted on  2:00 am - Aug 19, 2017


Thank you for the instructions. Finally I’ve managed to set up the Pi with the mq-2 sensor. I am happy AF, because it was the third setup that I’ve tried since ever.
It works properly with gas, I tried It with a lighter, and the results were really fine 🙂

However I set on fire some paper , and the sensor did not show anything.
My question is that, this program is designed for smoke as well, or am I dumb, and messed up something? 🙂

BTW my project would be check and log and trigger some actions (with timestamp) if my neighbors smoke on the balcony , since the vent system is push all their second hand smoke to my apartment. I am a non technical guy so I don’t know if this stuff could check that small amount of smoke, but I really want to do something for sake of my family’s health.

singleWingAcademyPosted on  1:36 pm - Jan 10, 2021

This kit is a ton of fun. Someone gave it to me as a gift, and I used your kit and code to make a smoke detector that plays Rick Astley’s 1987 hit “Never Gonna Give You Up”
Keep up the good work

You must be logged in to post a comment