/*
File: my_code.ino
Author: Technical Shubham
Date: April 8, 2024
Description: 🌡️🌧️ This sketch reads temperature and humidity using a DHT22 sensor
and displays the values on an LCD. It also controls LEDs based
on temperature and humidity levels.
*/
// Code developed by Technical Shubham
// All rights reserved ©️
#include <DHTesp.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 15
#define DHTTYPE DHT22
#define led1 12
#define led2 13
#define led3 14
DHTesp dht;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Temperature &");
lcd.setCursor(8, 1);
lcd.print("Humidity");
delay(2000);
lcd.clear();
dht.setup(DHTPIN, DHTesp::DHT22);
}
void loop() {
delay(1000);
// Read temperature and humidity
float h = dht.getHumidity();
float t = dht.getTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display temperature and humidity on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t, 1);
lcd.print(" °C ");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h, 1);
lcd.print("%");
// Control LEDs based on temperature and Humidity
// Above 34 degrees Celcius is dangerous for human
if (t > 34) {
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
// High Humidity is above 60
} else if (t <= 25 && t > 15 && h > 60) {
digitalWrite(led3, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
//Safe Limits
} else {
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
}
// Print temperature and humidity to Serial Monitor
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" °C ");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
}