Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino and ESP8266-Based Gas Leakage Detection System with Wi-Fi Alerts

View and Edit Project In Cirkit DesignerImage of Arduino and ESP8266-Based Gas Leakage Detection System with Wi-Fi Alerts

Circuit Documentation

Summary

This circuit is designed to detect gas leakage using an MQ-2 gas sensor and take appropriate actions such as activating a fan, sounding a buzzer, and moving a servo motor. The system also displays the status on a 16x2 I2C LCD and communicates with an ESP8266 NodeMCU for remote monitoring and control via Blynk.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. 5V Relay

    • Description: Electromechanical relay for switching high power devices.
    • Pins: Normally Open, Common terminal, Normally Closed, In, GND, VCC
  3. ESP8266 NodeMCU

    • Description: WiFi-enabled microcontroller for IoT applications.
    • Pins: D0, D1, D2, D3, D4, 3V3, GND, D5, D6, D7, D8, RX, TX, A0, RSV, SD3, SD2, SD1, CMD, SD0, CLK, EN, RST, VIN
  4. Fan

    • Description: 5V DC fan for ventilation.
    • Pins: GND, 5V
  5. 16x2 I2C LCD

    • Description: 16x2 character LCD with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  6. Servo

    • Description: Servo motor for mechanical movement.
    • Pins: GND, VCC, PWM
  7. MQ-2 Gas Sensor

    • Description: Gas sensor for detecting LPG, i-butane, propane, methane, alcohol, hydrogen, and smoke.
    • Pins: GND, VCC, ANALOG, Digital
  8. Buzzer

    • Description: Piezoelectric buzzer for sound alerts.
    • Pins: PIN, GND
  9. Mosfet

    • Description: MOSFET transistor for switching.
    • Pins: Gate, Drain, Source
  10. Resistor

    • Description: 10k Ohm resistor.
    • Pins: pin1, pin2
  11. USB Power

    • Description: USB power supply.
    • Pins: +, -

Wiring Details

Arduino UNO

5V Relay

ESP8266 NodeMCU

Fan

16x2 I2C LCD

Servo

MQ-2 Gas Sensor

Buzzer

Mosfet

Resistor

USB Power

Code Documentation

Arduino UNO Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

// Initialize the LCD with I2C address (usually 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Initialize the servo
Servo servoMotor;

// Pin Definitions
const int gasSensorPin = A0;  // MQ2 Gas Sensor (Analog)
const int fanPin = 10;        // Fan
const int buzzerPin = 8;      // Buzzer
const int servoPin = 9;       // Servo Motor
const int gasThreshold = 400; // Gas detection threshold

unsigned long lastReadingTime = 0;    // Last gas sensor reading time
const unsigned long readingInterval = 1000; // Time interval for readings (ms)

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.print("System Starting...");
  delay(2000);
  lcd.clear();

  servoMotor.attach(servoPin);
  servoMotor.write(0); // Servo in safe position

  pinMode(gasSensorPin, INPUT);
  pinMode(fanPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  Serial.begin(9600); // For communication with NodeMCU
}

void loop() {
  unsigned long currentTime = millis();
  
  if (currentTime - lastReadingTime >= readingInterval) {
    lastReadingTime = currentTime;

    int gasValue = analogRead(gasSensorPin);
    String currentStatus;

    if (gasValue > gasThreshold) {
      digitalWrite(fanPin, HIGH);
      digitalWrite(buzzerPin, HIGH);
      servoMotor.write(120);
      currentStatus = "LEAKAGE DETECTED";
    } else {
      digitalWrite(fanPin, LOW);
      digitalWrite(buzzerPin, LOW);
      servoMotor.write(0);
      currentStatus = "SAFE TO USE";
    }

    // Update LCD
    lcd.setCursor(0, 0);
    lcd.print("Gas Value: ");
    lcd.print(gasValue);
    lcd.print("     ");
    lcd.setCursor(0, 1);
    lcd.print(currentStatus + "     ");

    // Send data to NodeMCU
    Serial.print(gasValue); // Gas value
    Serial.print(",");
    Serial.println(currentStatus); // Status
  }
}

ESP8266 NodeMCU Code

// Blynk credentials (Replace with actual values from Blynk IoT setup)
#define BLYNK_TEMPLATE_ID "TMPL3lOb1YZbs"
#define BLYNK_TEMPLATE_NAME "Detection"
#define BLYNK_AUTH_TOKEN "ppUqmtQehDPtfaiWC6JcgOFEWd5AWXrG"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Blynk Auth Token, WiFi credentials
char auth[] = "ppUqmtQehDPtfaiWC6JcgOFEWd5AWXrG";
char ssid[] = "Raj";
char pass[] = "123465789";

// Pin definitions
#define RELAY_PIN D4 // Relay connected to D4

int gasStatus = 0; // Variable to store data from Arduino
String lastStatus = "UNKNOWN"; // Track last gas status to avoid duplicate notifications

BLYNK_WRITE(V0) { // Blynk button widget to control relay (Virtual Pin V0)
  int relayControl = param.asInt();
  digitalWrite(RELAY_PIN, !relayControl); // Invert logic