#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define DHT22 pin and type
#define DHTPIN 15
#define DHTTYPE DHT22
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Define LED pin
#define LED_PIN 14
// Define LCD properties
#define LCD_ADDRESS 0x27 // I2C address of the LCD
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the DHT sensor
dht.begin();
// Set LED pin as output
pinMode(LED_PIN, OUTPUT);
// Initialize LCD
lcd.init();
lcd.backlight(); // Enable backlight
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Read temperature as Celsius
float temperature = dht.readTemperature();
// Read humidity
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity values to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Display temperature and humidity on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
// Check if temperature is greater than 30 degrees Celsius
if (temperature > 30.0) {
// Turn on LED
digitalWrite(LED_PIN, HIGH);
} else {
// Turn off LED
digitalWrite(LED_PIN, LOW);
}
}