Program Sensor Gas MQ-4 Methana for Arduino Project
Program Sensor Gas MQ-4 Methana for Arduino Project
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 (methane, CH4) 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.
Baca juga - Sensor Proximity (Infrared)
Berikut Sketch sederhana-nya :
Int SensorValue;void setup() {Serial.begin(9600); //Seting serial port ke 9600pinMode(A0, INPUT);}void loop() {SensorValue = AnalogRead(A0); //Membaca analog input 0Serial.println(SensorValue, DEC); // Menampilkan Value dalam DECIMALdelay(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.comThis example is for calculating R0 which isthe resistance of the sensor at a known concentrationwithout 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 voltagefloat RS_air; //Define variable for sensor resistancefloat R0; //Define variable for R0float sensorValue; //Define variable for analog readingsfor (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 readingssensor_volt = sensorValue * (5.0 / 1023.0); //Convert average to voltageRS_air = ((5.0 * 10.0) / sensor_volt) - 10.0; //Calculate RS in fresh airR0 = RS_air / 4.4; //Calculate R0Serial.print("R0 = "); //Display "R0"Serial.println(R0); //Display value of R0delay(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.comThis example is to calculate the gas concentration using the R0calculated in the example aboveAlso a OLED SSD1306 screen is used to display data, if you do nothave 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 pinAdafruit_SSD1306 display(OLED_RESET); //Set Reset pin for OLED displayint led = 10; //LED pinint buzzer = 9; //Buzzer pinint gas_sensor = A0; //Sensor pinfloat m = -0.318; //Slopefloat b = 1.133; //Y-Interceptfloat R0 = 11.820; //Sensor Resistance in fresh air from previous codevoid setup() {Serial.begin(9600); //Baud ratedisplay.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize screendisplay.setTextColor(WHITE); //Set text colordisplay.setTextSize(3); //Set text sizepinMode(led, OUTPUT); //Set LED as outputdigitalWrite(led, LOW); //Turn LED offpinMode(buzzer, OUTPUT); //Set buzzer as outputdigitalWrite(buzzer, LOW); // Turn buzzer offpinMode(gas_sensor, INPUT); //Set gas sensor as input}void loop() {display.clearDisplay(); //Clear displaydisplay.setCursor(0, 5); //Place cursor in (x,y) locationfloat sensor_volt; //Define variable for sensor voltagefloat RS_gas; //Define variable for sensor resistancefloat ratio; //Define variable for ratiofloat sensorValue = analogRead(gas_sensor); //Read analog values of sensorsensor_volt = sensorValue * (5.0 / 1023.0); //Convert analog values to voltageRS_gas = ((5.0 * 10.0) / sensor_volt) - 10.0; //Get value of RS in a gasratio = RS_gas / R0; // Get ratio RS_gas/RS_airdouble ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale according to the the ratio valuedouble ppm = pow(10, ppm_log); //Convert ppm value to log scaledouble percentage = ppm / 10000; //Convert to percentagedisplay.print(percentage); //Load screen buffer with percentage valuedisplay.print("%"); //Load screen buffer with "%"display.display(); //Flush characters to screenif (ppm > 2000) {//Check if ppm value is greater than 2000digitalWrite(led, HIGH); //Turn LED ondigitalWrite(buzzer, HIGH); //Turn buzzer on} else {//Case ppm is not greater than 2000digitalWrite(led, LOW);//Turn LED offdigitalWrite(buzzer, LOW);//Turn buzzer off}}
Selamat Mencoba , Semoga Bermanfaat!!! Dan Jangan Sungkan Untuk Bertanya di Kolom komentar di bawah !
No comments