GAS SENSOR WITH WARNING SYSTEM USING ARDUINO UNO

Nowadays, natural gas is used as a fuel in almost every home, but in case of a leak, it can pose a serious threat. Even a small spark can lead to devastating consequences. Today, we will build a sensor that detects excessive gas in the environment, triggers an alarm, flashes a warning light, and displays the message “Gas Danger” on a screen. This way, even if we fail to notice the gas odor, we will still be able to hear and see the danger in time.

Components Used

SENSORS:
  • MQ-2 Gas Sensor (Detects gases such as LPG, CO, smoke, and methane)
Other Components:
  • Red LED (Indicates danger when gas levels are high)
  • Green LED (Indicates safe conditions)
  • Buzzer (Activates when gas levels are high)
  • LCD 16×2 with I2C Module (Displays gas concentration values)
  • 2x 1kΩ Resistor (Current-limiting resistors for LEDs)
  • 330Ω Resistor (To reduce the sound output of the buzzer)
  • Jumper Cables (8x Male-to-Male, 4x Female-to-Male)
MICROCONTROLLER:
  • Arduino UNO

CIRCUIT DIAGRAM

The MQ-2 sensor is connected to the analog input of the Arduino, while LEDs and the buzzer are connected to digital pins. The LCD screen communicates using the I2C interface to minimize the number of required connections.

Component Arduino Pin
MQ-2 SensorA0
Red LEDD7
Green LEDD4
BuzzerD2
LCD (I2C)SDA (A4), SCL(A5)

The circuit designs we created in the Fritzing application are shown in Image 1 and Image 2.

Image 1
Image 2

CODE EXPLANATION

First, we import the necessary libraries for the LCD screen (Lines 1, 2). Then, we define the pins that will be used (Lines 4 – 7). We write the required code to call the LCD screen using the “lcd” command (Line 9) and then initialize the screen (Line 12). To set the LCD backlight to blue, we add the corresponding command (Line 13). We specify that the defined pins will be used as outputs (Lines 15 – 17) and start serial communication (Line 18).

Next, we store the value received from the analog gas sensor in an integer variable named “value” (Line 22) and print this value to the serial monitor (Line 23). If the value is between 350 and 500, the red LED will blink at 1-second intervals, and the buzzer will sound. Additionally, the LCD screen will display the warning “GAS LEAK! BE CAREFUL!” (Lines 24 – 40). If the value exceeds 500, the red LED will blink at 300-millisecond intervals, the buzzer will sound more frequently, and the LCD screen will show the warning “DANGER! LEAVE THE AREA!” (Lines 41 – 57). If the value is below 350, the green LED will remain on continuously, and no warning message will be displayed on the screen.

THE CODE

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#define sensor A0 
#define redLed 7
#define greenLed 4
#define buzzer 2

LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.begin(16,2);
  lcd.backlight();

  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(sensor);
  Serial.println(value);
  if(value>350 && value<500) {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    digitalWrite(buzzer, HIGH);

    lcd.setCursor(0,0);
    lcd.print("GAS LEAK!");
    lcd.setCursor(0,1);
    lcd.print("BE CAREFUL!");

    delay(1000);

    lcd.clear();

    digitalWrite(redLed, LOW);
    digitalWrite(buzzer, LOW);
    delay(1000);
  } else if(value>500) {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    digitalWrite(buzzer, HIGH);

    lcd.setCursor(0,0);
    lcd.print("DANGER! LEAVE");
    lcd.setCursor(0,1);
    lcd.print("THE AREA!");

    delay(300);

    lcd.clear();
    
    digitalWrite(redLed, LOW);
    digitalWrite(buzzer, LOW);
    delay(300);
  } else {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    digitalWrite(buzzer, LOW);
  }
}

RESULTS

The system successfully measured the gas level using the MQ-2 gas sensor. When the predefined threshold values were reached, the system activated the warning mechanism and issued the necessary alert. This ensured that a potential gas leak was detected in time, allowing for preventive measures to be taken.

Yorum bırakın