This guide shows how to build a smoke detector that beeps when it detects flammable gas or smoke.
The MQ-2 Gas Sensor
The MQ-2 smoke sensor is the one in the following figure:
The MQ-2 smoke sensor is sensitive to smoke and to the following flammable gases:
- LPG
- Butane
- Propane
- Methane
- Alcohol
- Hydrogen
The resistance of the sensor is different depending on the type of the gas.
The smoke sensor has a built-in potentiometer that allows you to adjust the sensor digital output (D0) threshold. This threshold sets the value above which the digital pin will output a HIGH signal.
How does it work?
The voltage that the sensor outputs changes accordingly to the smoke/gas level that exists in the atmosphere. The sensor outputs a voltage that is proportional to the concentration of smoke/gas.
In other words, the relationship between voltage and gas concentration is the following:
- The greater the gas concentration, the greater the output voltage
- The lower the gas concentration, the lower the output voltage
The output can be an analog signal (A0) that can be read with an analog input of the Arduino or a digital output (D0) that can be read with a digital input of the Arduino.
Pin Wiring
The MQ-2 sensor has 4 pins.
Pin | Wiring to Arduino Uno |
A0 | Analog pins |
D0 | Digital pins |
GND | GND |
VCC | 5V |
Example: Gas Sensor with Arduino
In this example, you will read the sensor analog output voltage and when the smoke reaches a certain level, it will make sound a buzzer and a red LED will turn on.
When the output voltage is below that level, a green LED will be on.
Parts needed:
So, for this example, you’ll need:
- 1 x MQ-2 gas sensor
- Arduino UNO – read Best Arduino Starter Kits
- 1x Breadboard
- 1 x red LED
- 1 x green LED
- 1 x buzzer
- 3 x 220Ω resistor
- Jumper wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Schematics
Follow these schematics to complete the project:
Code
Upload the following sketch to your Arduino board (feel free to adjust the variable sensorThres with a different threshold value):
/*******
All the resources for this project:
https://randomnerdtutorials.com/
*******/
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
// Your threshold value
int sensorThres = 400;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
Serial.begin(9600);
}
void loop() {
int analogSensor = analogRead(smokeA0);
Serial.print("Pin A0: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzer);
}
delay(100);
}
Video demonstration
Watch this quick video demonstration to see this project in action:
I hope you found this guide useful.
Share this post with a friend that also likes electronics!
You can contact me by leaving a comment. If you like this post probably you might like my next ones (click here to subscribe my blog).
Thanks for reading,
-Rui Santos
Nice tutorial!
I found that if sensor sensitivity is not well set, it could respond to unexpected gas, even dust…
You really have to adjust the sensitivity. Thanks for your feedback Sam!
can someone send me the same programme which can detect a logic state instead of a densor using proteus ???? thanks
Good job,
i like the project 🙂
Thank you for reading,
Rui
Nice and clear tutorial, thanks, really appreciate it.
Thank you Nigon!
Nice explanation, Rui.
I have bought a lot of those, a while ago + a quite expensive CO2 sensor. The instruction says it would be better if you “burn them in” for about 24 hours. So put the right tension to the heating element for about a day, before using them in a project (only once…)Some heaters use quite some current, so it would be advised to use an external power supply, not the arduino itself.
may I know where is the jumper A0 can connect ? the schematics is not have jumper A0…
The MQ-2 sensor has a pin called A0 that connects to analog pin A5 in the Arduino board
Cadê o código? não achei o link para baixa-lo
Está no post na secção “Code” e podia copiar a partir de lá
The description of the MQ-2 sensor above includes the sentence “The smoke sensor has a built-in potentiometer that allows you to adjust the sensor sensitivity according to how accurate you want to detect gas.”
Please note, that this poti has only an influence on the threshhold of the digital pin.
I know Joachim! Thanks for pointing that out and I’ve updated the blog post to explain that better.
Thank you for your feedback!
It’s really Work, thank’s
Thanks!
Hi. This is a good app. But after the alarm, green led doesn’t work.
It should, are you sure?
Did you place the green LED properly?
Thanks
Rui
How did you get the threshold value?
Hi Nel.
To get the threshold value you need to know the sensor reading when there is no gas on the atmosphere, and set a value sightly above that for the threshold value.
You can also use a lighter to release some gas next to the sensor and see how the values change.
Your threshold value should be between those two values.
Hope this helps.
Hey, thanks a lot, I’ve been wondering how the heck that threshold value was set, great hint.
I don,t have the module. I have the separate mq-2 sensor.
What should be the ideal value of threshold when you are deploying the kit?
Hi.
The threshold value may vary depending on your sensor.
You need to measure the gas levels when there isn’t gas on the atmosphere and then, you can use a lighter to simulate gas on the atmosphere and see how the values change. Your threshold value should be between those two readings (reading without gas and reading with gas)
We can’t point a threshold value that works for everyone.
Regards.
my mq2 is not havinhg an analog output so how should i alter the codes
Hi sammy.
If your sensor doesn’t have an analog output, you need to read the digital output value.
It will be either HIGH or LOW, depending if the gas levels are above or below the threshold, respectively.
You should set the threshold value by using the potentiometer at the back of the sensor.
I hope this helps.
nice tuto.
If i connect a mq2 , buzzer , GSM module , to arduino . can you help with the source code .
Hi. Unfortunately, we can’t help you write custom code.
But we have a tutorial on how to use the GSM module that can help you with your projects.
Take a look here: https://randomnerdtutorials.com/sim900-gsm-gprs-shield-arduino/
I hope this helps.
It seems my MQ2 sensor doesn’t sense anything. Does it sense even the gas leak of a lighter ? Thanks !
Hi.
Yes, it should sense the gas leaking of a lighter if it is almost touching the sensor.
Regards, Sara 🙂
can someone send me the modified programme which detect a logic state from the sensor and can work in proteus ???? thanks
What does the data on the serial monitor represent? Is it supposed to be measured in ppm? If so then isn’t 300 ppm too high for any of those gases?
Hi.
It doesn’t mean ppm!
The MQ-2 gas smoke sensor is an analog sensor. It outputs a voltage between 0 and 5V accordingly to the gas concentration in the atmosphere.
The greater the gas concentration, the greater the output voltage.
The lower the gas concentration, the lower the output voltage.
When the Arduino reads analog values, it returns 1023 when it reads 5V, and it returns 0 with 0V. Any voltage in between will be given the corresponding value.
So, 300 is an analog reading that corresponds to a voltage of approximately 1.5V. This gives you a relative measurement of the gas concentration (not quantitative, unless you calibrate the sensor using know gas concentrations).
I hope this helps.
Regards,
Sara 🙂
Hi!
so if i want to add an lcd to display the concentration of the gas i have to add this formula in the arduino code ‘analogSansor’*1023 /5 is this correct ?
Hi.
Why do you have to divide by 5?
Come on, the code doesn’t even compile.
Hi Damien.
What errors are you getting on the serial monitor?
Regards,
Sara 🙂
I am getting values about 660 and 680 in “clean” air in my room. I think this values are to high?
Regars
Hi Bojan.
It depends on the specific sensor you’re using.
can i use another resistor i dont have 220 kresistor???
Hi Sam.
Yes, you can use other resistor values in the same range. For example: 330, 470 or 520, for example.
Regards,
Sara
Do you have a tutorial on mq2 sensor that senses smoke without using arduino?
Hi.
We just have this tutorial for the MQ-2 gas sensor.
Regards,
Sara
Hi,
can I use the same method for MQ137?
Yes. It should work with the same code.
Regards,
Sara
do you know how to send output from arduino to thingspeak? i have already setup all the components including wifi module.
Hi.
We don’t have any tutorial regarding Arduino with thingspeak.
We have a tutorial about using thingspeak with the ESP8266. It might be useful to figure out how to use it with Arduino: https://randomnerdtutorials.com/esp8266-daily-task-publish-temperature-readings-to-thingspeak/
Regards,
Sara
how about sending output from the sensor via email? can you help me?
Hello Mr Rui Santos
i will make this project
it can be help me
i would ask about the sensor MQ-2
it can be detect gasses on LPG
how diferrent sensor MQ-5 and MQ-2
i will be waiting your answer
thanks
connect sensor data pin to analog pin of the controller, then corresponding to input gases analog readings will change
Can I replace the buzzer with a 5vdc relay ?
This is to be able to connect a louder remote 220vac alarm & red LEDs.
Which sensor is more sensitive to smoke of fire?
MQ-2 or MQ-7 or MQ-135 or ….????
please where did you do the simulation?