We have seen fire alarm system or smoke detector alarm system in building or office. It is one of the best safety feature equipment for homes, offices or industries. It can cost around 8$ to 50$. but you can make your own Smoke Detector Alarm System using Arduino. It is suitable for detecting of NH3 (ammonia), Nox, Sulfide, alcohol, Benzene, smoke, CO2,etc. If you want to do more advance project like measure accurately gases etc. you need to calibrate it first. In this project we are detecting smoke and also calculating smoke percentage, which will be seen on LCD display. So lets get started.

Components Required:

For making smoke detector alarm system, you will need,

  1. Arduino,
  2. MQ135 Air Quality Sensor,
  3. LCD display 16X2,
  4. I2C,
  5. 220 ohms Resistor,
  6. LED,
  7. Piezo Buzzer,
  8. Jumper Wires,
  9. Breadboard.
Smoke Detector Alarm System Circuit Diagram:

Smoke detector alarm circuit diagram

Necessary Libraries:

Download library for I2C from GitHub  OR you can download it from here because GitHub library may change by the time or it may not work.

Download l2C library Arduino-LiquidCrystal-I2C-library-master.zip

Code for Smoke Detector Alarm System:

I am using base value 305. But It can be different for you so don’t use my base value. This sensor is also sensitive to temperature & humidity. If you don’t use your base value, it may give false alarm. So find your own environment base values by following steps.

Step 1: First copy paste this code in Arduino IDE.

Step 2: Upload the code.

Step 3: Open serial monitor.

Step 4: wait for 10 to 15 minutes, sensor will give much steady reading (sensor value). Note that value and use it in this code as a base value.

#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);

int redLED = 2;
int greenLED = 3;
int buzzer = 9;
int sensor = A0;
float smokePercentage;
float remainingGas;
float tolerance = 20;

int base_value = 305; // base value
int state; // state for blinking LED & buzzer without delay function

unsigned long previousMillis = 0; 
const long interval = 400; // interval at which to LED blink & Buzzer sound (milliseconds)

void setup() {
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
lcd.begin();
}

void loop() {
unsigned long currentMillis = millis();
float MQ135SensorValues = analogRead(sensor);

Serial.print("Sensor Values: ");
Serial.println(MQ135SensorValues);
lcd.setCursor(0, 0);
lcd.print("Smoke % :");

remainingGas = 1023 - base_value + tolerance;
smokePercentage = (MQ135SensorValues- base_value) /remainingGas ;
if (smokePercentage > 0){
Serial.print("Smoke percentage: ");
Serial.println(smokePercentage*100);
lcd.setCursor(9, 0);
lcd.print(smokePercentage*100);
lcd.setCursor(14, 0);
lcd.print("%");
}
else {
lcd.setCursor(10, 0);
lcd.print("-SAFE-");
}


if (MQ135SensorValues > (base_value + tolerance)) // Checks if MQ135 Sensor Value greater than base + tolerance value
{
digitalWrite(greenLED, LOW);
lcd.setCursor(0, 1);
lcd.print("Smoke Detected!!");
Serial.println("Smoke Detected!!");
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED & buzzer is off turn it on and vice-versa:
if (state == LOW) {
state = HIGH;
} else {
state = LOW;
}
// Red LED and buzzer go HIGH or LOW 
digitalWrite(redLED, state);
digitalWrite(buzzer, state);
}
}
else
{
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(buzzer, LOW);
lcd.setCursor(0, 1);
lcd.print("Sensor Value:");
lcd.setCursor(13, 1);
lcd.print(MQ135SensorValues);

}
delay(500);
lcd.clear();
}
Code Explanation:

Add library for I2C. Read how to connect LCD to I2C and How to Add library.

#include <LiquidCrystal_I2C.h>

Use your I2C address here. 0x3F is my I2C address. you can find it by using I2C scanner code.

LiquidCrystal_I2C lcd(0x3F, 16, 2);

I am giving here 20 points tolerance. You may increase or decrease this number. It is a number which make sure buzzer doesn’t ring on some little changes in environment gases. As soon as it crosses base value plus tolerance value, buzzer starts to make sound.

float tolerance = 20;

As I already told you, you have to find your base value in serial monitor.

int base_value = 305; // base value

This lines are used to make buzzer and LED go HIGH/LOW for 400 milliseconds without delay function.

unsigned long previousMillis = 0;

const long interval = 400; // interval at which to LED blink & Buzzer sound (milliseconds)

analogRead will read the sensor value.

float MQ135SensorValues = analogRead(sensor);

Here we are doing some math to find out smoke percentage. I am assuming resistance changes in MQ135 is directly proportional to Smoke. In short assuming it is linear.

After getting base value which is nothing but our environment gases at room temperature. Maximum value we can get from sensor is 5V i.e 1023. so we are subtracting the base value and plus tolerance will give us remaining gas value.

remainingGas = 1023 - base_value + tolerance;

we know how much gas it can hold in readings i.e remainingGas. Sensor value minus base value will give us smoke, which is added to environment. And dividing it with remaining gas, will give us smoke percentage.

smokePercentage = (MQ135SensorValues- base_value) /remainingGas ;

we want only positive percentage should be shown on lcd screen.

if (smokePercentage > 0){
Serial.print("Smoke percentage: ");
Serial.println(smokePercentage*100);
lcd.setCursor(9, 0);
lcd.print(smokePercentage*100);
lcd.setCursor(14, 0);
lcd.print("%");

If sensor value is greater than base value plus tolerance, it means smoke detected. It will start making buzzer sound and turn red LED light ON & OFF for 400 milliseconds.

if (MQ135SensorValues > (base_value + tolerance))

When smoke detected  red LED On & Off and Making buzzer sound (Alarm) using without delay function.

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED & buzzer is off turn it on and vice-versa:
if (state == LOW) {
state = HIGH;
} else {
state = LOW;
}
// Red LED and buzzer go HIGH or LOW
digitalWrite(redLED, state);
digitalWrite(buzzer, state);
}

If condition doesn’t match. Green LED will light up, which means everything is normal. Also print sensor values on LCD screen.

digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(buzzer, LOW);
lcd.setCursor(0, 1);
lcd.print("Sensor Value:");
lcd.setCursor(13, 1);
lcd.print(MQ135SensorValues);

This is how you can make your own smoke detector alarm.

Till then keep learning keep making.

Video: