Interfacing 128 X 64 LCD Module with Arduino (3/3): MQ2 Gas/Smoke Sensor


On the first 2 tutorials of 'Interfacing 128x64 LCD with Arduino' series, we've changed the contrast of the GLCD and even played a classic game on it. For the final part of the series, we're going to introduce an analog sensor, an MQ2 smoke/gas sensor, with LCD and LEDs for text display and status indication, respectively. This project will be a great tool to detect smoke or gas in order to avoid gas leakage or fire-related accidents.

Arduino Uno IDE


Application Discussion:

A smoke and gas detector is a great addition to your home, especially if you're keen on the idea of cooking or spending time in the kitchen, let your next Arduino project be your extended sensitive nose for identifying combustible gasses with the help of the MQ2 Gas Sensor.

The 128 x 64 GLCD will serve as our output if the surrounding is normal or in a hazardous situation. Two LEDs are also connected so we have a visual indication synchronized with what is displayed on the 128 x 64 GLCD.


MQ2 Gas/Smoke Sensor

This sensor is useful for detecting the concentrations of gasses around an area, such as LPG, smoke, alcohol, propane, hydrogen, methane and carbon monoxide. This sensor contains a sensing element enclosed in a stainless steel mesh, mainly aluminum-oxide based ceramic, coated with Tin Dioxide.

The MQ2 gas sensor is a great option for you if you are planning to set up an indoor monitoring system, especially for an early fire detection system, as it is one of the most commonly used gas sensors in the MQ sensor series.


For calibrating the gas sensor, you can hold the sensor near smoke/gas you want to detect. Turn the potentiometer (blue with white pan screw) on the back of the module until the Red LED on the upper part of the potentiometer will start to glow.
By turning the screw clockwise you can increase its sensitivity or anticlockwise to decrease sensitivity.

There are four pins on the MQ2 gas/smoke sensor:

VCC, which supplies the module with power. You can attach it to your Arduino's 5V output.
GND or Ground Pin, which must be attached to the Arduino GND pin.
DO a digital representation of the presence gases.
AO has an analog output voltage proportional to the smoke/gas concentration.

ST7920 128 x 64 LCD Module

The GLCD is used to display the condition of the area, to show whether the surrounding is in normal condition or if a smoke or gas leakage has been detected. You can CLICK HERE for other projects and further identification about the pins of the 128 x 64 GLCD.

All the components mentioned above available at CreateLabz Store.


Hardware Set-Up:


Software Set-Up:

Open your Arduino IDE and upload the code on to your Arduino board.


The following is the Arduino code, you can download the code in our Github page. CLICK HERE, or copy the code below.

#include <Mq2.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(13,11,10);

int Analog_Input = A1;
int smoke, lpg;
int Greenled = 8; 
int Redled = 9; 

MQ2 mq2(Analog_Input);
int sensorThreshold = 1;
void setup(void) { Serial.begin(9600); mq2.begin(); pinMode(Analog_Input, INPUT); pinMode(Redled,OUTPUT); pinMode(Greenled,OUTPUT);
u8g.firstPage(); do { u8g.drawFrame(1,2,126,62); first(); } while( u8g.nextPage() ); delay(2000); clearLCD(); u8g.firstPage(); do { second(); } while( u8g.nextPage() ); delay(3000); clearLCD(); delay(50); } } void first(void) { u8g.setFont(u8g_font_unifont); u8g.setFont(u8g_font_osb18); u8g.drawStr( 25, 27, "Part 3:"); u8g.drawStr( 10, 52, "Tutorials"); } void second(void) { u8g.setFont(u8g_font_osb18); u8g.drawStr( 0, 18, "Interfacing"); u8g.drawStr( 0, 38, "GLCD with"); u8g.drawStr( 20, 58, "Arduino"); } void loop(void) { float =; lpg = mq2.readLPG(); smoke = mq2.readSmoke(); if (smoke == 0) { do { u8g.drawFrame(1,2,126,62); u8g.drawFrame(0,0,128,31); u8g.drawFrame(0,33,128,31); u8g.setFont(u8g_font_unifont); u8g.drawStr(40, 28, "Normal"); u8g.drawStr(10, 15, "Smoke Reading:"); Serial.println(smoke);
u8g.drawStr(40, 61, "Normal"); u8g.drawStr(20,46, "Gas Reading:"); digitalWrite(Redled, LOW); digitalWrite(Greenled, HIGH); Serial.println(lpg); } while( u8g.nextPage() ); delay(2000); u8g.firstPage(); } if (smoke > 0 && lpg > 0){ u8g.firstPage(); do { u8g.drawFrame(0,0,128,31); u8g.drawFrame(0,33,128,31); u8g.setFont(u8g_font_unifont); u8g.drawStr(40, 28, "WARNING"); u8g.drawStr( 10, 15, "Smoke Reading:"); Serial.println(smoke); u8g.drawStr(40, 61, "WARNING"); u8g.drawStr(20,46, "Gas Reading:"); Serial.println(lpg); digitalWrite(Redled, HIGH); digitalWrite(Greenled, LOW); } while( u8g.nextPage() ); } } void clearLCD(){ u8g.firstPage(); do { } while( u8g.nextPage() ); }


