Gas Leakage Detector using Arduino and GSM Module with SMS Alert and Sound Alarm

jojo December 23, 2016 64 Comments

We have published lots of interesting arduino projects like GSM based Fire Alarm System , Line Following Robot, RFID Based Access Control System and many other useful projects. This time, we are publishing a highly useful home application – Gas Leakage Detector using Arduino and GSM Module with SMS Alert and Sound Alarm.  We have published an LPG  Sensor Project using Arduino and MQ2 sensor before – which senses lpg leak and produces sound alarm. The project also has a relay system which turns ON or OFF a particular device upon gas leak (say we can turn the main electrical supply OFF upon gas leak to prevent fire).

So let’s come to our project!

Objectives of the Project

  • Detect Gas Leakage (like LPG leak, Butane leak, Methane leak) or any such petroleum based gaseous substance that can be detected using MQ5 Sensor.
  • Setup an SMS based Alert Mechanism and send 3 SMS (3 alert messages) to 2 specified mobile numbers (input inside the arduino program)
  • Produce a sound alarm upon gas leak and stop the alarm once gas leak is under control (gas presence in atmosphere is under normal range)
  • Display status in an LCD using a 16×2 LCD module.

Lets begin to build our project – Gas/LPG leakage detector with SMS Alert and Sound Alarm!.

I recommend you read the following tutorials before building this project!

1. Interfacing MQ5 Sensor to Arduino – teaches you how to interface MQ5 LPG sensor to Arduino and read values using analog or digital out pins of the MQ5 module.

2. Interfacing GSM Module to Arduino – teaches you how to interface a GSM module to Arduino and send/receive text messages using AT Commands.

GSM Module – Buyers Guide – are you looking to buy a GSM module? There are a handful of product variants for GSM module – like SIM900, SIM300, SIM800 etc. We have created this buyers guide to help you select the right GSM module for your project needs.

Circuit Diagram – Gas Leakage Detector using Arduino with GSM Module

Gas Leakage Detector using Arduino and GSM Module with Sound Alarm

Assemble the circuit as shown in diagram! Important connections are explained below.A photograph of the assembled circuit is shown below.

Gas Leakage Detector using Arduino

 

Note:- The speaker is not shown in this photograph. Anyway you can wire the speaker as shown in circuit diagram and connect it to pin 8 of arduino.

The Program/Code

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 
SoftwareSerial mySerial(9, 10);

int sensor=7;
int speaker=8;
int gas_value,Gas_alert_val, Gas_shut_val;
int Gas_Leak_Status;
int sms_count=0;

void setup()
{

pinMode(sensor,INPUT);
pinMode(speaker,OUTPUT);
mySerial.begin(9600);   
Serial.begin(9600);
lcd.begin(16,2);  
delay(500);

}

void loop()
{
CheckGas();
CheckShutDown();
}

void CheckGas()
{

lcd.setCursor(0,0);
lcd.print("Gas Scan - ON");
Gas_alert_val=ScanGasLevel();
if(Gas_alert_val==LOW)
{
 SetAlert(); // Function to send SMS Alerts
}}

int ScanGasLevel()
{
gas_value=digitalRead(sensor); // reads the sensor output (Vout of LM35)

return gas_value; // returns temperature value in degree celsius
}

void SetAlert()
{
digitalWrite(speaker,HIGH);  
while(sms_count<3) //Number of SMS Alerts to be sent
{  
SendTextMessage(); // Function to send AT Commands to GSM module
}
Gas_Leak_Status=1; 
lcd.setCursor(0,1);
lcd.print("Gas Alert! SMS Sent!");
}

void CheckShutDown()
{
if(Gas_Leak_Status==1)
{

Gas_shut_val=ScanGasLevel();
if(Gas_shut_val==HIGH)
{

lcd.setCursor(0,1);
lcd.print("No Gas Leaking");
digitalWrite(speaker,LOW);
sms_count=0;
Gas_Leak_Status=0;
}}}

