#include <DHT.h>
#define DHTPIN 2 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // Type of DHT sensor
#define LED_PIN 12 // Pin where the LED is connected
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
}
void loop() {
delay(1000); // Wait for 2 seconds to avoid flooding the serial monitor
float humidity = dht.readHumidity(); // Read humidity
// Check if any reads failed and exit early (to try again)
if (isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (humidity > 80) { // adjust the threshold value as needed
Serial.println("Flood detected!");
digitalWrite(LED_PIN, HIGH); // Turn on the LED
} else {
Serial.println("No flood detected.");
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
}