Code Breakdown:

We have used two libraries for this project, libraries have additional features for use in sketches, hardware or data manipulation.

  • U8glib has been used in our previous project. A graphics library with support for many different monochrome displays. 
  • MQ2 is a library specifically for the mq2 gas/smoke sensor.
    you can download the libraries mentioned in our Github Page. 

  #include <MQ2.h>
  #include "U8glib.h"

  • For setting up the pins for the ST7920 LCD module:
    Pin 13 for enable
    Pin 11 for read/write
    Pin 10 for data/construction

U8GLIB_ST7920_128X64_4X u8g(13,11,10);

  • sensorThreshold  minimum value is used since it was calibrated to minimum sensitivity.
  • MQ2 analog pin connected to analog pin 1.
  • Red and Green LEDs connected to digital pins 9 and 8, respectively.

int Analog_Input = A1; int Greenled = 8; int Redled = 9;
int sensorThreshold = 1;
  • void setup() function is called when a sketch starts. Use it to initialize variables, pin modes, and even start using libraries.
  • Serial.begin(9600) tells the Arduino to exchange messages with the serial monitor at a data rate of 9600 bits per second. 
  • pinMode() function is used to configure a specific pin to behave either as an input or an output.
pinMode(Analog_Input, INPUT);

  • u8g.firstPage() to set the text you wanted to display.
  • u8g.nextPage() to move into another text display.
  • u8g.setFont() the font of the text which is included on the library.
  • u8g.drawStr() to input characters or text on the LCD.
  • u8g.drawFrame() to divide the frame from gas and smoke detection readings.

u8g.drawStr(40, 28, "Normal");
u8g.drawStr(10, 15, "Smoke Reading:"); 


  • Void loop() another function that Arduino uses as part of its structure. The code inside the loop function will run over and over as long as the Arduino Uno is turned on.
  • Float are often used to approximate analog and continuous values 
  • set it false if you don't want to print the values in the serial monitor.
  • digitalWrite writes either HIGH or a LOW value to a digital pin. High meaning it is active or 5v while LOW means 0v. 
  • if statement checks condition and executes the following statement if the condition is 'true'.

if (smoke == 0 && lpg == 0)
if (smoke > 0 && lpg > 0)


Video Demo:

Bear in mind that values can be seen on the Serial Monitor. Preferably, rather than placing a bunch of numbers, it is easier to place text indicators such as 'normal' or 'warning' to avoid confusion.

This project was able to interface an analog sensor with Arduino and the 128 x 64 LCD. 
The project is a good addition at home for safety and security. I recommend to attach a buzzer, if you had any, for an audio representation. 


Check out other tutorials involving the 128 x 64 LCD:

Interfacing 128 x 64 LCD Module with Arduino: Potentiometer

Interfacing 128 x 64 LCD Module with Arduino: Snake Games with Push Button Switches



Sensors Modules Glcd 128x64 | Sensors Modules (

128 x 64 lcdDetectorGasGas detectorGas sensorGlcdGraphicalLcdMq2SensorSmokeSmoke detector

Leave a comment

All comments are moderated before being published