void SendTextMessage()
{
  mySerial.println("AT+CMGF=1");    //To send SMS in Text Mode
  delay(1000);
  mySerial.println("AT+CMGS=\"+919495xxxxxx\"\r"); // change to the phone number you using 
  delay(1000);
  mySerial.println("Gas Leaking!");//the content of the message
  delay(200);
  mySerial.println((char)26);//the stopping character
  delay(1000);
   mySerial.println("AT+CMGS=\"+918113xxxxxx\"\r"); // change to the phone number you using 
  delay(1000);
  mySerial.println("Gas Leaking!");//the content of the message
  delay(200);
  mySerial.println((char)26);//the message stopping character
  delay(1000);
  sms_count++;
}

Applying ‘gas leak’ using a cigarette lighter – refer photograph

Gas leakage detector using GSM module

Important Aspects about the Program

When we develop critical systems like Gas Leakage Detector or similar systems like Fire Alarm System, we need to monitor the sensor parameters continuously(24×7). So our system must monitor “gas leak” continuously.This is achieved by scanning the sensor output (digital out of MQ5) continuously inside the ScanGasLevel() subroutine. If you look into the program, the main function loop() has only two subroutines – CheckGas() and CheckShutDown() – which are called repeatedly. CheckGas() – is a subroutine which scans sensor output continuously and take actions if there occurs a ‘gas leak’ at any point of time.  CheckShutDown() – is a subroutine to monitor the shut down process and check if status of room is back to normal conditions (no gas leaking).

CheckGas() – is the function which monitors occurrence of a gas leak 24×7.  This function fetches the gas level measured by MQ35 (by reading digital out of MQ35 using digitalRead() command) and stores it to the variable Gas_alert_val for comparison. If there is no ‘gas leak’ – the sensor out will be HIGH. If there occurs a ‘gas leak’ at any point of time, the sensor out will immediately change to LOW status. The statement  if(Gas_alert_val==LOW) checks this and if a gas leak occurs, then an inner subroutine SetAlert() will be invoked.

 SetAlert() is the function that controls number of SMS alerts sent to each mobile number loaded in the program. The number of SMS alerts sent can be altered by changing the stopping condition of while loop. The stopping condition sms_count<3 – means 3 SMS alerts will be sent to each mobile number. If you want to send 5 alerts, just change the stopping condition to sms_count<5 – you got it ?  The function to send SMS (using AT Commands) – SendTextMessage() will be called 3 times if SMS alert count is 3. This function SendTextMessage() will be invoked as many times as the number SMS alerts set in the program. In addition to sending SMS alerts, this subroutine also controls the sound alarm. The alarm is invoked using command digitalWrite(speaker,HIGH) – which will activate the speaker connected at pin 8 of arduino.

Note:- We have limited the number of SMS alerts using the stopping condition. Once a gas leak occurs and the set number of SMS alerts has been sent, the system will not send any more SMS! The system assumes that its job is over by sending SMS. Humans has to come and shut down the gas leak problem. After sending alerts, the system will start monitoring Shut Down process. Once the gas leak has been eliminated, system will automatically reactivate its SMS alert settings by resetting the sms_count variable back to zero.

The output after applying ‘gas leak’ is shown in photograph!

Gas leakage detector with sms alert

 

Here is a video demonstration of the output!

