#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
const int redLedPin = 8; // Pin for the red LED
const int buzzerPin = 9; // Pin for the buzzer
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Check humidity
if (humidity > 70) {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
} else {
digitalWrite(redLedPin, LOW); // Turn off the red LED
}
// Check temperature
if (temperature > 40) {
// Sound the buzzer for half a second
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
// Wait for another half a second
delay(500);
} else {
noTone(buzzerPin); // Turn off the buzzer
}
}