int gasSensor = A0; // MQ-2 connected to A0
int ledPin = 13; // LED connected to pin 13
int gasValue = 0;
int threshold = 800; // Adjust based on sensor
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
gasValue = analogRead(gasSensor);
Serial.print("Gas Value: ");
Serial.println(gasValue);
// While gas level is above threshold
while (gasValue > threshold) {
digitalWrite(ledPin, HIGH);
Serial.println("⚠️ Gas Detected!");
delay(500);
// Read again inside while loop
gasValue = analogRead(gasSensor);
Serial.print("Gas Value: ");
Serial.println(gasValue);
}
// Safe condition
digitalWrite(ledPin, LOW);
delay(500);
}