Guide for MQ-2 Gas/Smoke Sensor with Arduino

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:

smoke sensor r

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.

smoke sensor label

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 voltagesmokepicture

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:

DSC08397

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:

circuit

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);
}

View raw code

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



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

56 thoughts on “Guide for MQ-2 Gas/Smoke Sensor with Arduino”

  1. 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.

    Reply
    • I wondered about the power consumption from the arduino myself. These are quite power hungry. My device said to burn in as well.
      These are not for battery operated systems.

      Reply
  2. 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.

    Reply
    • 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.

      Reply
    • 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.

      Reply
    • 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.

      Reply
  3. can someone send me the modified programme which detect a logic state from the sensor and can work in proteus ???? thanks

    Reply
  4. 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?

    Reply
    • 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 🙂

      Reply
    • Hi Sam.
      Yes, you can use other resistor values in the same range. For example: 330, 470 or 520, for example.
      Regards,
      Sara

      Reply
  5. do you know how to send output from arduino to thingspeak? i have already setup all the components including wifi module.

    Reply
  6. 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

    Reply

Leave a Reply to emilo Cancel reply

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.