#include "DHT.h"
// DHT sensor settings
#define DHTPIN 21 // Pin where the DHT sensor is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302), DHT11
DHT dht(DHTPIN, DHTTYPE);
// LED settings
#define LEDPIN 25 // Pin where the LED is connected
void setup() {
// Initialize serial communication
Serial.begin(115200);
Serial.println("DHTxx test!");
// Initialize the DHT sensor
dht.begin();
// Initialize the LED pin as an output
pinMode(LEDPIN, OUTPUT);
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Control the LED based on temperature threshold
if (t > 25.0) { // Adjust the threshold temperature as needed
digitalWrite(LEDPIN, HIGH); // Turn the LED on
} else {
digitalWrite(LEDPIN, LOW); // Turn the LED off
}
}