CheckShutDown() – is the function which monitors if gas leak was shut down. We need to entertain this function only if a ‘gas leak’ has occurred. To limit the entry to the statements inside this routine, we have introduced a variable Gas_Leak_Status. This variable value will be set to value 1 when a gas leak occurs (check the statement inside SetAlert()). The statements inside CheckShutDown() will  be executed only if the value of Gas_Leak_Status==1. (If there was no gas leak occurred, we don’t need to waste time executing ShutDown checking statements). We consider the ‘gas leak’ has been eliminated once room temperature is back to normal. So if our variable Gas_shut_val falls back to HIGH status, we consider gas leak has been eliminated and surroundings are safe. The subroutine has statement to stop the gas leakage alarm (refer statement – digitalWrite(speaker,LOW) – which cuts the supply to pin 8 of arduino and stops the sound alarm) which will be executed when gas leak is eliminated completely (as the status of Gas_shut_val == HIGH). We start our Gas Leakage monitoring again with SMS Alerts active! (We reset the Gas_Leak_Status variable and sms_count variable back to zero – which are essential variable conditions for monitoring gas leak again and to send alert sms if gas leak repeats.

Here is a slideshare presentation of the project!

Comments
  • Jevaan
    February 21, 2018

    What if I don’t use the LCD but I used arduino uno, mq5, gsm module sim900 and buzzer. Is it possible to get sms from the gsm module? Or should use some othe coding?

  • SASI KIRAN.I
    February 16, 2018

    Sir,i put buzzer as per the circuit diagram but it is allways giving sound without sensing any gas.How to rectify the problem.Is there any problem in programme code.please send me sir.

  • denden
    February 7, 2018

    can you give me instruction how to assemble all of components ? please ? reply As soon as possible before project deadline 🙂

  • January 18, 2018

    hello sir the project is very nice please sir i wish to find out if it is possible to replace the arduino with a pic16f microcontrolle, if yes then please how do i go about?

  • fuzen
    January 14, 2018

    can i use gsm sim900A?

  • Aiswarya
    January 2, 2018

    mySerial.begin(9600);
    what does 9600 mean in this line?

    • August 11, 2018

      9600 represents speed of microcontroller i.e the microcontroller sends and rcives information at the speed of 9600 bytes per second

  • Arjun
    November 18, 2017

    sir can i buy this this project,and how much amount of money is required to do this project

    • Arjun
      November 27, 2017

      sir can i buy this this project,and how much amount of money is required to do this project

  • Devanshu Biswas
    November 8, 2017

    Arduino: 1.8.4 (Windows 8.1), Board: “Arduino/Genuino Uno”

    Sketch uses 4892 bytes (15%) of program storage space. Maximum is 32256 bytes.
    Global variables use 489 bytes (23%) of dynamic memory, leaving 1559 bytes for local variables. Maximum is 2048 bytes.
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x0b
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x0b
    Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    getting this error message while uploading

  • November 3, 2017

    This is marvelous! no more fire out break as a result of cooking gas leakage at home! Weldone Adnin.

    I need to work on a scrolling dotmatrix of 160×16 using atduino, can you help me with the project sir?

  • MZAR
    September 28, 2017

    hi sir can you help me, i use gsm module sim 900A.. can i use this coding ? or i need other coding .. please help me sir. tq

  • JC
    September 27, 2017

    What should I do to the program if I don’t want to include an lcd display?

  • piriyal
    September 25, 2017

    what is we will do the project with gsm 300 board and mq2 gas sensor can u help me

  • Hariharan
    September 14, 2017

    This circuit can be connected using 9v battery.

  • wei fei
    September 10, 2017

    hello, my project is same like you but i use arduino and send notifications through telegram. Do you have any coding of arduino that can connect to telegram?

  • Shuvo
    August 22, 2017

    jojo I used MQ 9, but it doesn’t not responding. screen always shows no gas leakage. then, I used MQ 5, it is also same. please give me a suggestion.

  • jitendra patel
    August 21, 2017

    my project working but I hv not received sms so can u help me?

  • Shuvo
    August 20, 2017

    jojo, I am following your project steps. it’s helpful. jojo I want to add solenoid valve to control the gas supply also. so, what will be the change in this code? can you help me?

  • suki
    August 5, 2017

    Is this program valid for arduino uno?

    • jojo
      August 5, 2017

      Yes. The circuit and code is developed using Arduino Uno.

  • pradip patil
    July 29, 2017

    i want add exhausted fan inn that project how to add and where to code for that fan guide me with code sir when gas leakage detect fan is automatic on and after dowm fan is off

  • pradip patil
    July 29, 2017

    i wannn add exhausted fan in that project what i do and where i code that for switch on fan to reduce air in room suggest what i do sir plzz

  • zariab
    June 13, 2017

    how can we control a device by using this project? like turning off the device which is suspected for gas leakage.

  • hazz
    June 8, 2017

    all the 5v supply we connect on the in 5v at arduino right?

  • akshay
    May 26, 2017

    Hello!
    The sms goes to the first number only. How to you make it go to the second number also

  • Shibabrata Mukherjee
    April 7, 2017

    Can i connect more than one sensor parallely? How is it possible??

    • jojo
      April 10, 2017

      Yes. You can. Use the free pins of Arduino.

      • hazz
        June 8, 2017

        all the program/coding…i upload in the arduino?

      • neha
        November 23, 2017

        sir can u suggest us any free pins aurdino.. which is suitable for gas leak detection……

  • anvesh
    April 3, 2017

    very helpful and easily understand by any person who is new to projects .
    Thank you

  • Avinash
    March 31, 2017

    thanku for giving such beautiful project
    I am also doing the same project sir but i need u to help me out pls I want a prg for automatic gas booking along with gas lekg detcr.

  • Circuitbaba
    March 31, 2017

    hello…which gsm module you ve used & how to program the gsm modem…..Plz reply

  • ruturaj dighe
    March 28, 2017

    where should i get report of this project..???

  • dipesh
    March 20, 2017

    sir i had done this project same like that but it,s not working . even lcd does not get power supply

  • SAMEER
    February 20, 2017

    please help me in knowing how to upload the code ARDUINO and could you HELP in letting me know the components names used here sir. As i’m very much interested in doing this project. and yeah how to upload the programme THE APPLICATION USED TO do it.

  • Ahsen
    February 17, 2017

    I tried this, worked beautifully. I also added a solneoid valve for automatic shutoff, just replacing the buzzer with the valve. Thanks a bunch!

  • HAFIZ AUWAL
    February 12, 2017

    I WANT TO DETECT LPG USING MICROCONTROLLER, PLEASE HOW

  • harry
    February 10, 2017

    can u plz help me i have followed the above circuit diagram everything is running fine i have used the same code which is given above my gas is been detected but the major problem is that the sms is not getting send on my cell. HOW TO DO IT ? PLZ HELP I HAVE MY SUBMISSION :(((((

  • Damola
    February 7, 2017

    How do i modify this code to use analog out of the gas sensor then display the gas values in real time on the lcd then finally when there is a gas leakage it turns on an exhaust fan as well as the speaker and sms?

    Thank you

  • Anya Westby
    January 12, 2017

    Hi there! I’m interested in doing this project, and if you already answered this I might’ve missed it, but what programming application did you use to write/run your code so that the arduino would execute it?

  • eleazar
    December 26, 2016

    Hello… please I tried implementing this project but the SMS received is infinite. It give an infinite SMS. Please how can I stop that. In my case I need just one alert SMS at a time.

  • Rajesh
    November 8, 2016

    Good, project…….!!!,
    i have tried this using my mq6 module, but my module is not heating up to apply gas, Help me with this please….

  • Arsen
    October 30, 2016

    What will be the difference if GSM Shield is used? What changes must be written in the code? would be very helpful, thank you in advance

  • ARSENE
    October 29, 2016

    will the code be the same for MQ2 gas sensor. thanks in advance

  • Dhivya
    September 23, 2016

    Is this necessary to change the ppm range in the sensor to detect…

  • siti aishah
    August 10, 2016

    can this project combined with gsm based fire alert system arduino project? as I need to come with need idea for my final year project.

  • Richmond
    August 2, 2016

    Can someone help me? This is my code.

    #include
    #include “SIM900.h”
    #include “sms.h”
    #include
    //#include
    #include
    SMSGSM sms;
    boolean started = false;
    char buffer[160];
    char smsbuffer[160];
    char n[20];
    //LiquidCrystal lcd(4,2,3,7,8,9);
    int buttonState;
    int lastButtonState = LOW;
    long lastDebounceTime = 0;
    long debounceDelay = 50;
    boolean st = false;
    int buzzer = 12;

    void setup() {

    //lcd.begin(16, 2);
    Serial.begin(9600);
    if (gsm.begin(2400))
    {
    started = true;
    }
    if (started)
    {
    delsms();
    }
    sms.SendSMS(“+60142646978” , “Gas Sensor and GSM module activated”);

    }

    void loop() {

    //lcd.setCursor(0, 0);

    //lcd.print(“Detektor Gas SMS”);
    int val = analogRead(A0);
    val = map(val, 0, 1023, 0, 100);
    //lcd.setCursor(0,1);
    //lcd.print(“Kadar: “);
    //lcd.print(val);
    //lcd.print(“% “);

    //code using sensor detection
    if (val > 10) {
    tone(buzzer,800,500);
    delay(1000);
    st = true;
    }
    else st = false;

    if (st != lastButtonState) {
    lastDebounceTime = millis();
    }

    if ((millis() – lastDebounceTime) > debounceDelay) {

    if (st != buttonState) {
    buttonState = st;

    if (buttonState == HIGH) {
    PString str(buffer, sizeof(buffer));
    str.begin();
    str.print(“Gas Detected! Gas leakage at “);
    str.print(val);
    str.print(“%”);
    //String a=str;
    sms.SendSMS(“+60142646978”, buffer);
    }
    }
    }

    //code using sms lapor.
    lastButtonState = st;
    int pos = 0;
    if (started)
    {
    pos = sms.IsSMSPresent(SMS_ALL);
    if (pos)
    {
    sms.GetSMS(pos, n, smsbuffer, 100);
    delay(2000);
    if (!strcmp(smsbuffer, “lapor”))
    {
    PString str(buffer, sizeof(buffer));
    str.begin();
    str.print(“Rate of gas leakage currently at “);
    str.print(val);
    str.print(“%”);
    //String a=str;
    sms.SendSMS(“+60142646978”, buffer);
    }
    delsms();
    }
    }
    }

    //delete sms yang dihantar
    void delsms()
    {
    for (int i = 0; i < 10; i++)
    {
    int pos = sms.IsSMSPresent(SMS_ALL);
    if (pos != 0)
    {
    if (sms.DeleteSMS(pos) == 1) {} else {}
    }
    }
    }

    I'm using arduino uno, sim900 module, mq2 gas sensor and buzzer to create a gas sensor detector based on sms.
    I have to 2 option :
    1. The mq2 gas sensor detects and send the result via sms to the number set in the code.
    2. We can send a specific string to know the surrounding gas percentage and send the result to the specific number set in the code.

    But I want to change the second option to be auto reply to any incoming number. What should I do?

  • Asif Marjan
    August 2, 2016

    Sir I an interested in this project but I want to use Unipolar Stepper Motor too with ULN2003A driver. So I want when the gas leakage is detected then the Stepper Motor rotates 180 degree in anticlockwise direction to OFF the gas regulator. So Sir I need programming for Stepper motor too that fulfill my above requirements. Plz reply!

  • Asif Marjan
    August 1, 2016

    Sir can I use Sim-900A or Sim-900D GSM module instead of Sim-900 because Sim-900 GSM module is not available in my country. And if I use Sim-900A or Sim-900D GSM module instead of Sim-900 then I have to change the code also or it remains the same for Sim-900A and Sim-900D module? Plz reply! Thank you!

  • ABDUR
    July 14, 2016

    how can we interface servomotor instead of buzzer

  • mat
    June 28, 2016

    hello..this mini project its sale?can i buy this because i dont know how to make it..please email me.
    tq

  • keerthan
    April 25, 2016

    how do i merge temperature sensor code with smoke detector code?

  • Ramesh k. pandey
    February 27, 2016

    hi,
    it s very intersting project for home safety.
    thanks, for such project,
    ramesh

    • jojo
      February 28, 2016

      @Ramesh – hope you tried this gas leakage detector project using arduino! Thanks for the feedback.

      • June 4, 2016

        I connected but but the SMS is not going please help me

      • saurabh
        July 16, 2016

        is it working…….? if i add LED at pin 8 then without sensing LPG it is glowing…….can u please tell how it is working…….please send me the detail….

  • Bhavani
    February 18, 2016

    Vanakam jojo.this is very useful. I need a code for lpg leakage detector with arduino uno and Gsm with mq2 sensor without using lcd display.pls help me with ur code jojo

    • jojo
      February 18, 2016

      @bhavani – the code is already given there in this article – http://www.circuitstoday.com/gas-leakage-detector-using-arduino-with-sms-alert

      There is not much difference in code when you are using Digital Out of MQ2! You can easily do it yourself by modifying this code!

      • Asif hamid
        May 30, 2016

        I am using mq4 sensor n the code doesn’t work on it.it senses the gas but msg cant b sent to the given no.now plzz tell me what i have to do now or please provide me the new code

  • S.Yogaraj
    February 15, 2016

    Very useful project and the code is explained neatly
    Thank you author.

    S.yogaraja

Leave a Reply to Bhavani Cancel reply

Your email address will not be published. Required fields are marked *