#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define LED_PIN 3 // Digital pin connected to the LED
#define TEMP_THRESHOLD 50.0 // Temperature threshold in degrees Celsius
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
}
void loop() {
delay(2000); // Wait for 2 seconds
float temperature = dht.readTemperature(); // Read temperature in Celsius
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
if (temperature > TEMP_THRESHOLD) {
digitalWrite(LED_PIN, HIGH); // Turn on LED
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
}
}