#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust according to your LCD module
// Pin definitions
const int gasSensorPin = A1; // Analog input pin for MQ-2 sensor
const int ledPin = 13; // Digital output pin for LED
const int buzzerPin = 6; // Digital output pin for buzzer
// Threshold value for gas detection
const int threshold = 500; // Adjust this value based on your calibration
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(ledPin, OUTPUT); // LED pin as output
pinMode(buzzerPin, OUTPUT); // Buzzer pin as output
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Leakage");
}
void loop() {
int sensorValue = analogRead(gasSensorPin); // Read analog value from sensor
Serial.print("Gas Sensor Value: ");
Serial.println(sensorValue);
// Update LCD with sensor value
lcd.setCursor(0, 1);
lcd.print("Sensor: ");
lcd.print(sensorValue);
// Check if gas concentration exceeds threshold
if (sensorValue > threshold) {
// Gas detected, activate LED and buzzer
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
lcd.setCursor(0, 1);
lcd.print("Gas Detected "); // Update LCD with alert message
delay(1000); // Delay for 1 second
} else {
// No gas detected, turn off LED and buzzer
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
lcd.setCursor(0, 1);
lcd.print(" ");
}
delay(1000);
}