0% found this document useful (0 votes)
2 views5 pages

code

The document outlines an Arduino-based gas leakage detection system using components like gas sensors, an LCD, and an ESP8266 WiFi module. It includes code for setting up the system, monitoring gas levels, activating alarms, and sending alerts when a gas leak is detected. The system also features a mute button to silence alarms during false alerts.

Uploaded by

srushtip2929
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

code

The document outlines an Arduino-based gas leakage detection system using components like gas sensors, an LCD, and an ESP8266 WiFi module. It includes code for setting up the system, monitoring gas levels, activating alarms, and sending alerts when a gas leak is detected. The system also features a mute button to silence alarms during false alerts.

Uploaded by

srushtip2929
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/*

Arduino Based Gas Leakage Detection System

Components: Gas Sensor (MQ-2/MQ-6), LCD 16x2, ESP8266 WiFi Module,

Alarm LED, Buzzer/Relay (via transistor), Push Button

*/

#include <SoftwareSerial.h>

#include <LiquidCrystal.h>

// ---------- Pin Definitions ----------

#define GAS_SENSOR_PIN A0

#define ALARM_LED 6

#define ALARM_BUZZER 5

#define MUTE_BUTTON A1

// Gas concentration threshold (adjust based on sensor calibration)

#define GAS_THRESHOLD 400

// ESP8266 communication (SoftwareSerial)

SoftwareSerial espSerial(2, 3); // RX, TX

// LCD pin connections: RS, EN, D4, D5, D6, D7

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// ---------- State Variables ----------

bool alarmActive = false;

bool alarmMuted = false;

bool alertSent = false;


void setup() {

[Link](9600); // Debug serial

[Link](9600); // ESP8266 serial

pinMode(GAS_SENSOR_PIN, INPUT);

pinMode(ALARM_LED, OUTPUT);

pinMode(ALARM_BUZZER, OUTPUT);

pinMode(MUTE_BUTTON, INPUT_PULLUP);

digitalWrite(ALARM_LED, LOW);

digitalWrite(ALARM_BUZZER, LOW);

[Link](16, 2);

[Link]("Gas Leak Detect");

[Link](0, 1);

[Link]("Sensor warming..");

initESP8266();

delay(20000); // Allow gas sensor to warm up (20s, increase for MQ-


series if needed)

[Link]();

void loop() {

int gasValue = analogRead(GAS_SENSOR_PIN);

[Link]("Gas Level: ");

[Link](gasValue);
checkMuteButton();

if (gasValue > GAS_THRESHOLD) {

handleGasLeak(gasValue);

} else {

handleNormalState(gasValue);

delay(500);

// ---------- ESP8266 Initialization ----------

void initESP8266() {

[Link]("AT");

delay(500);

[Link]("AT+CWMODE=1");

delay(500);

[Link]("AT+CWJAP=\"YOUR_SSID\",\"YOUR_PASSWORD\"");

delay(3000);

// ---------- Gas Leak Detected ----------

void handleGasLeak(int gasValue) {

alarmActive = true;

[Link](0, 0);

[Link]("GAS LEAK ");

[Link](0, 1);

[Link]("DETECTED! Val:");
[Link](gasValue);

digitalWrite(ALARM_LED, HIGH);

if (!alarmMuted) {

digitalWrite(ALARM_BUZZER, HIGH);

// Send alert to ESP8266 -> Cloud/App once per leak event

if (!alertSent) {

[Link]("ALERT,GAS_LEAK,VALUE=");

[Link](gasValue);

alertSent = true;

// ---------- Normal / Safe State ----------

void handleNormalState(int gasValue) {

if (alarmActive) {

// Reset alarm once gas level returns to safe range

alarmActive = false;

alarmMuted = false;

alertSent = false;

[Link]("STATUS,NORMAL");

digitalWrite(ALARM_LED, LOW);

digitalWrite(ALARM_BUZZER, LOW);
[Link](0, 0);

[Link]("Air Quality: ");

[Link](0, 1);

[Link]("Normal Val:");

[Link](gasValue);

[Link](" ");

// ---------- Mute Button Handling ----------

void checkMuteButton() {

if (digitalRead(MUTE_BUTTON) == LOW) {

delay(200); // debounce

if (alarmActive) {

alarmMuted = true;

digitalWrite(ALARM_BUZZER, LOW);

You might also like