Arduino Gas Leakage Detection and Safety Alert System Using MQ-2 Gas Sensor

Arduino Gas Leakage Detection System Using MQ-2 Sensor | MakeMindz
🔥 Arduino Safety Project

Gas Leakage Detection &
Safety Alert System

Build a smart home/industrial gas monitoring system using Arduino Uno, MQ-2 sensor, relay module, and buzzer — with full circuit diagram, code, and simulation.

⏱ 30 min build ⚡ Beginner–Intermediate 🔧 Arduino Uno 📡 MQ-2 Sensor
💡
This project demonstrates a smart gas leakage detection and safety alert system built with Arduino Uno. It detects LPG, methane, smoke, and other combustible gases and triggers an alarm while controlling connected devices through a relay.

🧰 Components Required

Arduino Uno
MQ-2 Gas Sensor
IR Sensor Module
5V Relay Module
Buzzer (Active)
9V Battery + Clip
Bulb (Demo Load)
Jumper Wires
Breadboard
USB Cable (programming)

🔌 Circuit Diagram

Circuit diagram showing Arduino Uno connected to MQ-2 gas sensor, IR sensor, relay module, buzzer, and load bulb.

📋 Pin Connections / Wiring Guide

⚠️
Always double-check polarity before powering up. Incorrect connections can damage components.
Component Component Pin Arduino Pin Wire Color
MQ-2 Gas SensorAO (Analog Out)A0🟡 Yellow
MQ-2 Gas SensorVCC5V🔴 Red
MQ-2 Gas SensorGNDGND⚫ Black
IR SensorOUTD2🔵 Blue
IR SensorVCC5V🔴 Red
IR SensorGNDGND⚫ Black
Relay ModuleIN (Signal)D3🟠 Orange
Relay ModuleVCC5V🔴 Red
Relay ModuleGNDGND⚫ Black
Buzzer (+)PositiveD5🟣 Purple
Buzzer (-)NegativeGND⚫ Black
9V Battery+VIN🔴 Red
9V Battery-GND⚫ Black

🛠️ Step-by-Step Build Instructions

1

Gather all components

Collect Arduino Uno, MQ-2 gas sensor, IR sensor module, 5V relay module, active buzzer, 9V battery, jumper wires, and a breadboard. Ensure all modules are in working condition.

2

Place components on the breadboard

Lay out the MQ-2 sensor, IR sensor, relay, and buzzer on or near the breadboard. Leave enough space for clean wiring. The relay module can be mounted off-board if controlling AC loads.

3

Connect power rails

Connect Arduino 5V to the breadboard positive rail and GND to the negative rail. All VCC pins of the MQ-2, IR sensor, and relay connect to this 5V rail. All GND pins connect to the GND rail.

4

Wire the MQ-2 Gas Sensor

Connect the MQ-2 AO (analog output) pin to Arduino A0. The DO (digital output) pin is optional for this project. Ensure VCC → 5V and GND → GND are connected.

5

Wire the IR Sensor

Connect IR Sensor OUT pin to Arduino digital pin D2. Connect VCC to 5V and GND to GND. Adjust the potentiometer on the IR module if needed for sensitivity.

6

Connect the Relay Module

Connect relay IN pin to Arduino D3. The relay's NO (Normally Open) and COM terminals are wired in series with your load (bulb). VCC → 5V, GND → GND. If using AC loads, exercise extreme caution or use an optoisolated relay.

7

Connect the Buzzer

Connect the buzzer positive (+) lead to Arduino D5. Connect negative (–) to GND. For an active buzzer, no extra components are needed — it will buzz when D5 goes HIGH.

8

Power the Arduino

Connect a 9V battery to the Arduino via the barrel jack (VIN + GND) or use a USB cable from a PC/charger for testing. The Arduino regulates the voltage to 5V for the components.

9

Upload the code

Open the Arduino IDE, paste the code from the section below, select Board: Arduino Uno and the correct COM port, then click Upload. Open Serial Monitor at 9600 baud to view gas level readings.

