Gas Detection and PPM Measurement using PIC Microcontroller and MQ Gas Sensors

Gas Detection

MQ series Gas sensors are very common types of sensors used in Gas Detectors to detect or measure certain types of Gases. These sensors are widely used in all Gas related devices like from simple Smoke Detectors to Industrial Air Quality Monitors. We have already used these MQ gas sensors with Arduino to measure some harmful gases like Ammonia. In this article, we will learn how to use these gas sensors with PIC Microcontrollers, to measure the PPM value of the gas and display it on a 16×2 LCD.Gas Detection

As mentioned earlier, there are different kinds of MQ series sensors available in the market and each sensor can measure different types of gases as shown in the table below. For the sake of this article, we will be using the MQ6 Gas sensor with PIC that can be used to detect the LPG gas presence and concentration. However, by using the same hardware and firmware other MQ series sensors can also be used without major modification in the code and hardware part.

Sensor Detects
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 gas
MQ-7 Carbon Monoxide
MQ-8 Hydrogen Gas
MQ-9 Carbon Monoxide, flammable gasses.
MQ131 Ozone
MQ135 Air Quality (Benzene, Alcohol, smoke)
MQ136 Hydrogen Sulfide gas
MQ137 Ammonia
MQ138 Benzene, Toluene, Alcohol, Acetone, Propane, Formaldehyde gas, Hydrogen
MQ214 Methane, Natural gas
MQ216 Natural gas, Coal gas
MQ303A Alcohol, Ethanol, smoke
MQ306A LPG, butane gas
MQ307A Carbon Monoxide
MQ309A Carbon Monoxide, flammable gasses
MG811 Carbon Dioxide (CO2)
AQ-104 Air quality

MQ6 Gas Sensor

The below image shows the MQ6 sensor pin diagram. However, the left image is a module-based MQ6 sensor for interfacing with the microcontroller unit, the pin diagram of the module is also shown in that image.

MQ6 Gas SensorPin 1 is VCC, Pin 2 is the GND, Pin 3 is the Digital out (Logic low when gas is detected.) and Pin 4 is the Analog output. The pot is used to adjust the sensitivity. It is not RL. The RL resistor is the right resistor of the DOUT LED.MQ6 Gas Sensor 1

Each MQ series sensor has a heating element and a sensing resistance. Depending on the concentration of the gas, the sensing resistance gets changed and by detecting the changing resistance, the gas concentration can be measured. To measure the gas concentration in PPM all MQ sensors provide a logarithmic graph which is very important. The graph provides an overview of the gas concentration with the ratio of RS and RO.

How to measure PPM using MQ Gas sensors?

The RS is the sense resistance during the presence of a particular gas whereas the RO is the sense resistance in clean air without any particular gas. The below logarithmic graph taken from the datasheet provides an overview of the gas concentration with the sense resistance of the MQ6 sensor. The MQ6 sensor is used to detect LPG gas concentration. Therefore, the MQ6 sensor will provide a particular resistance during the clean air condition where the LPG gas is unavailable. Also, the resistance will change whenever the LPG gas is detected by the MQ6 sensor.measure PPM using MQ Gas sensors

So, we need to plot this graph into our firmware similar to what we did in our Arduino Gas detector Project. The formula is to have 3 different data points. The first two data points are the starting of the LPG curve, in X and Y coordinates. The third data is the slope.

So, If we select the deep blue curve which is the LPG curve, the starting of the curve in X and Y coordinate is the 200 and 2. So, the first data point from the logarithmic scale is (log200, log2) which is (2.3, 0.30).

Let’s make it as, X1 and Y1 = (2.3, 0.30). The ending of the curve is the second data point. By the same process described above, X2 and Y2 are (log 10000, log0.4). Thus, X2 and Y2 = (4, -0.40). To get the slope of the curve, the formula is

=(-0.40 - 0.30) / (4 - 2.3)
= (-0.70) / (1.7)
= -0.41

The graph we need can be given as

LPG_Curve = {starting X and starting Y, slope}

 LPG_Curve = {2.3, 0.30, -0.41}

