#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Define LCD pin configuration
LiquidCrystal lcd(8, 9, 7, 6, 5, 4);
// Define buzzer, LED, and DS18B20 sensor pins
const int buzzerPin = 3; // Connect to Bz1:1
const int ledPin = 13; // Connect to Digital Pin 13
const int heatSensorPin = 2; // Connect to Data (Pin 2) of DS18B20 sensor
// Setup a oneWire instance to communicate with the DS18B20 sensor
OneWire oneWire(heatSensorPin);
DallasTemperature sensors(&oneWire);
// Threshold temperature for the fire alarm
const float fireThreshold = 50.0; // Adjust as needed
void setup() {
lcd.begin(16, 2);
lcd.print("Fire Alarm System");
delay(2000); // Wait for 2 seconds
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize the DS18B20 sensor
sensors.begin();
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fire Alarm System");
// Request temperature data from the DS18B20 sensor
sensors.requestTemperatures();
// Get the temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
lcd.setCursor(0, 1);
lcd.print("Temperature: ");
lcd.print(temperatureC);
lcd.print(" °C");
// Check if the temperature is above the threshold
if (temperatureC > fireThreshold) {
// Turn on the buzzer and LED simultaneously
tone(buzzerPin, 1000); // Adjust the frequency as needed
digitalWrite(ledPin, HIGH);
// Display the alarm message on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fire Detected!");
lcd.setCursor(0, 1);
lcd.print("Evacuate!");
// Wait for a moment
delay(5000); // Adjust the delay as needed
// Turn off the buzzer and LED
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
}
// Wait for 5 seconds before the next loop
delay(5000);
}