10

Test the system

Bring a lighter (unlit, gas only) near the MQ-2 sensor. The Serial Monitor should show rising gas levels. When the value exceeds 300, the buzzer should sound and the relay should click/activate. Adjust the threshold in the code as needed for your environment.

⚙️ How It Works

🌫️
MQ-2 Sensor Monitors Air Continuously The MQ-2 sensor's analog output voltage rises as the concentration of LPG, methane, smoke, or other combustible gases increases in the surrounding air.
📊
Arduino Reads Analog Value Arduino reads the analog voltage from A0 every second (0–1023). A value above 300 is treated as a gas detection event (this threshold can be tuned).
🔊
Buzzer Activates as Alarm When gas is detected, digital pin D5 goes HIGH, activating the active buzzer to produce a continuous audible alarm alert.
🔌
Relay Cuts/Switches the Load Digital pin D3 activates the relay module, which can disconnect a connected appliance (e.g. a gas stove fan, exhaust, or light) to prevent further hazards.
👁️
IR Sensor for Additional Input The IR sensor on D2 can detect motion or object proximity, useful for additional automation logic (e.g. person detection, manual override, or presence-based alerts).
System Resets Automatically When gas levels drop below the threshold, the buzzer and relay deactivate automatically. No manual reset required.

💻 Arduino Code

ℹ️
Copy the code below into Arduino IDE. No external libraries required. Adjust the threshold value 300 based on your MQ-2 sensor's baseline readings in clean air.
Arduino C++ / .ino
/*
  Arduino Gas Leakage Detection and Safety Alert System
  Using MQ-2 Gas Sensor — MakeMindz.com

  Connections:
  - MQ-2 Gas Sensor AO pin  → Arduino A0
  - IR Sensor OUT pin       → Arduino D2
  - Relay IN pin            → Arduino D3
  - Sound Sensor OUT pin    → Arduino D4
  - Buzzer PIN              → Arduino D5
*/

const int gasSensorPin  = A0;  // Analog pin for MQ-2 sensor
const int irSensorPin   = 2;   // Digital pin for IR sensor
const int relayPin       = 3;   // Digital pin for Relay
const int soundSensorPin = 4;   // Digital pin for Sound sensor
const int buzzerPin      = 5;   // Digital pin for Buzzer

void setup() {
  pinMode(gasSensorPin,  INPUT);
  pinMode(irSensorPin,   INPUT);
  pinMode(relayPin,      OUTPUT);
  pinMode(soundSensorPin,INPUT);
  pinMode(buzzerPin,     OUTPUT);

  Serial.begin(9600);
}

void loop() {
  int gasLevel   = analogRead(gasSensorPin);
  int irState    = digitalRead(irSensorPin);
  int soundState = digitalRead(soundSensorPin);

  Serial.print("Gas Level: ");
  Serial.println(gasLevel);

  if (gasLevel > 300) {         // Threshold for gas detection
    digitalWrite(relayPin,  HIGH); // Activate relay
    digitalWrite(buzzerPin, HIGH); // Activate buzzer
    Serial.println("Gas Detected! Alarm Activated.");
  } else {
    digitalWrite(relayPin,  LOW);  // Deactivate relay
    digitalWrite(buzzerPin, LOW);  // Deactivate buzzer
  }

  if (irState == HIGH) {
    Serial.println("IR Sensor Triggered!");
  }

  if (soundState == HIGH) {
    Serial.println("Sound Detected!");
  }

  delay(1000); // Delay for stability
}

🗂️ Diagram.json CirkitDesigner

