#include <DHT.h>
// Define the pin where the DHT22 is connected
#define DHTPIN 2
// Define the type of sensor (DHT22 in this case)
#define DHTTYPE DHT22
// Create an instance of the DHT class
DHT dht(DHTPIN, DHTTYPE);
// Define the pin where the LED is connected
#define LEDPIN 13
// Define the temperature threshold
#define TEMP_THRESHOLD 25.0 // Temperature threshold in degrees Celsius
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
// Set the LED pin as an output
pinMode(LEDPIN, OUTPUT);
}
void loop() {
// Delay between measurements
delay(2000);
// Read the temperature from the DHT22 sensor
float temperature = dht.readTemperature();
// Check if the reading was successful
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Control the LED based on the temperature
if (temperature > TEMP_THRESHOLD) {
digitalWrite(LEDPIN, HIGH); // Turn the LED on
} else {
digitalWrite(LEDPIN, LOW); // Turn the LED off
}
}