Iot Gas Leakage Detector and Weighing Cell
by Wilbert Soenarjo in Circuits > Arduino
1241 Views, 0 Favorites, 0 Comments
Iot Gas Leakage Detector and Weighing Cell
This project will be useful to prevent an accident caused by gas leakage. This project has an additional feature, weighing cell for gas tube. This project consists of 2 parts, which is gas leakage detector and weighing cell, all connected to an IoT.
Gas detector will detect gas while active, and if there is any gas detected, buzzer in the system will ring, alerting user while the system also sends an alert to user's email and BLYNK app. Meanwhile, 16x2 LCD will be the user interface for the system, showing percent of gas leaked. User can also monitor the system using BLYNK app as long as the system is connected to a WiFi. Gas detector is on step 5 - 9.
Weighing cell will show the weight of the gas cylinder. Using ESP8266 and BYLNK application, this weighing tool is connected to the internet. User will be able to check the gas weight from their personal mobile devices, and be alerted when the gas is almost finished and needed to be changed.
Supplies
ESP8266 Nodemcu (1x)
MQ - 2 gas sensor (1x)
HX711 (1x)
Load Cell 10kg (1x)
Breadboard (2x)
16x2 I2C LCD (1x)
LED (1x)
Buzzer (1x)
Resistor 100 ohm (2x)
Jumper wires (sufficiently)
Preparation
In this project, we will be using 2 software, which is Arduino IDE and Blynk app.
We start with the first software. The Arduino Integrated Development Environment (IDE) is a cross-platform application that is written in functions from C and C++. It is used to write and upload programs to Arduino compatible boards, but also, with the help of third-party cores, other vendor development boards.
Secondly, we need to understand Blynk. Blynk is a hardware-agnostic IoT platform with white-label mobile apps, private clouds, device management, data analytics, and machine learning. You can download the software from your mobile store app.
Arduino IDE Preparation for ESP8266 Nodemcu
First of all, we need to download Arduino IDE. Here is the link : https://www.arduino.cc/en/software/
After downloading the software, we need to install and include a few library.
ESP8266 nodemcu library : https://github.com/esp8266/Arduino
I2C LCD library : https://github.com/fdebrabander/Arduino-LiquidCrys...
Include both of the library downloaded with example from the instruction picture number 1. After including the library, we need to select nodemcu board. Please refer to instruction picture number 2.
For the weighing scale, additional libraries are needed to successfully run.
HX711 library: https://circuits4you.com/wp-content/uploads/2016/1...
Blynk Preparation for Arduino IDE
Download Blynk library in Arduino IDE. Please refer to the pictures of how to install the library. For the Blynk version, select the newest version of available library.
Blynk App Preparation
Download Blynk app from any mobile store. After installing the app, create an account and then login.
Create new project and name it. Select ESP8266 as the device.
Flow Chart and Block Diagram
As you can see in the flow chart, first thing to do is to connect ESP8266 to WiFi. It is essential because Blynk's server is taking real time data from the gas detector. After that, the system start to detect gas. If there is not any gas detected (below 50%), then LCD will print "system is fine". The system will recognize two gas threat. The first one as you can see if there is a 50% gas detected, LCD will print "detecting gas", alerting the user that there is a minor gas leak. And if there is a 70% gas detected, then LCD will print "danger". Keep in mind that these data is sent to Blynk in real time and Blynk will print the same thing as physical LCD.
Wiring Schematic
The first picture is the wiring of gas detector. First of all, I want to explain how the MQ - 2 gas sensor works. It is a Metal Oxide Semiconductor (MOS) type Gas Sensor also known as Chemiresistors as the detection is based upon change of resistance of the sensing material when the Gas comes in contact with the material. Using a simple voltage divider network, concentrations of gas can be detected. MQ2 Gas sensor works on 5V DC and draws around 800mW.
It can detect LPG, Smoke, Alcohol, Propane, Hydrogen, Methane and Carbon Monoxide concentrations anywhere from 200 to 10000 ppm. When tin dioxide (semiconductor particles) is heated in air at high temperature, oxygen is adsorbed on the surface. In clean air, donor electrons in tin dioxide are attracted toward oxygen which is adsorbed on the surface of the sensing material. This prevents electric current flow.
In the presence of reducing gases, the surface density of adsorbed oxygen decreases as it reacts with the reducing gases. Electrons are then released into the tin dioxide, allowing current to flow freely through the sensor. In this case, we use analog output to measure the gas. The analog output voltage provided by the sensor changes in proportional to the concentration of smoke/gas. The greater the gas concentration, the higher is the output voltage; while lesser gas concentration results in low output voltage.
Back to the wiring, we use each 100 ohm of resistor for the LED and buzzer. We connect SDA and SCL as D2 and D1 because of the coding.
Interfacing With Blynk
Now we need to set interface from Blynk mobile app. Select add widget box, and then add LCD, email, and app notification. The result will be like picture number 2.
Next, we need to change a few settings in LCD interface. Click the LCD interface and set the options up, shown in pictures number 3. Feel free to choose the input but in this case we use V0 as an example. You will notice that there is two options which is simple and advanced. Advanced mode in more easy to code and easy to understand.
For the scale, you would need a gauge and a value display. It will look like the 5th picture.
Notes : we need to acquire auth token (unique token that identifies each of the projects). Auth token is used in the Arduino code. Press project settings (beside add widget button). You'll see at left bottom of the interface there is a email all button. Press it and you will get an auth token sent to your email.
The Code
First of all, we need to include code library. You'll notice WidgetLCD vlcd(V0); code, we use vlcd (virtual LCD) so it can differ the physical LCD and Blynk LCD. Virtual LCD is Blynk LCD.
#include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #include <Blynk.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> WidgetLCD vlcd(V0); LiquidCrystal_I2C lcd(0x27, 16, 2);
Next is we need to define char auth, ssid, and pass. Auth is the token sent to your email. SSID and pass is your connected WiFi SSID and password. Value is the analog output from the sensor and percent is the amount of percent that will be printed to LCD.
char auth[] = "auth"; char ssid[] = "ssid"; char pass[] = "pass"; int led = D6; int buzzer = D5; int sensor = A0; int value; String percent;
The next step is setup. Wire.begin is the code used for defining SDA and SCL slot in the microcontroller. In this case, we use D2 , D1 which means SDA at D2 and SCL at D1.
void setup() { Blynk.begin(auth, ssid, pass); Wire.begin(D2,D1); lcd.begin(); lcd.backlight(); pinMode(led,OUTPUT); pinMode(buzzer,OUTPUT); pinMode(sensor,INPUT); Serial.begin(9600); }
This is the main code. The purpose of this code is to set threshold to the MQ - 2 gas sensor, so if the value exceeds the threshold, several things will happen such as buzzer rings and LED lights up. Percent code also make the LCD print the percent of gas leaking. If the value exceeds 50%, LCD will print "Detecting gas", if the value exceeds 75%, then LCD will print "Danger".
void gasmode() { value = analogRead(sensor); Serial.println(value); percent = " Gas : "; percent += ((float)value / 427 * 100); percent += "%"; if(value > 315){ lcd.clear(); lcd.setCursor(4,0); lcd.print("Danger"); tone(buzzer, 1000); digitalWrite(led,HIGH); lcd.setCursor(1,1); lcd.print(percent); delay(960); } else if(value > 215){ lcd.clear(); lcd.setCursor(2,0); lcd.print("Gas detected"); tone(buzzer, 1000); digitalWrite(led,HIGH); lcd.setCursor(1,1); lcd.print(percent); delay(960); } else { lcd.clear(); lcd.setCursor(0,0); lcd.print("IoT Gas Detector"); noTone(buzzer); digitalWrite(led,LOW); lcd.setCursor(1,1); lcd.print("System is fine"); delay(960); } }
This is the Blynk IoT code. We use vlcd so that the code doesn't overlapse between physical and virtual (Blynk) LCD. We use almost the same code as the gasmode code because we want our IoT LCD to print the exact same thing that our physical LCD prints. Blynk.email is used for sending email, meanwhile Blynk.notify is used for sending notifications to phone.
void iot(){ vlcd.clear(); Blynk.virtualWrite(V0, percent); vlcd.print(0,0, "IoT Gas Detector"); if(value > 315){ vlcd.clear(); vlcd.print(4,0, "Danger"); vlcd.print(0,1, percent); Blynk.email("wilbertsoenarjoo@gmail.com", "IoT Gas Leakage Alert", "Gas leakage over 70%, Danger! "); Blynk.notify("IoT Gas Leakage Alert - Gas leakage over 70%, Danger! "); } else if(value > 215){ vlcd.clear(); vlcd.print(2,0, "Gas detected"); vlcd.print(0,1, percent); Blynk.email("wilbertsoenarjoo@gmail.com", "IoT Gas Leakage Alert", "Gas leakage over 50%, Detecting gas! "); Blynk.notify("IoT Gas Leakage Alert - Gas leakage over 50%, Detecting gas! "); } if(value < 215){ vlcd.clear(); vlcd.print(0,0, "IoT Gas Detector"); vlcd.print(0,1,"System is fine"); } delay(100); }
Lastly, we loop our gasmode and iot code to keep the system running. Blynk.run is used for activating the Blynk app.
void loop(){ gasmode(); iot(); Blynk.run(); }
Calibrating the Scale
#include "HX711.h" //HX711.h is not initially installed, please follow step number 2. #define DOUT D5 #define CLK D6 HX711 scale; float calibration_factor = -112557;
This is used to include and define what's needed for the calibration. Calibration_factor varies between projects. You will be able to get new calibration factors every calibration.
void setup() { Serial.begin(9600); Serial.println("HX711 Calibration"); Serial.println("Remove all weight from scale"); Serial.println("After readings begin, place known weight on scale"); Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively"); Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively"); Serial.println("Press t for tare"); scale.set_scale(); scale.tare(); long zero_factor = scale.read_average(); Serial.print("Zero factor: "); Serial.println(zero_factor); }<br>
This code above is the setup stage for the scale calibration.
void loop() { scale.set_scale(calibration_factor); Serial.print("Reading: "); Serial.print(scale.get_units(), 3); Serial.print(" kg"); Serial.print(" calibration_factor: "); Serial.print(calibration_factor); Serial.println(); if(Serial.available()) { char temp = Serial.read(); if(temp == '+' || temp == 'a') calibration_factor += 10; else if(temp == '-' || temp == 'z') calibration_factor -= 10; else if(temp == 's') calibration_factor += 100; else if(temp == 'x') calibration_factor -= 100; else if(temp == 'd') calibration_factor += 1000; else if(temp == 'c') calibration_factor -= 1000; else if(temp == 'f') calibration_factor += 10000; else if(temp == 'v') calibration_factor -= 10000; else if(temp == 't') scale.tare(); } }
The Code Pt 2 (Scale)
#include <ESP8266WiFi.h> #include "HX711.h" #define BLYNK_PRINT Serial #include <Blynk.h> #include <BlynkSimpleEsp8266.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2);
The codes above are used to set the base of our coding. It includes the library needed and define what's needed for the program. The next part is define the SSID, password, and authentication code for this system. It is defined with this coding below
const char *ssid = "Raspati"; // change the one inside the quotation mark with your personal wifi ssid const char *pass = "qwerty123"; //change the one inside the quotation mark with your personal wifi password char auth[] = "12345asdf"; // the auth token will be obtainable via BLYNK. WiFiClient client; HX711 scale; int rbutton = D4; // this button will be used to reset the scale to 0. float weight; float calibration_factor = -101225; // this is received from the previous step (step 9)
Next step is to initialize the system
void setup() { Serial.begin(115200); pinMode(rbutton, INPUT_PULLUP); scale.set_scale(); scale.tare(); //Reset the scale to 0 long zero_factor = scale.read_average(); //Get a baseline reading <br>
this code below is to start the start the wires for the BLYNK, wires of the LCD, and the LCD itself.
Blynk.begin(auth, ssid, pass); Wire.begin(D2, D1); lcd.begin();
Connect to wifi, and see if the system is connected to the wifi.
lcd.print("Connecting Wifi"); WiFi.begin(ssid, pass); { delay(1000); Serial.print("."); lcd.clear(); } Serial.println(""); Serial.println("WiFi connected"); lcd.clear(); lcd.print("WiFi connected"); delay(2000); }
Now that the system is connected to the WiFi, we can proceed to go to the weighing scale
void loop() { Blynk.run(); scale.set_scale(-101225); <br> weight = scale.get_units(5); lcd.setCursor(0, 0); lcd.print("Measured Weight"); lcd.setCursor(0, 1); lcd.print(weight); lcd.print(" KG "); Blynk.virtualWrite(V3, weight); delay(2000); lcd.clear(); Serial.print("Weight: "); Serial.print(weight); Serial.println(" KG"); Serial.println(); if ( digitalRead(rbutton) == LOW) { scale.set_scale(); scale.tare(); //Reset the scale to 0 } }
The Product
Demonstration video of IoT gas leakage detector and weighing scale.