Gas Detector Using MQ2 on ESP32 and Blynk App

Make your own gas leak detector today!


The MQ-2 Gas sensor can detect or measure gasses like LPG, Alcohol, Propane, Hydrogen, CO, and even methane. With this sensor, we can build a gas leak detector to check if there is a gas leak and send a notification to our mobile phone. Watch this video to know how to make a gas detector using ESP32 and Blynk App.


This tutorial use :

Software Preparation.

  1. Open the Arduino IDE. Please make sure that your Arduino IDE is version 1.6.9 and above.
  2. Then, click at the on Sketch – Include Library – Manage Libraries… Search for “blynk” and install the latest version of Blynk.
  3. Next, we need to install ESP32 development board.
  4. Insert this link into the Arduino preference “”.
  5. Go to Tools>Boards>Board Manager and install the ESP32 board.

Sample Code

This is the sample code used for this tutorial. Try it!

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
#define MQ2 34
#define GREEN 16
#define RED 17
int sensorValue = 0;
boolean state = false;
void setup()
// Debug console
Blynk.begin(auth, ssid, pass);
pinMode(MQ2, INPUT);
pinMode(RED, OUTPUT);
timer.setInterval(1000L, sendUptime);
void sendUptime()
sensorValue = analogRead(MQ2);
Blynk.virtualWrite(V1, sensorValue);
if (sensorValue > 600)
Blynk.notify("Gas Detected!");
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
void loop()
view raw mq2_ESP32.ino hosted with ❤ by GitHub

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiries, please post at Cytron Technical Forum.

3 thoughts on “Gas Detector Using MQ2 on ESP32 and Blynk App”

  1. Is there any other way to power the node32 board? I want to install in my kitchen with a battery source. Please help

  2. Hello Admin,

    I have try this project and it very successfully function but the warning notification not display on my phone. Can help me?

    Thank you.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

micro:bit Quick Start Kit
Design 3D Model Using TinkerCAD and Print
3D Print Lithophane for Hari Raya Aidilfitri
With Raspberry Pi Imager, you can write Raspberry Pi OS and remote SSH easier
3D Printing Pelita Raya
Tutorials of Cytron Technologies Scroll to Top