Tech Gyan Set
Published © MIT

Smart LPG Gas Cylinder Usage & Leak Safety System for India

A smart system for Indian LPG cylinders that monitors gas usage, predicts refill time, and detects gas leaks to improve kitchen safety.

BeginnerShowcase (no instructions)8 hours250
Smart LPG Gas Cylinder Usage & Leak Safety System for India

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

πŸ”§ Pin Configuration (ESP32)

C/C++
// ===== PIN DEFINITIONS =====
#define GAS_SENSOR_PIN 34      // MQ-2 Analog OUT
#define BUZZER_PIN 27
#define LED_PIN 26

#define HX711_DT 32
#define HX711_SCK 33

#define SERVO_PIN 13           // Optional

🧠 COMPLETE ESP32 CODE

C/C++
#include <HX711.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>

// ================= OBJECTS =================
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo gasServo;

// ================= CONSTANTS =================
#define GAS_THRESHOLD 1800      // Adjust after testing
#define FULL_CYLINDER_WEIGHT 29000  // grams (Domestic LPG approx)
#define EMPTY_CYLINDER_WEIGHT 15000 // grams

float calibration_factor = -7050; // Adjust for your load cell

// ================= VARIABLES =================
float currentWeight = 0;
float gasUsedToday = 0;
float previousWeight = 0;
int daysLeft = 0;

unsigned long lastDayCheck = 0;
const unsigned long oneDay = 86400000UL;

// ================= SETUP =================
void setup() {
  Serial.begin(115200);

  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);

  lcd.init();
  lcd.backlight();

  scale.begin(HX711_DT, HX711_SCK);
  scale.set_scale(calibration_factor);
  scale.tare();

  gasServo.attach(SERVO_PIN);
  gasServo.write(0); // Gas ON position

  lcd.setCursor(0, 0);
  lcd.print("Smart LPG System");
  lcd.setCursor(0, 1);
  lcd.print("Initializing...");
  delay(3000);
}

// ================= LOOP =================
void loop() {
  readWeight();
  detectGasLeak();
  calculatePrediction();
  displayData();
  delay(1000);
}

// ================= FUNCTIONS =================

// ---- Read Cylinder Weight ----
void readWeight() {
  if (scale.is_ready()) {
    currentWeight = scale.get_units(5) * 1000; // kg to grams
    if (currentWeight < EMPTY_CYLINDER_WEIGHT) {
      currentWeight = EMPTY_CYLINDER_WEIGHT;
    }
  }
}

// ---- Gas Leakage Detection ----
void detectGasLeak() {
  int gasValue = analogRead(GAS_SENSOR_PIN);

  if (gasValue > GAS_THRESHOLD) {
    digitalWrite(BUZZER_PIN, HIGH);
    digitalWrite(LED_PIN, HIGH);
    gasServo.write(90); // Close gas knob

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("!! GAS LEAK !!");
    lcd.setCursor(0, 1);
    lcd.print("Valve OFF");

    Serial.println("GAS LEAK DETECTED!");
    delay(2000);
  } else {
    digitalWrite(BUZZER_PIN, LOW);
    digitalWrite(LED_PIN, LOW);
    gasServo.write(0); // Keep gas ON
  }
}

// ---- Gas Usage Prediction ----
void calculatePrediction() {
  if (millis() - lastDayCheck > oneDay) {
    gasUsedToday = previousWeight - currentWeight;
    previousWeight = currentWeight;
    lastDayCheck = millis();
  }

  float remainingGas = currentWeight - EMPTY_CYLINDER_WEIGHT;
  float avgDailyUsage = gasUsedToday > 0 ? gasUsedToday : 500;

  daysLeft = remainingGas / avgDailyUsage;
  if (daysLeft < 0) daysLeft = 0;
}

// ---- Display on LCD ----
void displayData() {
  int gasPercent = map(currentWeight, EMPTY_CYLINDER_WEIGHT, FULL_CYLINDER_WEIGHT, 0, 100);
  gasPercent = constrain(gasPercent, 0, 100);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Gas: ");
  lcd.print(gasPercent);
  lcd.print("%  ");

  lcd.setCursor(0, 1);
  lcd.print("Days Left: ");
  lcd.print(daysLeft);
}

Credits

Tech Gyan Set
9 projects β€’ 1 follower
Thanks to Tech Gyan Set .

Comments