SiTeGas: Sistem Pendeteksi Gas

by Vadrick in Circuits > Sensors

479 Views, 1 Favorites, 0 Comments

SiTeGas: Sistem Pendeteksi Gas

Circuit for real.jpg
Kitchen security with MQ2 Gas sensor and Rain module water sensor using ESP32

By: Muhammad Daffa Ramadhani, Tagreg Wiryo Nursalim, & Vadrick Ferdinand Fu - Computer Engineering - Binusian 2024 - Bina Nusantara University.

SiTeGas: Sistem Pendeteksi Gas is a device that notifies you whenever gas can be detected in the vicinity of the device. It has 2 features which includes gas detection and rain detection which will collect data and notifies the user in the case of any detection. For example, it will notify the user "Gas Detected" whenever the gas sensor detects some gas, and "Rain Detected" whenever the rain sensor hits water or raindrops.

For this device, the Blynk App is used which can be downloaded from the Play Store of the App Store.

Supplies

Blynk photo.jpg
Esp32.jpeg
MQ2 gas sensor.jpg
Rain Sensor module.jpg
Resistor 1k.jpg
LED 5mm.jpg

Software:

  1. Blynk
  2. Arduino IDE

Hardwares:

  1. ESP32 or NodeMCU
  2. MQ2 Gas sensor
  3. Rain sensor module
  4. 1k 1/4 watt resistors (2 Pcs)
  5. 5-mm LEDs (2 Pcs)
  6. Jumper wires.

Blynk IoT Cloud Setup

Blynk ss 1.jpeg
Blynk ss 2.jpeg
Blynk ss 3.jpeg
Blynk ss 4.jpeg
Blynk ss 5.jpeg
Blynk ss 6.jpeg
Blynk ss 7.jpeg
Blynk ss 8.jpg
Blynk ss 9.jpg
  1. Create a Blynk account and log in.
  2. Create Blynk Template and select ESP 32 as hardware.
  3. Create Datastreams in Blynk Cloud for each sensor (in this case 2 datastreams).
  4. Add 2 new events on Blynk (GAS and RAIN).
  5. Create a web dashboard, click and drag Gauge and Label widgets according to the number of sensors (2), and click save.
  6. Now add new device using the template that we just made
  7. We can add the firmware configuration in other words, the blynk authentication token from the device info to our code

Coding

Code.jpg
  1. Install Arduino IDE and open the application.
  2. Go to File => Preferences => Additional Board Manager URLs => Paste this link (https://dl.espressif.com/dl/package_esp32_index.json)
  3. Go to Tools => Board => Board Manager => Search "esp32" => Install "esp32".
  4. Go to Sketch => Include Library => Manage Libraries => Search "Blynk" => Install "Blynk".
  5. Type this code in Arduino IDE
/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""

// Your WiFi Credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

// define the GPIO connected with Sensors & LEDs
#define MQ2_SENSOR    35
#define RAIN_SENSOR   34
#define GREEN_LED     14
#define RED_LED       25
#define WIFI_LED      2

//#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;

int MQ2_SENSOR_Value = 0;
int RAIN_SENSOR_Value = 0;
bool isconnected = false;
char auth[] = BLYNK_AUTH_TOKEN;

#define VPIN_BUTTON_1    V1 
#define VPIN_BUTTON_2    V2

void checkBlynkStatus() { // called every 2 seconds by SimpleTimer
  isconnected = Blynk.connected();
  if (isconnected == true) {
    digitalWrite(WIFI_LED, HIGH);
    sendData();
    //Serial.println("Blynk Connected");
  }
  else{
    digitalWrite(WIFI_LED, LOW);
    Serial.println("Blynk Not Connected");
  }
}

void getSensorData()
{
  MQ2_SENSOR_Value = map(analogRead(MQ2_SENSOR), 0, 4095, 0, 100);
  RAIN_SENSOR_Value = digitalRead(RAIN_SENSOR);
  
  if (MQ2_SENSOR_Value > 50 ){
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(RED_LED, HIGH);
  }
  else if (RAIN_SENSOR_Value == 0 ){
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(RED_LED, HIGH);
  }
  else{
    digitalWrite(GREEN_LED, HIGH);
    digitalWrite(RED_LED, LOW);
  }
}

void sendData()
{  
  Blynk.virtualWrite(VPIN_BUTTON_1, MQ2_SENSOR_Value);
  if (MQ2_SENSOR_Value > 50 )
  {
    Blynk.logEvent("gas", "Gas Detected!");
  }
  else if (RAIN_SENSOR_Value == 0 )
  {
    Blynk.logEvent("rain", "Water Detected!");
    Blynk.virtualWrite(VPIN_BUTTON_2, "Water Detected!");
  }
  else if (RAIN_SENSOR_Value == 1 )
  {
    Blynk.virtualWrite(VPIN_BUTTON_2, "No Water Detected.");
  } 
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(MQ2_SENSOR, INPUT);
  pinMode(RAIN_SENSOR, INPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(WIFI_LED, OUTPUT);

  digitalWrite(GREEN_LED, LOW);
  digitalWrite(RED_LED, LOW);
  digitalWrite(WIFI_LED, LOW);

  WiFi.begin(ssid, pass);
  timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
  Blynk.config(auth);
  delay(1000);
}

void loop()
{
  getSensorData();
  Blynk.run();
  timer.run();
}

Setup Mobile Dashboard in Blynk IoT App

App.jpg
  1. Download the Blynk App on Play Store or App Store.
  2. Log in to your account.
  3. Open the Blynk IoT app, and select the Template.
  4. Tap on the 3-dash icon and add 1 Gauge & 1 Value Display from the widget Box.
  5. Tap on the Gauge widget and select the Datastream.
  6. Tap on the Value Display widget and select the related Datastream. Select Front Size as AUTO.
  7. Then exit from the EDIT mode.

Block Diagram

Block Diagram.jpeg

This is the block diagram of SiTeGas: Sistem Pendeteksi Gas.

Flow Chart

Flow Chart.jpeg

This is the flow chart of SiTeGas: Sistem Pendeteksi Gas.

Schematic

Circuit diagram.png

This is the schematic of SiTeGas: Sistem Pendeteksi Gas.

Assembly

Circuit for real.jpg
Final result.jpg
  1. Connect 5V and GND from Power Supply to a breadboard.
  2. Connect the MQ-2 Gas sensor analog output to the GPIO D35, the Vcc to the Vin pin and to the (+) on the breadboard, and the GND to the GND pin and to the (-) on the breadboard.
  3. Connect the rain sensor module’s digital output to the GPIO D34, the Vcc to the Vin pin and to the (+) on the breadboard, and the GND to the GND pin and to the (-) on the breadboard.
  4. The Indicator LEDs are connected with GPIO D14 & D25, and to 1k resistors connected to the GND pin and to the (-) on the breadboard.
  5. The ESP32 board LED connected with GPIO D2 is used as the Wi-Fi indicator.
  6. Make a casing.