0% found this document useful (0 votes)
17 views

fire&Smoke_code

This document contains an Arduino sketch for a smoke and fire detection system using an MQ-2 gas sensor and a flame sensor. It utilizes an OLED display to show alerts for smoke or fire detection and activates an alarm with a buzzer and LED. The system continuously reads sensor data and updates the display accordingly, providing real-time feedback on the environmental conditions.

Uploaded by

aS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

fire&Smoke_code

This document contains an Arduino sketch for a smoke and fire detection system using an MQ-2 gas sensor and a flame sensor. It utilizes an OLED display to show alerts for smoke or fire detection and activates an alarm with a buzzer and LED. The system continuously reads sensor data and updates the display accordingly, providing real-time feedback on the environmental conditions.

Uploaded by

aS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Wire.

h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128


#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define MQ2_PIN A0 // MQ-2 gas sensor pin


#define FLAME_SENSOR_PIN 7 // Flame sensor digital pin (D0)
#define BUZZER_PIN 8 // Buzzer pin
#define LED_PIN 9 // LED pin

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!");

// Set fixed intensity level since D0 is digital


display.setCursor(0, 20);
display.println("Intensity: HIGH");

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);
}

You might also like