#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions
const int MQ2_PIN = 4; // Analog pin for MQ2
const int MQ7_PIN = 35; // Analog pin for MQ7
const int ledPin = 5; // LED indicator for safe status
const int ledPin1 = 2; // LED indicator for dangerous status
// LCD configuration
#define LCD_ADDR 0x27 // I2C address for the LCD
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
// Calibration factors (adjust these based on calibration tests)
const float calibrationFactorMQ2 = 0.5; // Example calibration factor for MQ2
const float calibrationFactorMQ7 = 0.8; // Example calibration factor for MQ7
void setup() {
Serial.begin(115200);
// Pin modes
pinMode(MQ2_PIN, INPUT);
pinMode(MQ7_PIN, INPUT); // Set MQ7 pin mode
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
// Initialize LEDs to off
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("CO Level Monitor");
delay(1000);
}
void loop() {
// Read values from the sensors
int sensorValueMQ2 = analogRead(MQ2_PIN);
int sensorValueMQ7 = analogRead(MQ7_PIN); // Read MQ7 sensor value
// Convert analog values to CO levels (in ppm) with calibration
int coLevelMQ2 = map(sensorValueMQ2, 0, 1023, 0, 1000) * calibrationFactorMQ2;
int coLevelMQ7 = map(sensorValueMQ7, 0, 1023, 0, 1000) * calibrationFactorMQ7;
// Output to Serial Monitor
Serial.print("CO Level MQ2: ");
Serial.print(coLevelMQ2);
Serial.println(" ppm");
Serial.print("CO Level MQ7: ");
Serial.print(coLevelMQ7);
Serial.println(" ppm");
// Update LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CO MQ2: ");
lcd.print(coLevelMQ2);
lcd.print(" "); // Clear previous status
lcd.setCursor(0, 1);
lcd.print("CO MQ7: ");
lcd.print(coLevelMQ7); // Display MQ7 value
// Check CO levels and update status
if (coLevelMQ2 < 200 && coLevelMQ7 < 100) {
Serial.println("Tingkat CO aman");
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, HIGH);
lcd.setCursor(0, 1);
lcd.print("Status Aman "); // Clear previous status
} else {
Serial.println("Tingkat CO berbahaya");
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, LOW);
lcd.setCursor(0, 1);
lcd.print("Status Berbahaya");
}
delay(1000); // Delay for stability in readings
}