#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MQUnifiedsensor.h>
#define Board ("ESP-32")
#define Pin (4) // Analog input pin for MQ2 sensor
#define Type ("MQ-2") // MQ2 gas sensor
#define Voltage_Resolution (3.3)
#define ADC_Bit_Resolution (12) // ADC resolution
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD with I2C address 0x27
void setup() {
lcd.init(); // Initialize LCD
lcd.clear();
lcd.backlight(); // Turn on the LCD backlight
Serial.begin(9600); // Start serial communication for debugging
// Initialize MQ2 sensor
MQ2.init();
}
void loop() {
MQ2.update(); // Update the sensor data
MQ2.readSensor(); // Read the sensor value
int valor_gas = analogRead(Pin); // Read the raw value from the analog pin
valor_gas = map(valor_gas, 0, 4095, 0, 100); // Scale the value for display (0-100)
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print("Gas Value: ");
lcd.print(valor_gas); // Display the gas value (scaled)
delay(500); // Delay for a half second
}