#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//These library files include the DHT library, Wire library and LiquidCrystal_I2C library, which provide functions related to the DHT22 sensor, I2C communication and LCD display.
#define DHTPIN 4
#define DHTTYPE DHT22
#define LED_HIGH_TEMP_PIN 5
#define LED_LOW_TEMP_PIN 6
#define LED_HIGH_HUMIDITY_PIN 7
#define LED_LOW_HUMIDITY_PIN 8
//These define the pin numbers used to connect the sensor and LED.DHT dht(DHTPIN, DHTTYPE);
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(LED_HIGH_TEMP_PIN, OUTPUT);
pinMode(LED_LOW_TEMP_PIN, OUTPUT);
pinMode(LED_HIGH_HUMIDITY_PIN, OUTPUT);
pinMode(LED_LOW_HUMIDITY_PIN, OUTPUT);
Wire.begin(GPIO_NUM_21, GPIO_NUM_33); // Connect the SDA pin to GPIO21 and the SCL pin to GPIO33
lcd.begin(16, 2);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print("Humidity:");//These statements initialize the LCD and print temperature and humidity labels on the display.
dht.begin();//This statement starts the DHT22 sensor and prepares to read temperature and humidity.
Serial.begin(9600);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();//These statements read the temperature and humidity values from the DHT22 sensor using the `readTemperature` and `readHumidity` methods of the `dht` object.
lcd.setCursor(5, 0);
lcd.print(temperature, 1); // Display temperature to one decimal place
lcd.print("'C");
lcd.setCursor(9, 1);
lcd.print(humidity, 1); // Display temperature to one decimal place
lcd.print("%");//These statements use the corresponding methods of the `lcd` object to display temperature and humidity values on the LCD display.
if (temperature >= 28) {
digitalWrite(LED_HIGH_TEMP_PIN, HIGH);
} else {
digitalWrite(LED_HIGH_TEMP_PIN, LOW);
}//These statements control the light and dark status of the corresponding LED pins according to the range of temperature and humidity.
if (temperature <= 15) {
digitalWrite(LED_LOW_TEMP_PIN, HIGH);
} else {
digitalWrite(LED_LOW_TEMP_PIN, LOW);
}//These statements control the light and dark status of the corresponding LED pins according to the range of temperature and humidity.
if (humidity >= 75) {
digitalWrite(LED_HIGH_HUMIDITY_PIN, HIGH);
} else {
digitalWrite(LED_HIGH_HUMIDITY_PIN, LOW);
}//These statements control the light and dark status of the corresponding LED pins according to the range of temperature and humidity.
if (humidity <= 40) {
digitalWrite(LED_LOW_HUMIDITY_PIN, HIGH);
} else {
digitalWrite(LED_LOW_HUMIDITY_PIN, LOW);
}//These statements control the light and dark status of the corresponding LED pins according to the range of temperature and humidity.
Serial.print("Temp: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");//These statements output the temperature and humidity values through the serial port so that the values can be viewed on the serial port monitoring console of the development environment.
delay(1000);
}