💡
Copy the JSON below and import it into CirkitDesigner to load the full interactive circuit simulation instantly.
diagram.json — CirkitDesigner Format
{
  "version": 1,
  "author": "MakeMindz",
  "title": "Arduino Gas Leakage Detection and Safety Alert System",
  "description": "MQ-2 gas sensor with relay, buzzer, and IR sensor for gas leak detection",
  "components": [
    {
      "id": "arduino_uno_1",
      "type": "arduino-uno",
      "name": "Arduino Uno",
      "position": { "x": 300, "y": 200 }
    },
    {
      "id": "mq2_1",
      "type": "mq2-gas-sensor",
      "name": "MQ-2 Gas Sensor",
      "position": { "x": 80, "y": 100 }
    },
    {
      "id": "ir_sensor_1",
      "type": "ir-sensor",
      "name": "IR Sensor Module",
      "position": { "x": 80, "y": 280 }
    },
    {
      "id": "relay_1",
      "type": "relay-5v",
      "name": "5V Relay Module",
      "position": { "x": 560, "y": 100 }
    },
    {
      "id": "buzzer_1",
      "type": "buzzer-active",
      "name": "Active Buzzer",
      "position": { "x": 560, "y": 280 }
    },
    {
      "id": "bulb_1",
      "type": "light-bulb",
      "name": "Bulb (Demo Load)",
      "position": { "x": 560, "y": 420 }
    },
    {
      "id": "battery_1",
      "type": "battery-9v",
      "name": "9V Battery",
      "position": { "x": 300, "y": 420 }
    }
  ],
  "wires": [
    { "from": "mq2_1.AO",        "to": "arduino_uno_1.A0",  "color": "yellow"  },
    { "from": "mq2_1.VCC",       "to": "arduino_uno_1.5V",  "color": "red"     },
    { "from": "mq2_1.GND",       "to": "arduino_uno_1.GND", "color": "black"   },
    { "from": "ir_sensor_1.OUT",  "to": "arduino_uno_1.D2",  "color": "blue"    },
    { "from": "ir_sensor_1.VCC",  "to": "arduino_uno_1.5V",  "color": "red"     },
    { "from": "ir_sensor_1.GND",  "to": "arduino_uno_1.GND", "color": "black"   },
    { "from": "relay_1.IN",       "to": "arduino_uno_1.D3",  "color": "orange"  },
    { "from": "relay_1.VCC",      "to": "arduino_uno_1.5V",  "color": "red"     },
    { "from": "relay_1.GND",      "to": "arduino_uno_1.GND", "color": "black"   },
    { "from": "relay_1.NO",       "to": "bulb_1.positive",   "color": "yellow"  },
    { "from": "buzzer_1.positive","to": "arduino_uno_1.D5",  "color": "purple"  },
    { "from": "buzzer_1.negative","to": "arduino_uno_1.GND", "color": "black"   },
    { "from": "battery_1.positive","to": "arduino_uno_1.VIN", "color": "red"     },
    { "from": "battery_1.negative","to": "arduino_uno_1.GND", "color": "black"   }
  ],
  "code": {
    "file": "gas_leakage_detection.ino",
    "target": "arduino_uno_1"
  }
}

▶️ Try the Simulation

Open the live circuit simulation on CirkitDesigner — run the sketch virtually without any hardware.

Import the diagram.json above into CirkitDesigner, or use the Tinkercad link to build manually.

Key Features & Applications

Real-time Detection

Continuously monitors gas levels every second with instant response when threshold is exceeded.

🔊

Audible Alarm

Active buzzer provides immediate audible alert so people in the vicinity are warned.

🔌

Appliance Control

Relay module can switch off appliances or trigger external safety systems automatically.

🔋

Battery Powered

9V battery operation for portable deployment — no wall outlet required for monitoring.

💰

Low Cost

All components are inexpensive and widely available. Full build cost under ₹500 / $7.

📡

Expandable

Easily extended with GSM, ESP8266/ESP32 WiFi, or LoRa for remote alerts and IoT monitoring.

🏭 Applications

Home kitchen LPG safety
Industrial gas monitoring
LPG cylinder safety
Smart home automation
Fire prevention systems
IoT environmental monitoring
Lab safety systems
Vehicle gas leak detection

Comments

try for free