// Define sensor pins
const int smokeSensorPin = A0; // Analog pin for MQ-2 sensor
const int flameSensorPin = 2; // Digital pin for flame sensor
const int buzzerPin = 9; // Buzzer for alarm
// Threshold values for smoke and fire detection
const int smokeThreshold = 300; // Adjust based on your sensor calibration
const int flameThreshold = LOW; // Flame sensor typically outputs LOW when flame is detected
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set sensor pin modes
pinMode(smokeSensorPin, INPUT);
pinMode(flameSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Start with buzzer off
digitalWrite(buzzerPin, LOW);
}
void loop() {
// Read sensor values
int smokeValue = analogRead(smokeSensorPin);
int flameValue = digitalRead(flameSensorPin);
// Display sensor values on the serial monitor
Serial.print("Smoke Level: ");
Serial.print(smokeValue);
Serial.print(" | Flame Detected: ");
Serial.println(flameValue == LOW ? "YES" : "NO");
// Check if smoke level exceeds the threshold or flame is detected
if (smokeValue > smokeThreshold || flameValue == flameThreshold) {
// Trigger the buzzer
digitalWrite(buzzerPin, HIGH);
Serial.println("ALERT! Smoke or Fire detected!");
} else {
// Turn off the buzzer if no danger
digitalWrite(buzzerPin, LOW);
}
// Add a small delay for stability
delay(500);
}