For other MQ sensors, get the above data from the datasheet and the Logarithmic graph plot. The value will differ based on the sensor and gas measured. For this particular module, it has a digital pin that only provides information about gas present or not. For this project, it is also used.

Required components

The required components for interfacing MQ sensor with PIC microcontroller are given below-

  1. 5V power supply
  2. Breadboard
  3. 4.7k resistor
  4. LCD 16×2
  5. 1k resistor
  6. 20Mhz crystal
  7. 33pF capacitor – 2pcs
  8. PIC16F877A microcontroller
  9. MQ series sensor
  10. Berg and other hookup wires.

SchematicSchematic 1

The schematic for this Gas sensor with a PIC project is pretty straight forward. The Analog pin is connected with the RA0 and the digital one with the RD5 to measure the analog voltage provided by the Gas sensor module. If you are completely new to PIC, then you might want to look into PIC ADC tutorial and PIC LCD tutorial to better understand this project.

The circuit is constructed in a breadboard. Once the connections were completed, my set-up looks like this, shown below.MQ Sensor

The main part of this code is the main function and other associated peripheral functions. The Complete program can be found at the bottom of this page, the important code snippets are explained as follows

The below function is used for getting the sensor resistance value in free air. As the Analog channel 0 is used, it is getting data from the analog channel 0. This is for calibrating the MQ Gas sensor.

float SensorCalibration(){
  int count;                                              // This function will calibrate the sensor in free air
  float val=0;
  for (count=0;count<50;count++) {                   //take multiple samples and calculate the average value
                val += calculate_resistance(ADC_Read(0));
  val = val/50;                         
  val = val/RO_VALUE_CLEAN_AIR;                              //divided by RO_CLEAN_AIR_FACTOR yields the Ro                                                                     
  return val;

Below Function is used to read the MQ sensor analog values and average it to calculate the Rs value

float read_MQ()
  int count;
  float rs=0;
  for (count=0;count<5;count++) { // take multiple readings and average it.
                rs += calculate_resistance(ADC_Read(0));   // rs changes according to gas concentration.
  rs = rs/5;

  return rs;  

The below function is used to calculate the resistance from the voltage divider resistor and the load resistance.

float calculate_resistance(int adc_channel)
{                                                               // sensor and load resistor forms a voltage divider. so using analog value and load value
  return ( ((float)RL_VALUE*(1023-adc_channel)/adc_channel));            // we will find sensor resistor.

The RL_VALUE is defined at the starting of the code like shown below

#define RL_VALUE (10)    //define the load resistance on the board, in kilo-ohms

Change this value after checking the onboard load resistance. It can be different in other MQ sensor boards. To plot the available data into the log scale, the below function is used.

int gas_plot_log_scale(float rs_ro_ratio, float *curve)
                return pow(10,(((log(rs_ro_ratio)-curve[1])/curve[2]) + curve[0]));  

The curve is the LPG curve defined in above of the code that is previously calculated in our article above.

float ​MQ6_curve[3]  = {2.3,0.30,-0.41};   //Graph Plot, change this for particular sensor

Finally, the main function inside which we measure the analog value, calculate the PPM and display it on the LCD is given below

void main() {  
                Ro = SensorCalibration();
                lcd_puts("Done!                   ");
                lcd_com(FIRST_LINE);                 lcd_print_number(Ro);
                lcd_puts(" K Ohms");
                gas_detect = 0;
                if(gas_detect == 0){              
                lcd_puts("Gas is present  ");
                lcd_puts ("Gas ppm = ");
                float rs = read_MQ();
                float ratio = rs/Ro;
                lcd_print_number(gas_plot_log_scale(ratio, MQ6_curve));
                lcd_puts("Gas not present  ");

First, the sensor’s RO is measured in clean air. Then the digital pin is read to check whether the gas is present or not. If the gas is present, the gas is measured by the provided LPG curve.

I have used a lighter to check if the PPM value is changing when the gas is detected. These cigar lighters have LPG gas inside them, which when released in the air will be read by our sensor and the PPM value on the LCD changes as shown below.PPM Measurement

The complete working can be found in the video given at the bottom of this page. If you have any questions please leave them in the comment section, or use our forums for other technical questions.


Leave a Comment

Your email address will not be published. Required fields are marked *