LPG Gas Sensor Interfacing with 8051

In our previous tutorial, we have seen a Rain sensor interfacing with 8051. In this tutorial, we are going to see LPG Gas Sensor Interfacing with 8051.

Prerequisites

Before starting this tutorial we should know the below topics. If you know already, please go ahead.

Components Required

Introduction

There are many gases available in the environment. Like that there are many sensors available to detect particular gas. In this tutorial, we are going to detect LPG gas. So we need to use the MQ-2 sensor. Before that, we will see the sensors available to detect gas.

Features

  • Operating Voltage is +5V
  • Can be used to Measure or detect LPG, Alcohol, Propane, Hydrogen, CO, and even methane
  • Analog output voltage: 0V to 5V
  • Digital Output Voltage: 0V or 5V (TTL Logic)
  • Preheat duration 20 seconds
  • Can be used as a digital or analog sensor
  • The PCB of this electronic circuit has a potentiometer. The Sensitivity of the Digital pin can be varied using the potentiometer

Sensors available to detect different Gases

Sensor Name Gas to measure
MQ-2 Methane, Butane, LPG, Smoke
MQ-3 Alcohol, Ethanol, Smoke
MQ-4 Methane, CNG Gas
MQ-5 Natural gas, LPG
MQ-6 LPG, butane
MQ-7 Carbon Monoxide
MQ-8 Hydrogen Gas
MQ-9 Carbon Monoxide, flammable gasses
MQ131 Ozone
MQ135 Air Quality
MQ136 Hydrogen Sulphide gas
MQ137 Ammonia
MQ138 Benzene, Toluene, Alcohol, Propane, Formaldehyde gas, Hydrogen
MQ214 Methane, Natural Gas
MQ216 Natural gas, Coal Gas
MQ303A Alcohol, Ethanol, smoke
MQ306A LPG, butane
MQ307A Carbon Monoxide
MQ309A Carbon Monoxide, flammable gas

Selecting between Sensor type and a module type

When it comes to measuring or detecting a particular Gas the MQ series Gas sensors are the most commonly used ones. These sensors can either be purchased as a module or as just the sensor alone. If you are trying to only detect (not measuring ppm) the presence of gas then you can buy it as a module since it comes with an op-amp comparator and a digital out pin. But if you planning to measure the ppm of a gas it is recommended to buy the sensor alone (without a module).

Working with Gas Sensor

Using an MQ sensor it detects a gas is very easy. You can either use the digital pin or the analog pin to accomplish this. Simply power the module with 5V and you should notice the power LED on the module to glow and when no gas it detected the output LED will remain turned off meaning the digital output pin will be 0V (LOW). Remember that these sensors have to be kept on for pre-heating time (mentioned in features above) before you can actually work with it. Now, introduce the sensor to the gas you want to detect and you should see the output LED to go HIGH along with the digital pin, if not use the potentiometer until the output gets high. Now every time your sensor gets introduced to this gas at this particular concentration the digital pin will go high (5V) else will remain low (0V).

You can also use the analog pin to achieve the same thing (We have not covered this part in this tutorial). Read the analog values (0-5V) using a microcontroller, this value will be directly proportional to the concentration of the gas to which the sensor detects. You can experiment with these values and check how the sensor reacts to different concentrations of gas and develop your program accordingly.

LPG Gas Sensor Interfacing with 8051

Connection

LPG Gas Sensor

  • Vcc – 5v
  • GND – Ground
  • DO – P1.0

LCD

  • RS – P2.0
  • RW – P2.1
  • EN – P2.2
  • Data Lines – P3.0 – P3.7

LPG Gas Sensor Interfacing with 8051

Source Code

If it is detecting LPG gas in front of this sensor, LCD will display “Gas Detected”.

/* Gas Sensor Interfacing with 8051
 * Done by EmbeTronicX
*/

#include<reg51.h>

#define lcd P3
 
sbit GAS=P1^0;
 
sbit rs=P2^0; //register select
sbit rw=P2^1; //RW
sbit en=P2^2; //enable
 
void lcd_init();
void cmd(unsigned char);
void dat(unsigned char);
void delay();
void lcd_string(char *s);
 
void main()
{
    lcd_init();
    lcd_string("  EmbeTronicX   ");
    while(1) {
        if(GAS) {
            cmd(0xc0);
            lcd_string("Gas Detected");
            delay();
        } else {
            cmd(0xc0);
            lcd_string("                ");
        }
    }
}
 
void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x06);
    cmd(0x01);
    cmd(0x80);
}
 
void cmd(unsigned char a)
{
    lcd=a;
    rs=0;
    rw=0;
    en=1;
    delay();
    en=0;
}
 
void dat(unsigned char b)
{
    lcd=b;
    rs=1;
    rw=0;
    en=1;
    delay();
    en=0;
}
 
void lcd_string(char *s)
{
    while(*s) {
       dat(*s++);
     }
}
 
void delay()
{
    unsigned int i;
    for(i=0;i<20000;i++);
}

Applications

  • Detects or measure Gases like LPG, Alcohol, Propane, Hydrogen, CO, and even methane
  • Air quality monitor
  • Gas leak alarm
  • Safety standard maintenance
  • Maintaining environmental standards  in hospitals

NOTE: if you are planning to measure the ppm of gas then you should take the analog output from the AO pin and need to convert that analog value to digital value and display that using ADC.

In our next tutorial, we will see how to transmit the data without wire using Xbee and 8051.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Table of Contents