MQ-2 gas sensor interfacing with pic microcontroller

MQ-2 gas sensor or  Smoke detector interfacing with pic microcontroller. How to design smoke detector circuit using pic microcontroller? In this tutorial, you will learn how to interface smoke detector. Smoke detector sensor is used to detect the presence of smoke and gas in surrounding.  It is also used to detect gas leakage detection in household and industrial equipment’s. MQ-2 smoke sensor or gas sensor is suitable for detection of Hydrogen, methane and LPG related gases. In short, it can detect combustible gases. It has a wide operating detection range. It has very high sensitivity and very fast response rate.  It can even be used in harsh environments. So you can easily use it detect the presence of gas or smoke in your surrounding. There are many smoke detector circuits available in market. But all these sensors are expensive.MQ-2 gas sensor

Working video of smoke/ gas detector with pic microcontroller

But if you want to have a gas detector circuit for your home or office in very low price, you can build it yourself using this simple MQ-2 smoke sensor. In this tutorial I will show you, how you can interface MQ-2 smoke or gas sensor with pic16f877a microcontroller. You can also use it with other microcontrollers like Arduino Uno R3 or any other. I will already posted a article on smoke detector interfacing with Arduino. You can check it below:

Applications of MQ-2 smoke sensor

It has many application but some of them are given below:

  • Domestic and industrial gas leakage detection
  • Combustible gas leakage detection

Introduction to MQ-2 gas sensor

So now lets start with the introduction of MQ-2 smoke sensor. The sensitivity material used in this sensor is SnO2.  It offers low conductivity in air when there is no gas present in air.  If there is a combustible gas presence in the air, MQ-2 gas sensor offers high conductivity. The conductivity depends on the concentration of gas presence in the air.  The sensor module we are using in this tutorial has on board electronics circuit as well which converts this change in conductivity to a output voltage of the sensor. So we will using this output voltage as reference to assure presence or absence of gas in air. I will explain it in more details in working part of this article.

Pin out of MQ-2 gas sensor

Pin out of MQ-2 gas sensor is given below:

MQ-2 gas sensor pin out

  • Connect Vcc >> 5v power supply
  • Ground >> Ground pin of power supply
  • Aout >>  Analog pin of pic microcontroller

working of MQ-2 gas sensor

As I explained earlier, MQ-2 smoke sensor offers low conductivity when no gas present in the air and it offers high conductivity when any combustible gas is present in the air. The module we are using in this tutorial as shown in above picture has on board electronics circuit which provides this conductivity in the form of voltage output. When there is no has present in the air, voltage at the output of AOUT pin will be low and when there is a gas present in the air, voltage at the AOUT pin will be high. So we will use this AOUT output pin to detect the presence of gas in air. This sensor also has digital output pin that is DOUT.  There is a on board variable resistor in this sensor which is used to set the sensitivity of sensor. If you rotate the variable resistor clock wise, it will increase the sensitivity of sensor and if you rotate the variable resistor anticlockwise it will reduce the sensitivity of sensor. We are using analog output pin in this tutorial, so we need to measure voltage across AOUT pin. So We will connect AOUT pin to analog channel of pic microcontroller. So now lets see how to interface MQ-2 sensor with pic microcontroller.

MQ-2 gas / smoke sensor interfacing with pic microcontroller

To interface this sensor with pic microcontroller, we only need to connect analog output pin of smoke sensor with analog input pin of pic microcontroller. I am using pic16f877a microcontroller in this tutorial. PIC16F877A microcontroller has built in 8 analog input channels. So We need to use only one analog channel to measure the output voltage of sensor.  Circuit diagram for interfacing with MQ-2 gas sensor with pic microcontroller is given below: MQ-2 gas sensor interfacing with pic microcontroller

LCD is connected with the PORTC of pic microcontroller and whenever gas is detected it will be displayed on LCD that gas is detected. You can also connect a buzzer with output pin of pic microcontroller. If gas is detected, buzzer or alarm will turn on. You can also add the gsm module to this project to get sms on your mobile number when gas is present in air. You may also like to check:

Code for MQ-2 gas sensor interfacing with pic microcontroller

Code for this tutorial is given below. You just need to upload this code to pic microcontroller and test your working circuit.

// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;

////// MQ-2 gas sensor interfacing with pic microcontroller ///////

int gas_value;
char text[10];
void main(void)

ADC_Init(); // it will initialize the adc module of pic16f877a microcontroller
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"MQ-2 sensor" ); // Write text in first
Lcd_Cmd(_LCD_CLEAR); // Clear display

{ // Endless loop

gas_value = ADC_Read(0); // It will read the gas value of sensor
if( gas_value > 400 )
Lcd_Out(1,1, "Gas detected" );
Lcd_Out(1,4, "No Gas " );
intToStr(gas_value, Ltrim(text));
Lcd_Out(2,1, text );


So this is all about how to interfacing smoke sensor MQ-2 with pic microcontroller. I hope you liked this tutorial.

Add Comment