Gas Detector
This project shows you how to build a gas and smoke detector using an MQ‑2 sensor and an Arduino. The MQ‑2 can identify smoke, LPG, methane, and other gases, making it a useful tool for safety and electronics learning. Follow the steps to wire the components, upload the code, and test your own working detector.
Supplies
This project shows you how to build a gas and smoke detector using an MQ‑2 sensor and an Arduino. The MQ‑2 can identify smoke, LPG, methane, and other gases, making it a useful tool for safety and electronics learning. Follow the steps to wire the components, upload the code, and test your own working detector.
Get the Proper Supplies
- Arduino Uno:
- The main microcontroller board that reads the sensor and runs your code.
- MQ-2 gas sensor module:
- A module with pins typically labeled VCC, GND, DO, AO. It outputs both analog and digital signals.
- Jumper wires:
- To connect the sensor to the Arduino.
- (Optional) LED + resistor (220 Ω):
- To visually indicate gas detection.
- (Optional) Buzzer:
- For an audible alarm when gas is detected.
Understand the MQ-2 Pins
- VCC: Power input (5 V)
- GND: Ground
- AO (Analog Out): Gives a variable voltage proportional to gas concentration
- DO (Digital Out): Goes HIGH/LOW based on a threshold set by the onboard potentiometer
For this tutorial, we’ll mainly use AO with Arduino’s analog input
Wire the Citcut
Connect everything on a breadboard or directly with jumper wires.
MQ-2 → Arduino:
- VCC → 5V on Arduino
- GND → GND on Arduino
- AO → A0 on Arduino (analog input)
Optional LED (alert indicator):
- LED anode (+) → digital pin 8 through a 220 Ω resistor
- LED cathode (−) → GND
Optional buzzer:
- Buzzer + → digital pin 9
- Buzzer − → GND
Write the Code
int Input = A0;
int Buzzer = A1;
int GreenLED = A2;
int RedLED = A3;
int value;
int MAX = 600;
void setup() {
Serial.begin(9600);
pinMode(Input, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(RedLED, OUTPUT);
}
void loop() {
value = analogRead(Input);
Serial.println(value);
if (value >= MAX) {
digitalWrite(GreenLED, LOW);
digitalWrite(RedLED, HIGH);
digitalWrite(Buzzer, HIGH);
delay(500);
} else {
digitalWrite(RedLED, LOW);
digitalWrite(Buzzer, LOW);
digitalWrite(GreenLED, HIGH);
}
}
Schematic