Description
This Gas Sensor(MQ2) module is useful for gas leakage detection (in home and industry). It is suitable for detecting H2, LPG, CH4, CO, Alcohol, Smoke or Propane. Due to its high sensitivity and fast response time, measurements can be taken as soon as possible.
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.
The MQ-2 sensor has 4 pins.
Pin | Wiring to Arduino Uno |
A0 | Analog pins |
D0 | Digital pins |
GND | GND |
VCC | 5V |
Getting started with the Smoke, Methane, Butane, LPG Sensor ( MQ-2 )
In this example, you will read the sensor analog output voltage and when the smoke reaches a certain level, It also prints a message on serial monitor when smoke is detected.
Hardware required
Connecting the Hardware
Connecting the MQ2 Gas sensor module to the Arduino is pretty easy. Start by placing the sensor on to your breadboard. Connect VCC pin to the 5V pin on the Arduino and connect GND pin to the Ground pin on the Arduino.
Connect D0 output pin on the module to Digital pin#8 on the Arduino and A0 output pin on the module to Analog pin#0 on the Arduino.
When you’re done you should have something that looks similar to the illustration shown below.
Code
The code is very simple & basically just keeps reading analog voltage on A0 pin. It also prints a message on serial monitor when smoke is detected. Try the sketch out, before we begin its detailed breakdown.
#define MQ2pin (0)
float sensorValue; //variable to store sensor value
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
Serial.println(“Gas sensor warming up!”);
delay(20000); // allow the MQ-6 to warm up
}
void loop()
{
sensorValue = analogRead(MQ2pin); // read analog input pin 0
Serial.print(“Sensor Value: “);
Serial.print(sensorValue);
if(sensorValue > 300)
{
Serial.print(” | Smoke detected!”);
}
Serial.println(“”);
delay(2000); // wait 2s for next reading
}
The output on the serial monitor looks like: