#include <Servo.
h> // Include the Servo library for controlling the servo motor
// Define pin numbers for the components
const int gasSensorPin = A0; // Analog pin for the MQ-2 gas sensor
const int buzzerPin = 8; // Digital pin for the buzzer
const int servoMotorPin = 9; // Digital pin for the servo motor
const int exhaustFanPin = 10; // Digital pin for the exhaust fan (connected via
MOSFET)
Servo myServo; // Create a servo object
// Define thresholds
const int gasThreshold = 400; // Adjust this value based on your sensor's
sensitivity and environment
// Higher value means less sensitive, lower means
more sensitive
void setup() {
[Link](9600); // Initialize serial communication for debugging
[Link](servoMotorPin); // Attach the servo motor to its pin
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as an output
pinMode(exhaustFanPin, OUTPUT); // Set exhaust fan pin as an output
// Initial state: gas regulator ON, buzzer OFF, fan OFF
[Link](90); // Adjust this angle to keep the regulator "ON"
digitalWrite(buzzerPin, LOW);
digitalWrite(exhaustFanPin, LOW);
[Link]("LPG Leakage Detection System Initialized");
[Link]("Gas Regulator is ON");
}
void loop() {
int gasValue = analogRead(gasSensorPin); // Read analog value from the gas sensor
[Link]("Gas Sensor Value: ");
[Link](gasValue);
if (gasValue > gasThreshold) {
// Gas leakage detected
[Link]("!!! GAS LEAKAGE DETECTED !!!");
// Activate buzzer
digitalWrite(buzzerPin, HIGH);
// Turn off gas regulator (close valve)
[Link](0); // Adjust this angle to turn the regulator "OFF"
[Link]("Gas Regulator is OFF");
// Turn on exhaust fan
digitalWrite(exhaustFanPin, HIGH);
[Link]("Exhaust Fan is ON");
// You might want to add a delay here or a mechanism to keep the alert active
// until the gas level drops below the threshold for a certain period.
delay(500); // Small delay to keep the buzzer on for a moment
} else {
// No gas leakage
if (digitalRead(buzzerPin) == HIGH) { // If buzzer was active, turn it off
digitalWrite(buzzerPin, LOW);
[Link]("Gas Leakage Cleared. Buzzer OFF");
}
if ([Link]() == 0) { // If regulator was off, turn it back on
[Link](90); // Adjust this angle to turn the regulator "ON"
[Link]("Gas Regulator is ON");
}
if (digitalRead(exhaustFanPin) == HIGH) { // If fan was on, turn it off
digitalWrite(exhaustFanPin, LOW);
[Link]("Exhaust Fan is OFF");
}
}
delay(100); // Small delay before reading the sensor again
}