#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns and 2 rows
const int gasSensorPin = A0; // Analog pin connected to the sensor output
const int greenLedPin = 8; // Digital pin for Green LED
const int redLedPin = 12; // Digital pin for Red LED
const int thresholdValue = 500; // Adjust this threshold according to your sensor and calibration
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(9600); // Initialize serial communication
lcd.setCursor(0, 0);
lcd.print("Gas Sensor:");
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(gasSensorPin); // Read the analog sensor value
Serial.print("Gas Sensor Value: ");
Serial.println(sensorValue); // Print the sensor value to serial monitor
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(sensorValue); // Display sensor value on LCD
// Control LEDs based on sensor value
if (sensorValue < thresholdValue) {
digitalWrite(greenLedPin, HIGH); // Turn on Green LED
digitalWrite(redLedPin, LOW); // Turn off Red LED
} else {
digitalWrite(greenLedPin, LOW); // Turn off Green LED
digitalWrite(redLedPin, HIGH); // Turn on Red LED
}
delay(1000); // Delay for 1 second
}