Jump to content

MDM331AID/Experiment 4

From cognifront

Experiment: Gas Leakage Detection Using Arduino UNO

Aim

To develop a gas leakage detection application using an Arduino UNO and an MQ-2 gas sensor to monitor the surrounding environment and generate an alert when the sensor reading exceeds a predefined threshold.

Objectives

  • To understand the working principle of the MQ-2 gas sensor.
  • To interface a gas sensor with the Arduino UNO.
  • To read analog sensor values using the ADC of the Arduino.
  • To implement threshold-based gas detection.
  • To activate an alert using an LED and buzzer when the sensor reading exceeds the set threshold.

Components Required

Sr. No. Component Quantity
1 Arduino UNO Development Board (COG-U1) 1
2 MQ-2 Gas Sensor (COG-MSBA) 1
3 LED bar 1
4 Buzzer 1
5 USB data cable 1
6 Patch cords As required
7 Computer with Arduino IDE 1

Theory

Gas Leakage Detection

Gas leakage detection is an important application in homes, industries, laboratories, and other environments where combustible gases may be present. A gas sensor detects changes in the surrounding gas concentration and generates an electrical signal that can be processed by a microcontroller.

MQ-2 Gas Sensor

The MQ-2 is a semiconductor gas sensor commonly used for detecting combustible gases and smoke. It is sensitive to gases such as LPG, propane, methane, hydrogen, and smoke.

The sensor contains a sensing element whose electrical resistance changes in the presence of certain gases. The sensor module converts this change into an electrical output.

Common MQ-2 module interfaces include:

  • VCC: Power supply input.
  • GND: Ground connection.
  • AO: Analog output voltage related to the sensor response.
  • DO: Digital output from an onboard comparator, often with an adjustable threshold.

In this experiment, the analog output is used to read sensor values through the Arduino UNO's analog input.

Analog-to-Digital Conversion

The Arduino UNO has a 10-bit analog-to-digital converter (ADC). The analogRead() function converts the analog input voltage into a digital value ranging from 0 to 1023 when using the default reference.

Threshold-Based Detection

The Arduino continuously reads the analog output of the MQ-2 sensor. The measured value is compared with a predefined threshold.

  • If the sensor reading is below the threshold, the system indicates normal operation.
  • If the sensor reading is equal to or above the threshold, the system activates the alert LED and buzzer.

The threshold used in this experiment is a demonstration value and must be adjusted based on actual sensor readings. It does not represent a calibrated gas concentration.

Pin Connections

Arduino UNO Pin MQ-2 / Output Connection Description
5V MQ-2 VCC Sensor power supply
GND MQ-2 GND Common ground
A0 MQ-2 AO Analog sensor output
D8 LED bar input Visual alert output
D9 Buzzer input Audible alert output

Note: These connections assume that the MQ-2 board exposes an analog output compatible with the Arduino UNO and that the LED bar and buzzer accept active-HIGH digital control. Verify the actual pin labels and interface of your Cognifront sensor and output boards before connecting.

Connection Diagram

          MQ-2 Gas Sensor
         +---------------+
         |               |
         | VCC  ---------+---- 5V
         | GND  ---------+---- GND
         | AO   ---------+---- A0
         +---------------+

             Arduino UNO
         +------------------+
         |                  |
         | A0  <--- MQ-2 AO |
         |                  |
         | D8  ------------+---- LED Bar Input
         |                  |
         | D9  ------------+---- Buzzer Input
         |                  |
         | GND ------------+---- Common GND
         +------------------+

Arduino Program

const int gasPin = A0;
const int ledPin = 8;
const int buzzerPin = 9;

// Demonstration threshold; calibrate for your setup
const int gasThreshold = 400;

int gasValue = 0;

void setup() {
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  digitalWrite(ledPin, LOW);
  digitalWrite(buzzerPin, LOW);

  Serial.println("MQ-2 Gas Detection System");
}

void loop() {
  gasValue = analogRead(gasPin);

  Serial.print("Gas Sensor Value: ");
  Serial.println(gasValue);

  if (gasValue >= gasThreshold) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    Serial.println("WARNING: Gas level threshold exceeded!");
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
    Serial.println("Status: Normal");
  }

  delay(1000);
}

Code Explanation

Statement Description
const int gasPin = A0; Defines analog pin A0 as the gas sensor input.
const int ledPin = 8; Defines digital pin D8 as the LED control output.
const int buzzerPin = 9; Defines digital pin D9 as the buzzer control output.
const int gasThreshold = 400; Sets a demonstration threshold for gas detection.
analogRead(gasPin) Reads the analog output of the gas sensor and returns a value from 0 to 1023.
Serial.println(gasValue) Displays the sensor reading on the Serial Monitor.
if (gasValue >= gasThreshold) Checks whether the sensor reading has reached or exceeded the threshold.
digitalWrite(ledPin, HIGH) Activates the LED alert output.
digitalWrite(buzzerPin, HIGH) Activates the buzzer alert output.
delay(1000) Pauses the program for one second before the next reading.

Procedure

  1. Connect the MQ-2 sensor's VCC and GND pins to the Arduino UNO's 5V and GND, respectively.
  2. Connect the sensor's analog output (AO) to the Arduino analog input A0.
  3. Connect the LED bar input to D8 and the buzzer input to D9, ensuring that the output interfaces are compatible.
  4. Connect the Arduino UNO to the computer using the USB data cable.
  5. Open the Arduino IDE and select the Arduino UNO board and appropriate COM port.
  6. Enter the program and upload it to the Arduino UNO.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Allow the MQ-2 sensor to warm up and stabilize according to its datasheet and module instructions.
  9. Observe the sensor readings under the available ambient conditions.
  10. Adjust the demonstration threshold based on observed readings, if required.
  11. Observe the LED and buzzer response when the reading crosses the configured threshold.

Safety precaution: Do not intentionally release LPG, lighter gas, smoke, or other combustible gases to test the circuit. This is an educational demonstration, not a certified gas alarm or safety system. Conduct any controlled sensor testing only with appropriate laboratory safety procedures.

Observations

Sr. No. Sensor Reading Condition LED Buzzer
1 Below threshold Normal OFF OFF
2 Equal to or above threshold Threshold exceeded ON ON

The readings and alert response should be recorded during the experiment. The table describes expected behavior, not measured results.

Result

An Arduino UNO-based gas detection demonstration was developed using an MQ-2 sensor. The sensor's analog output was read and compared against a predefined threshold. An LED and buzzer were programmed to provide alerts when the reading exceeded the threshold.

Viva Questions

  1. What is the purpose of a gas sensor?
  2. What is the working principle of the MQ-2 sensor?
  3. Which gases can the MQ-2 sensor detect?
  4. What is the difference between the analog and digital outputs of an MQ-2 module?
  5. What is the resolution of the Arduino UNO ADC?
  6. What is the purpose of the analogRead() function?
  7. Why is a threshold used in gas detection?
  8. Why does an MQ-2 sensor require a warm-up period?
  9. Can an MQ-2 sensor directly measure gas concentration in parts per million without calibration?
  10. What are the limitations of a threshold-based gas detection system?
  11. How can this project be extended to send gas alerts through an IoT cloud platform?