#define MQ2pin 8
int sensorValue; //variable to store sensor value
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4 // variable for reading the pin status
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600); // sets the serial port to 9600
Serial.println("MQ2 warming up");
lcd.setCursor(1, 0);
lcd.print("MQ2 warming up");
delay(500); // allow the MQ2 to warm up
lcd.clear();
delay(500);
}
void loop() {
sensorValue = digitalRead(MQ2pin); // read digital output pin
Serial.print("Digital Output: ");
//lcd.setCursor(1, 0);
//lcd.print("DigitOut: ");
Serial.print(sensorValue);
//lcd.setCursor(1, 0);
//lcd.print(sensorValue);
// Determine the status
if (sensorValue) {
Serial.println(" | Smoke: -");
} else {
Serial.println(" | Smoke: Detected!");
lcd.setCursor(1, 1);
lcd.print("Smoke");
}
delay(2000); // wait 2s for next reading
}