Guide For MQ-2 Gas Sensor With Arduino - Random Nerd Tutorials
Maybe your like
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); }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
Tag » Arduino Mq2 Sketch
-
Smoke Detection Using MQ-2 Gas Sensor - Arduino Project Hub
-
How To Connect MQ2 Gas Sensor To Arduino
-
MQ-2 Gas Sensor Arduino - Arduino Project Hub
-
How MQ2 Gas/Smoke Sensor Works? & Interface It With Arduino
-
Arduino MQ-2 Gas Sensor Tutorial - Circuit Digest
-
MQ2 Sensor Interfacing With Arduino - Gas And Smoke Detection
-
How To Use MQ2 Gas Sensor - Arduino Tutorial - Instructables
-
[arduino-tutorial] MQ-2 GAS Sensor By silakis - Codebender
-
Arduino Gas Sensor MQ2, Smoke Detector, Programming & Circuit
-
Interfacing MQ2 To Arduino- Gas Sensor For Smoke-Butane-CH4 And ...
-
Interfacing MQ2 To Arduino-MQ2 Gas And Smoke Sensor
-
How To Connect MQ2 Gas Sensor To Arduino - YouTube
-
Arduino Mq2 Sketch Archives - Electronic Clinic