fire&Smoke_code
fire&Smoke_code
h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
pinMode(MQ2_PIN, INPUT);
pinMode(FLAME_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
display.clearDisplay();
}
void loop() {
int smokeLevel = analogRead(MQ2_PIN); // Read MQ-2 gas sensor
bool fireDetected = digitalRead(FLAME_SENSOR_PIN) == LOW; // Flame detected if
D0 is LOW
bool smokeDetected = smokeLevel > 400; // Smoke detected threshold
display.clearDisplay();
if (smokeDetected) {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Smoke Detected!");
activateAlarm();
} else if (fireDetected) {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Fire Detected!");
activateAlarm();
} else {
// No fire or smoke detected
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("No Smoke/Fire");
delay(1000);
}
display.display();
delay(500); // Delay between readings
}
void activateAlarm() {
digitalWrite(LED_PIN, HIGH); // Blink LED
tone(BUZZER_PIN, 1000); // Activate buzzer
delay(200);
digitalWrite(LED_PIN, LOW); // LED off
noTone(BUZZER_PIN); // Buzzer off
delay(200);
}