🚨 Smart Gas Leak Detector with NodeMCU & Servo Motor

 

Student Project – IoT Based Gas Cylinder Safety System

This project detects gas leaks using an MQ-2 sensor, alerts the user, and allows remote control of the gas cylinder valve using a servo motor. The system hosts a colorful animated webserver for monitoring and control.




Components Required

ComponentQuantityDescription
NodeMCU ESP82661Wi-Fi enabled microcontroller
MQ-2 Gas Sensor1Detects LPG, Butane, Methane
Servo Motor (SG90)1Rotates valve knob to OFF position
Jumper WiresSeveralFor connections
Breadboard1Prototyping
USB Cable1Power + Programming

Circuit Connections

NodeMCU PinComponent
D0 (GPIO16)Servo Signal (Orange wire)
3.3VMQ-2 VCC
GNDMQ-2 GND & Servo GND
A0MQ-2 Analog Output
Vin (5V)Servo VCC

💻 Arduino Code (NodeMCU)

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

#include <Servo.h>


// Wi-Fi Credentials

const char* ssid = "YOUR_WIFI_NAME";

const char* password = "YOUR_WIFI_PASSWORD";


// Web server on port 80

ESP8266WebServer server(80);


// MQ-2 Sensor

int mq2Pin = A0;

int gasValue = 0;

bool gasLeak = false;


// Servo

Servo gasServo;

int servoPin = D0;


// HTML Page

String webpage = R"rawliteral(

<!DOCTYPE html>

<html>

<head>

  <title>Smart Gas Leak Detector</title>

  <style>

    body { font-family: Arial, sans-serif; text-align: center; 

           background: linear-gradient(to right, #00c6ff, #0072ff); color:white; }

    h1 { font-size: 30px; }

    h2 { font-size: 20px; }

    .cylinder { width: 150px; animation: bounce 2s infinite; }

    @keyframes bounce { 0%,100%{transform:translateY(0);} 50%{transform:translateY(-10px);} }

    .status { margin:20px; padding:10px; border-radius:10px; font-size:18px; }

    .safe { background: #4CAF50; }

    .leak { background: #f44336; animation: blink 1s infinite; }

    @keyframes blink { 0%,100%{opacity:1;} 50%{opacity:0.5;} }

    button { padding:15px 30px; margin:10px; font-size:16px; border:none; border-radius:10px; cursor:pointer; }

    .on { background:#4CAF50; color:white; }

    .off { background:#f44336; color:white; }

  </style>

</head>

<body>

  <h1>Smart Gas Leak Detector</h1>

  <h2>Student Name</h2>

  <img src="https://i.imgur.com/3CSYwQd.png" class="cylinder">

  <div id="status" class="status safe">Gas Status: SAFE</div>

  <div>

    <button class="on" onclick="location.href='/on'">Gas ON</button>

    <button class="off" onclick="location.href='/off'">Gas OFF</button>

  </div>

  <script>

    setInterval(()=>{ fetch('/gas').then(r=>r.text()).then(data=>{

      let st=document.getElementById('status');

      if(data.includes('LEAK')){

        st.innerHTML='Gas Status: LEAK DETECTED!';

        st.className='status leak';

      } else {

        st.innerHTML='Gas Status: SAFE';

        st.className='status safe';

      }

    })}, 2000);

  </script>

</body>

</html>

)rawliteral";


// Handle Root

void handleRoot() {

  server.send(200, "text/html", webpage);

}


// Servo ON

void handleOn() {

  gasServo.write(90); // Open valve

  server.send(200, "text/html", "<h1>Gas Turned ON</h1><a href='/'>Back</a>");

}


// Servo OFF

void handleOff() {

  gasServo.write(0); // Close valve

  server.send(200, "text/html", "<h1>Gas Turned OFF</h1><a href='/'>Back</a>");

}


// Gas Sensor Data

void handleGas() {

  gasValue = analogRead(mq2Pin);

  if (gasValue > 400) { gasLeak = true; server.send(200, "text/plain", "LEAK"); }

  else { gasLeak = false; server.send(200, "text/plain", "SAFE"); }

}


void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  Serial.print("Connecting");

  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

  Serial.println("Connected!");

  Serial.println(WiFi.localIP());


  gasServo.attach(servoPin);

  gasServo.write(90); // Default ON


  server.on("/", handleRoot);

  server.on("/on", handleOn);

  server.on("/off", handleOff);

  server.on("/gas", handleGas);


  server.begin();

}


void loop() {

  server.handleClient();

}


1)Detects LPG / Butane leaks with MQ-2 sensor
2)Webserver with animation (works on PC & Mobile)
 3)Live gas status (SAFE / LEAK DETECTED)
4)Remote ON/OFF gas control using servo motor
5)Colorful UI with bouncing cylinder animation



Comments