top of page
Writer's pictureSanskruti Ashtikar

Gas Leakage Detection System Using PIC Microcontroller

Updated: Nov 29

Introduction


Gas leakage detection systems are crucial for ensuring safety in both residential and industrial environments. These systems are designed to detect the presence of hazardous gases like LPG (Liquefied Petroleum Gas), methane, or other combustible gases in the air and alert the user to prevent potential accidents. In this article, we will explore the design and implementation of a gas leakage detection system using a PIC microcontroller. This system will detect gas leakage, trigger an alarm, and can even activate a ventilation system.





Overview of the Project


The project involves using a gas sensor (such as MQ-2, MQ-6, or MQ-135) interfaced with a PIC microcontroller to detect the presence of gas. When the gas concentration exceeds a predefined threshold, the system will activate an alarm (such as a buzzer) and may also turn on a fan or send a signal to a connected system for further action. This project is ideal for safety systems in homes, factories, and other environments where gas leaks pose a risk.


Components Required


  • PIC Microcontroller (PIC16F877A): The central processing unit that controls the entire system.

  • Gas Sensor (MQ-2/MQ-6/MQ-135): A sensor that detects the presence of gas in the air.

  • Buzzer/Alarm: Provides an audible alert when gas is detected.

  • LCD Display (16x2): Displays the gas concentration and system status.

  • Relay Module: Can be used to control a fan or other external devices for safety measures.

  • Oscillator (Crystal and Capacitors): Provides the necessary clock signal to the microcontroller.

  • Power Supply: To power the microcontroller, sensor, and other components.

  • Resistors, Capacitors, and Diodes: Additional components for circuit stability and protection.

  • Connecting Wires: For making connections between different components.

  • Breadboard/PCB: For assembling the circuit.


Circuit Diagram


Below is a basic circuit diagram for the gas leakage detection system:


Gas Sensor (MQ-2/MQ-6)  -->  PIC16F877A  -->  Buzzer/Alarm
              |                      |
          Power Supply            LCD Display
                                  Relay Module (Optional)
  1. Gas Sensor to PIC Microcontroller:

  2. The analog output pin of the gas sensor is connected to an analog input pin of the PIC microcontroller (e.g., AN0).

  3. The sensor’s Vcc and GND are connected to the power supply.

  4. PIC Microcontroller to Buzzer/Alarm:

  5. An output pin of the PIC microcontroller is connected to the buzzer, which will be activated when gas is detected.

  6. PIC Microcontroller to Relay Module (Optional):

  7. An output pin can be connected to a relay module that controls a fan or other external safety devices.

  8. PIC Microcontroller to LCD Display:

  9. The microcontroller’s output pins (data and control pins) are connected to the corresponding pins of the LCD display to show the gas concentration and system status.

  10. Power Supply:

  11. The entire system, including the microcontroller, sensor, and alarm, is powered by a suitable DC power supply, typically 5V.


Working Principle


  1. Gas Detection:

  2. The gas sensor (e.g., MQ-2) detects the concentration of gas in the air and produces an analog voltage output proportional to the gas concentration. The higher the concentration, the higher the output voltage.

  3. ADC Conversion:

  4. The PIC microcontroller reads the analog output from the gas sensor using its ADC (Analog-to-Digital Converter). The ADC converts the analog signal into a digital value that the microcontroller can process.

  5. Threshold Comparison:

  6. The microcontroller compares the digital value against a predefined threshold. If the gas concentration exceeds the threshold, it indicates a gas leak.

  7. Alarm Activation:

  8. If a gas leak is detected, the microcontroller activates the buzzer to alert the user. Additionally, a relay can be activated to turn on a fan or another safety device.

  9. Display:

  10. The LCD displays the gas concentration level and the system status (e.g., “Gas Detected” or “Safe”).





Programming the PIC Microcontroller


The microcontroller is programmed using Embedded C or Assembly language. Below is an example of a basic code snippet in Embedded C for this project:

#include <pic16f877a.h>
#include <lcd.h> // Assumed LCD library for easy interfacing
#define GAS_SENSOR_CHANNEL 0 // AN0
#define BUZZER_PIN RC0
#define THRESHOLD 200 // Example threshold value for gas concentration
// Function prototypes
void init_system();
unsigned int read_adc(unsigned char channel);
void check_gas_level(unsigned int gas_value);
void delay(unsigned int ms);
void main() {
    unsigned int gas_value = 0;
    
    init_system();
    
    while(1) {
        gas_value = read_adc(GAS_SENSOR_CHANNEL);
        check_gas_level(gas_value);
        
        lcd_clear();
        lcd_set_cursor(1,1);
        lcd_print("Gas Level:");
        lcd_print_int(gas_value);
        
        delay(1000);
    }
}
void init_system() {
    TRISC = 0xFE; // Set RC0 as output for buzzer
    BUZZER_PIN = 0; // Turn off buzzer initially
    ADCON1 = 0x80; // Configure ADC
    lcd_init(); // Initialize LCD
}
unsigned int read_adc(unsigned char channel) {
    ADCON0 = (channel << 3) | 0x01; // Select ADC channel and enable ADC
    ADCON0 |= 0x02; // Start conversion
    while(ADCON0 & 0x02); // Wait for conversion to complete
    return (ADRESH << 8) | ADRESL; // Return 10-bit ADC value
}
void check_gas_level(unsigned int gas_value) {
    if (gas_value > THRESHOLD) {
        BUZZER_PIN = 1; // Turn on buzzer
        lcd_set_cursor(2,1);
        lcd_print("Gas Detected!");
    } else {
        BUZZER_PIN = 0; // Turn off buzzer
    }
}
void delay(unsigned int ms) {
    unsigned int i, j;
    for(i = 0; i < ms; i++)
        for(j = 0; j < 1275; j++);
}




Explanation of the Code


  • TRISC: Configures the necessary pins as input or output. In this case, RC0 is used to control the buzzer.

  • ADCON1: Configures the ADC module for proper operation.

  • ADCON0: Selects the ADC channel and starts the conversion process.

  • read_adc(): Reads the analog value from the specified ADC channel and returns the 10-bit digital value.

  • check_gas_level(): Compares the gas concentration value with the predefined threshold and activates the buzzer if the threshold is exceeded.

  • BUZZER_PIN: Represents the output pin connected to the buzzer, which alerts the user in case of a gas leak.


Advantages of a Gas Leakage Detection System


  • Safety: Provides early warning of gas leaks, reducing the risk of explosions or poisoning.

  • Real-time Monitoring: Continuously monitors the environment for gas leaks, ensuring ongoing safety.

  • Scalability: Can be expanded to monitor multiple gases or integrated with other safety systems.

  • Ease of Use: The system is easy to set up and provides clear alerts and feedback to the user.


Conclusion


A gas leakage detection system using a PIC microcontroller is a vital safety project that can be implemented in various environments where gas leaks pose a risk. By following the steps outlined in this article, you can build your own gas detection system, ensuring safety and peace of mind. This project demonstrates the practical application of microcontrollers in safety systems and provides a foundation for more advanced detection and alert systems.


Want us to guide through your project or make the project for you ?






Create Various Projects

Check out our Free Arduino Projects Playlist - Arduino Projects 

Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects 

Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects 

Check out our Free IoT Projects Playlist - IoT Projects 

Check out our Free Home Automation Projects Playlist - Home Automation Projects 

Check out our Free NodeMCu Projects Playlist - NodeMCu Projects 



38 views0 comments

Related Posts

See All

Comments


bottom of page