#include <DHT.h>
// Define pins and DHT sensor type
#define DHTPIN 5 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // Define the DHT sensor type (changed to DHT22)
#define LEDPIN 13 // Pin connected to the LED
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start serial communication
Serial.begin(115200);
// Set LED pin as output
pinMode(LEDPIN, OUTPUT);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait for 2 seconds between readings
delay(2000);
// Read temperature and humidity values
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if reading failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
// Continue the loop even if the reading fails
} else {
// Print the results to the serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
// Control LED based on temperature
if (temperature > 25.0) {
digitalWrite(LEDPIN, HIGH); // Turn on the LED
} else {
digitalWrite(LEDPIN, LOW); // Turn off the LED
}
}
}