Program Sensor Gas MQ-4 Methana for Arduino Project

Program Sensor Gas MQ-4 Methana for Arduino Project

Sensor gas MQ-4
SENSOR GAS MQ-4

Program sensor Gas MQ-4 Methana for Arduino Project - Di postingan kali ini saya akan mengenalkan salah satu Sensor Gas yang bisa di aplikasikan dengan Arduino dan sensor ini bisa jadi bahan penelitian untuk mengukur zat / kadar Gas yang terkandung di dalam suatu ruangan. menggunakan Sensor Gas Methane berserta dengan pemprogramannya .

Sensor Gas MQ-4 adalah komponen elektronika yang dapat mendeteksi kadar gas alam terkompresi / CNG (compressed natural gas) yang utamanya mengandung gas metana (methaneCH4) yang merupakan bentuk zat paling sederhana dari hidrokarbon. Walaupun tidak bersifat racun, gas metana dapat berbahaya karena mudah terbakar (combustive / flammable gas). Gas ini tidak berbau dan tidak berwarna, menjadikannya sulit untuk dideteksi secara langsung oleh manusia.





Berikut Sketch sederhana-nya :

Int SensorValue;
void setup() {
Serial.begin(9600); //Seting serial port ke 9600
pinMode(A0, INPUT);
}
void loop() {
SensorValue = AnalogRead(A0); //Membaca analog input 0
Serial.println(SensorValue, DEC); // Menampilkan Value dalam DECIMAL
delay(100); // Jeda 100ms
}



Program Lengkap Sensor Gas MQ-4 Methana Gas Sensor ke dalam satuan PPM
Berikut Sketch Untuk merasi Resistansi R0 :


/*
 Arduino MQ4 gas sensor - Geekstips.com
 This example is for calculating R0 which is
 the resistance of the sensor at a known concentration 
 without the presence of other gases, or in fresh air
*/
void setup() {
  Serial.begin(9600); //Baud rate
}
void loop() {
  float sensor_volt; //Define variable for sensor voltage
  float RS_air; //Define variable for sensor resistance
  float R0; //Define variable for R0
  float sensorValue; //Define variable for analog readings
  for (int x = 0 ; x < 500 ; x++) //Start for loop
  {
    sensorValue = sensorValue + analogRead(A0); //Add analog values of sensor 500 times
  }
  sensorValue = sensorValue / 500.0; //Take average of readings
  sensor_volt = sensorValue * (5.0 / 1023.0); //Convert average to voltage
  RS_air = ((5.0 * 10.0) / sensor_volt) - 10.0; //Calculate RS in fresh air
  R0 = RS_air / 4.4; //Calculate R0
  Serial.print("R0 = "); //Display "R0"
  Serial.println(R0); //Display value of R0
  delay(1000); //Wait 1 second
}



Dan berikut ini adalah program untuk mengkalkulasikan / menghitung kadar Gas Methana ke dalam Persentasi (%)  dan kedalam Satuan PPM :

/*
 Arduino MQ4 gas sensor - Geekstips.com
 This example is to calculate the gas concentration using the R0
 calculated in the example above
 Also a OLED SSD1306 screen is used to display data, if you do not
 have such a display, just delete the code used for displaying
*/
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <SPI.h> //Library for SPI interface 
#include <Wire.h> //Library for I2C interface 
#define OLED_RESET 11 //Reset pin 
Adafruit_SSD1306 display(OLED_RESET); //Set Reset pin for OLED display
int led = 10; //LED pin
int buzzer = 9; //Buzzer pin
int gas_sensor = A0; //Sensor pin
float m = -0.318; //Slope
float b = 1.133; //Y-Intercept
float R0 = 11.820; //Sensor Resistance in fresh air from previous code
void setup() {
  Serial.begin(9600); //Baud rate
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize screen
  display.setTextColor(WHITE); //Set text color
  display.setTextSize(3); //Set text size
  pinMode(led, OUTPUT); //Set LED as output
  digitalWrite(led, LOW); //Turn LED off
  pinMode(buzzer, OUTPUT); //Set buzzer as output
  digitalWrite(buzzer, LOW); // Turn buzzer off
  pinMode(gas_sensor, INPUT); //Set gas sensor as input
}
void loop() {
  display.clearDisplay(); //Clear display
  display.setCursor(0, 5); //Place cursor in (x,y) location
  float sensor_volt; //Define variable for sensor voltage
  float RS_gas; //Define variable for sensor resistance
  float ratio; //Define variable for ratio
  float sensorValue = analogRead(gas_sensor); //Read analog values of sensor
  sensor_volt = sensorValue * (5.0 / 1023.0); //Convert analog values to voltage
  RS_gas = ((5.0 * 10.0) / sensor_volt) - 10.0; //Get value of RS in a gas
  ratio = RS_gas / R0;   // Get ratio RS_gas/RS_air
  double ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value
  double ppm = pow(10, ppm_log); //Convert ppm value to log scale
  double percentage = ppm / 10000; //Convert to percentage
  display.print(percentage); //Load screen buffer with percentage value
  display.print("%"); //Load screen buffer with "%"
  display.display(); //Flush characters to screen
  if (ppm > 2000) {
    //Check if ppm value is greater than 2000
    digitalWrite(led, HIGH); //Turn LED on
    digitalWrite(buzzer, HIGH); //Turn buzzer on
  } else {
    //Case ppm is not greater than 2000
    digitalWrite(led, LOW);
    //Turn LED off
    digitalWrite(buzzer, LOW);
    //Turn buzzer off
  }
}


Selamat Mencoba , Semoga Bermanfaat!!!  Dan Jangan Sungkan Untuk Bertanya di Kolom komentar di bawah !

No comments