/* This is a Gas leakage dectector.
It reads the current gas level, if it exceeds the pre-defined threshold level,
the LED and Buzzer is triggered. In case of any gas leakage, it automatically
shutts the valve of the gas. */
#include <LiquidCrystal.h>
#define GasSensor A0
#define LedPin 13
#define BuzzerPin 7
#define RelayPin 8
// Initialize the leakage as negative
bool gasLeak = false;
// Initialize the LCD with the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
//Set the output pins
pinMode(LedPin, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
pinMode(RelayPin, OUTPUT);
// Set up the number of columns and rows on the LCD
lcd.begin(20, 4);
}
void loop() {
// Read gas level from the sensor
int gasLevel = analogRead(GasSensor);
// Set a threshold value
int GasThres = 500;
// Check if gas level is above threshold
if (gasLevel > GasThres) {
// If gas leak detected, activate LED and buzzer
digitalWrite(LedPin, HIGH);
digitalWrite(BuzzerPin, HIGH);
tone(7, 262, 250); // Controls the buzzer sound
digitalWrite(RelayPin, HIGH); // Activate relay to close the valve
gasLeak = true;
} else {
// Otherwise, turn off LED and buzzer
digitalWrite(LedPin, LOW);
digitalWrite(BuzzerPin, LOW);
digitalWrite(RelayPin, LOW); // Deactivate relay to open the valve
gasLeak= false;
}
// Display gas valve status and current gas level on LCD
PrintOnLCD(gasLevel);
delay(1000);
}
void PrintOnLCD(int gasLevel) {
// Print a message on the LCD display based on gas leak status
lcd.clear();
lcd.setCursor(0, 0);
// Print current gas level value on the LCD
lcd.print("Gas Level : ");
lcd.print(gasLevel);
//Print the condition on the screen
lcd.setCursor(0, 2);
if (gasLeak) {
lcd.print("Gas Leak Detected!");
lcd.setCursor(0, 3);
lcd.print("Shutting off valve");
} else {
lcd.print("Gas Level in Control");
lcd.setCursor(0, 3);
lcd.print("Valve is Open");
}
}