/*
 * This code is made by Technical Shubham
 * Date: [Insert Date]  Time: [Insert Time]
 * Description: This program reads temperature, humidity (DHT22), and gas concentration (MQ-2 sensor).
 * The data is displayed on a 16x2 I2C LCD and printed on the Serial Monitor.
 * Note: PPM values are not accurate and need calibration.
 */

#include <Wire.h>                   // Library for I2C communication
#include <LiquidCrystal_I2C.h>       // Library for LCD
#include <DHT.h>                     // Library for DHT sensor

// Define pin connections
#define DHT_PIN 2        // DHT22 sensor data pin connected to pin 2
#define MQ2_PIN A0       // MQ-2 gas sensor analog output connected to A0
#define DHT_TYPE DHT22   // Define sensor type

// Initialize LCD with I2C address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Initialize DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(9600);  // Start serial communication at 9600 baud rate
  lcd.init();          // Initialize LCD
  lcd.backlight();     // Turn on LCD backlight
  dht.begin();         // Initialize DHT sensor
  pinMode(MQ2_PIN, INPUT);  // Set MQ-2 gas sensor pin as input
}

void loop() {
  static unsigned long lastSensorRead = 0;

  // Read sensor data every 2 seconds
  if (millis() - lastSensorRead > 2000) {
    float humidity = dht.readHumidity();    // Read humidity from DHT22
    float temperature = dht.readTemperature();  // Read temperature from DHT22
    int gasValue = analogRead(MQ2_PIN);     // Read gas sensor value (Needs Calibration)

    // Display data on LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("T:");
    lcd.print(temperature);
    lcd.print("C H:");
    lcd.print(humidity);
    lcd.print("%");

    lcd.setCursor(0, 1);
    lcd.print("Gas: ");
    lcd.print(gasValue);  // Display raw gas sensor value (Needs Calibration)

    // Print data on Serial Monitor
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" °C, Humidity: ");
    Serial.print(humidity);
    Serial.print("%, Gas Level: ");
    Serial.println(gasValue);

    lastSensorRead = millis();  // Update last sensor read time
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
gas1:AOUT
gas1:DOUT
gas1:GND
gas1:VCC