Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
60 min
Share

Measure the LPG gas concentration using the Arduino

LPG is an important energy source that is widely used around the world for a variety of applications, including heating and cooking. However, if a leak occurs, it can cause a catastrophic fire unless the leak is detected as soon as possible. In this tutorial, we will learn how to make an LPG gas detector using an MQ-2 gas sensor.

Project Video

Overview

In this tutorial, we will learn how to use the MQ-2 gas sensor with Arduino to build a detector for LPG, smoke, and CO gas. We will display the concentration of these gases on a TFT display and use a buzzer to sound an alarm if the concentration exceeds a certain value, which is written in the code. You can use the same project in order to give a warning in the event of a LPG leak in your kitchen.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
2.8 inch Touch Screen Module
Get Item
Combustible Gas Sensor (MQ-2)
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Passive Buzzer – 5V
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

It is important to connect resistors with a value of 10 kilo ohms to the pins of the TFT Display, as shown in the image.

Connect the wires between the TFT Display and the Arduino and the MQ-2 gas sensor as shown in the image below.

Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line


Connections from the MQ-2 gas sensor:

• MQ-2  gas sensor VCC pin → Breadboard 5V line

• MQ-2  gas sensor GND pin → Breadboard ground line

• MQ-2 gas sensor A0 pin → Arduino pin A0


Connections from the passive buzzer:

•passive buzzer GND pin → Breadboard ground line

•passive buzzer VCC pin (+ pin) → Arduino pin 3


Connections from the TFT Display to the breadboard:

• TFT Display VCC pin → Breadboard 5V line

• TFT Display GND pin → Breadboard ground line

• TFT Display LED pin → Breadboard 5V line


Connections from the TFT Display to arduino :

• TFT Display CS pin → Arduino pin 10

• TFT Display RST pin → Arduino pin 8

• TFT Display DC pin → Arduino pin 9

• TFT Display MOSI pin → Arduino pin 11

• TFT Display SCK pin → Arduino pin 13

• TFT Display MISO pin → Arduino pin 12

Coding

/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:

Tutorial: Measure the LPG gas concentration using the Arduino!
Adafruit_GFX library
Adafruit_ILI9341 library
MQ2 library
wire library

The purpose of this sketch is to display the concentration of LPG, smoke, and CO gas
in ppm obtained from the MQ-2 gas sensor on the TFT Display and to sound an alarm if
the concentration exceeds a certain value using a buzzer.

Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line


Connections from the MQ-2 gas sensor:

• MQ-2  gas sensor VCC pin → Breadboard 5V line

• MQ-2  gas sensor GND pin → Breadboard ground line

• MQ-2 gas sensor A0 pin → Arduino pin A0


Connections from the passive buzzer:

•passive buzzer GND pin → Breadboard ground line

•passive buzzer VCC pin (+ pin) → Arduino pin 3


Connections from the TFT Display to the breadboard:

• TFT Display VCC pin → Breadboard 5V line

• TFT Display GND pin → Breadboard ground line

• TFT Display LED pin → Breadboard 5V line


Connections from the TFT Display to arduino :

• TFT Display CS pin → Arduino pin 10

• TFT Display RST pin → Arduino pin 8

• TFT Display DC pin → Arduino pin 9

• TFT Display MOSI pin → Arduino pin 11

• TFT Display SCK pin → Arduino pin 13

• TFT Display MISO pin → Arduino pin 12


*/

#include "Adafruit_GFX.h"    
#include "Adafruit_ILI9341.h"
#include "Wire.h"
#include  "SPI.h"
#include "MQ2.h"

//Define TFT Display pin numbers
#define TFT_DC 9              
#define TFT_CS 10            
#define TFT_RST 8            
#define TFT_MISO 12          
#define TFT_MOSI 11          
#define TFT_CLK 13          

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

#define Analog_Input    A0  //analog feed from MQ-2
#define passive_buzzer    3  //define passive buzzer pin
char LPGppmChar[10];  
char SMOKEppmChar[10];
char COppmChar[10];
MQ2 mq2(Analog_Input);
void setup(){
Serial.begin(9600);
mq2.begin();
tft.begin();                      
tft.setRotation(0);            
tft.fillScreen(ILI9341_BLACK);

pinMode(passive_buzzer,OUTPUT); //define passive buzzer pin as an output pin

Wire.begin();
printText("LPG Level", ILI9341_GREEN,20,20,3);
printText("SMOKE Level", ILI9341_GREEN,20,130,3);
printText("CO Level", ILI9341_GREEN,20,230,3);
}

void loop()
{


float* values= mq2.read(true); //set it false if you don't want to print the values in the Serial
 //lpg = values[0];
float LPGPPM = mq2.readLPG();
 //co = values[1];
float COPPM = mq2.readCO();
 //smoke = values[2];
float SMOKEPPM = mq2.readSmoke();

int grafX = 0; //int for x value of graph


String LPGppmString = String(LPGPPM,1); //Convert float to string

LPGppmString.toCharArray(LPGppmChar,10);

tft.fillRect(30,70,120,40,ILI9341_BLACK); //Draw a Rectangle

printText(LPGppmChar, ILI9341_WHITE,35,70,4); //printing the lpg gas concentration on the TFT Display

printText("ppm", ILI9341_WHITE,160,70,4);


grafX = map(LPGPPM,0,1000,0,127);           //map value to screen width

tft.fillRect(0, 112, grafX, 10, ILI9341_WHITE);  //print graph 400min 1000max


String SMOKEppmString = String((SMOKEPPM*100)/1000000,1); //Convert float to string

SMOKEppmString.toCharArray(SMOKEppmChar,10);

tft.fillRect(30,170,120,40,ILI9341_BLACK); //Draw a Rectangle

printText(SMOKEppmChar, ILI9341_WHITE,35,170,4); //printing the smoke concentration on the TFT Display

printText(" %", ILI9341_WHITE,160,170,4);



grafX = map((SMOKEPPM*100)/1000000,0,1000,0,127);      //map value to screen width

tft.fillRect(0, 212, grafX, 10, ILI9341_WHITE);  //print graph 400min 1000max


String COppmString = String(COPPM,1); //Convert float to string

COppmString.toCharArray(COppmChar,10);

tft.fillRect(30,270,120,40,ILI9341_BLACK); //Draw a Rectangle

printText(COppmChar, ILI9341_WHITE,35,270,4); //printing the co gas concentration on the TFT Display

printText("ppm", ILI9341_WHITE,160,270,4);

grafX = map(COPPM,0,1000,0,127);           //map value to screen width

tft.fillRect(0, 312, grafX, 10, ILI9341_WHITE);  //print graph 400min 1000max

if(LPGPPM>400){                             //if lpg ppm >400
   digitalWrite(passive_buzzer,HIGH);                   //turn on buzzer
}
else{                                       //if not
   digitalWrite(passive_buzzer,LOW);                    //turn off buzzer
}



}

//The function of writing words on the TFT Display
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
 tft.setCursor(x, y);
 tft.setTextColor(color);
 tft.setTextSize(textSize);
 tft.setTextWrap(true);
 tft.print(text);
}

Testing it Out

Once you’ve uploaded the code to the Arduino board, you will find that the TFT Display displays the concentration of LPG, smoke, and CO gas in ppm as shown on the image below:

Resources

No items found.