// This project uses two temperature sensors (indoor & outdoor) and if the temperature indoor is lower than outdoor the relay is triggered to tur on a FAN to increase indoor temperature.
#include "DHT.h"
#define DHT_INDOOR_PIN 2
#define DHT_INDOOR_TYPE DHT22
#define DHT_OUTDOOR_PIN 3
#define DHT_OUTDOOR_TYPE DHT22
#define REL_PIN 10
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht_indoor(DHT_INDOOR_PIN, DHT_INDOOR_TYPE);
DHT dht_outdoor(DHT_OUTDOOR_PIN, DHT_OUTDOOR_TYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("FAN test!"));
pinMode(REL_PIN, OUTPUT);
// Utilise inbuild LED pin 13
pinMode(LED_BUILTIN, OUTPUT);
dht_indoor.begin();
dht_outdoor.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h_in = dht_indoor.readHumidity();
// Read temperature as Celsius (the default)
float t_in = dht_indoor.readTemperature();
float t_out = dht_outdoor.readTemperature();
Serial.print(F("Humidity IN: "));
Serial.print(h_in);
Serial.print(F("% Temperature IN: "));
Serial.print(t_in);
Serial.print(F("°C "));
Serial.print(F("Temperature OUT: "));
Serial.print(t_out);
Serial.println(F("°C "));
if(t_in <= t_out) {
digitalWrite(REL_PIN, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(REL_PIN, LOW);
digitalWrite(LED_BUILTIN, LOW);
}
}