#include <Wire.h> //library untuk komponen I2C
#include <LiquidCrystal_I2C.h> //Library untuk LCD Display I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
int mq2Pin = A0; // Analog pin connected to MQ2 A0 output
int redLed = 12; // Digital pin connected to a red LED
int sensorThres = 90; // Threshold value for gas detection
int buzzer = 7; //Pin buzzzer
void setup() {
pinMode(mq2Pin, INPUT); // Mendeklarasikan pin MQ2 sebagai input
pinMode(redLed, OUTPUT); // Mendeklarasikan pin LED sebagai output
pinMode(buzzer, OUTPUT); // Mendeklarasikan pin BUZZER sebagai ouput
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("KELOMPOK 5");
lcd.setCursor(1, 1);
lcd.print("GAS SENSOR");
delay(1000);
lcd.clear();
}
void loop() {
delay(1000);
int analogSensorValue = analogRead(mq2Pin); // Membaca value dari sensor MQ2
digitalRead(buzzer);
Serial.print("Output MQ-2: ");
Serial.println(analogSensorValue); // Print sensor value to Serial Monitor
// Check if the sensor value exceeds the threshold
if (analogSensorValue > sensorThres) {
digitalWrite(redLed, HIGH); // Nyalain LED
digitalWrite(buzzer,HIGH); //Nyalain Buzzer
} else {
digitalWrite(redLed, LOW); // Mematikan LED
digitalWrite(buzzer,LOW); //Mematikan Buzzer
}
// Update LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GAS: ");
lcd.print(analogSensorValue);
lcd.setCursor(0, 1);
if (analogSensorValue > sensorThres) {
lcd.print("Status: Bahaya");
} else {
lcd.print("Status: Aman ");
}
}