const int sensorPin = A0; // Pin connected to MQ sensor AO
const int buzzerPin = 8; // Pin connected to Buzzer (optional)
const int ledPin = 13; // Built-in LED on Arduino
// Threshold for detection (0 to 1023)
// Adjust this value based on your room's clean-air reading
const int gasThreshold = 400;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int gasValue = analogRead(sensorPin); // analogRead returns an integer (0–1023)
Serial.print("Gas Sensor Value: ");
Serial.println(gasValue);
// Check if gas level exceeds the threshold
if (gasValue > gasThreshold) {
digitalWrite(ledPin, HIGH); // Turn on LED alert
digitalWrite(buzzerPin, HIGH); // Sound buzzer
Serial.println("WARNING: Gas Detected!");
} else {
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzerPin, LOW); // Turn off buzzer
}
delay(500); // Small delay to prevent spamming the serial monitor
}