#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define PPM_ANALOG_PIN 0 // Analog output pin (A0) of MQ135 connected to pin 0 of ESP32
LiquidCrystal lcd(40, 14, 35, 36, 39, 38);
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(20, OUTPUT);
pinMode(19, OUTPUT);
lcd.begin(16, 2);
}
void loop() {
delay(2000);
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Read analog output from MQ135
int16_t ppmValue = analogRead(PPM_ANALOG_PIN);
// Convert analog reading to voltage
float volts = ppmValue * (5.0 / 1023.0);
// Check if sensor readings are valid
if (!isnan(temp) && !isnan(hum)) {
Serial.print("Temperature : ");
Serial.print(temp);
Serial.println(" °C");
Serial.print("Humidity : ");
Serial.print(hum);
Serial.println(" %");
Serial.print("PPM: ");
Serial.println(volts);
// Display sensor readings on LCD with scrolling
displayScrolling("Temp: " + String(temp, 1) + "C Humidity: " + String(hum, 1) + "% Air Quality: " + String(volts, 1) + "V");
// Control LEDs based on temperature
if (temp > 27) {
digitalWrite(1, HIGH);
delay(900);
digitalWrite(1, LOW);
} else {
digitalWrite(0, HIGH);
delay(900);
digitalWrite(0, LOW);
}
} else {
Serial.println("Failed to read from DHT sensor!");
}
delay(100);
}
// Function to display scrolling text on the LCD
void displayScrolling(String text) {
for (int i = 0; i < text.length() + 16; i++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(text.substring(i, i + 16));
delay(500); // Adjust the scrolling speed here
}
}