#include <Adafruit_Sensor.h>
#include <DHT_U.h> // DHT sensor library
// Defining Pins
#define DHTPIN 12 // Pin where DHT22 is connected
#define FAN_PIN 26 // Pin where the fan (simulated by an LED) is connected
// DHT parameters
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);
float temp;
const float thresholdTemp = 35.0; // Threshold temperature for fan activation
void setup() {
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW); // Fan off initially
Serial.println("Temperature monitoring started...");
}
void loop() {
// Read temperature from DHT22 sensor
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
temp = event.temperature;
Serial.print(F("Temperature: "));
Serial.print(temp);
Serial.println(F("°C"));
// Check if temperature exceeds the threshold
if (temp > thresholdTemp) {
Serial.println("Temperature exceeded threshold! Turning on fan...");
digitalWrite(FAN_PIN, HIGH); // Turn on fan
} else {
Serial.println("Temperature below threshold. Fan is off.");
digitalWrite(FAN_PIN, LOW); // Turn off fan
}
}
delay(1000); // Wait 1 second before next reading
}