Things used in this project

Hardware components:
Ard nano
Arduino Nano R3
×1
386 00
DHT11 Temperature & Humidity Sensor
×1
5v DC Relay Module
×1

Schematics

Temp Controlled Fan
Tempcontrolledfan schem

Code

FanTempControllerC/C++
Arduino sketch that uses the average temp reading from a DHT11 sensor to turn on a fan via a relay.
// Temperature controller for A/V Cabinet
// Fan is controlled by 10A relay connected to a 12v (computer) or 5v (USB) Fan
// Piezo alarm for alternate overheating alert


int FANTEMP = 95;      // High temp at which the fan turns on (90*F)
int ALARMTEMP = 120;   // Overheating temp (120*F)
int FANLED = 2;        // Pin for fan "on" LED
int TEMPOK = 3;        // LED for when temp is under FANTEMP
int ALARMLED = 4;      // Alarm LED 
int ALARMPIN = 7;      // Audible alarm for overheating
// DHTPIN = 8;        (defined below)
int FANPIN = 9;        // Relay for fan switch

#include "DHT.h"        // Written by ladyada, public domain
#define DHTPIN 8        // DHT Sensor

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

// Readings used for average
const int numReadings = 10;

// Set variables to zero
float avetemp = 0;                
float temp = 0;
float checkdelay = 0;           

void setup() {
  Serial.begin(9600); 
  Serial.println("Temp Monitor Started");
  
  dht.begin();
  pinMode(FANPIN,OUTPUT);
  pinMode(ALARMPIN,OUTPUT);
  pinMode(ALARMLED,OUTPUT);
  pinMode(FANLED,OUTPUT);
  pinMode(TEMPOK,OUTPUT);
  
  digitalWrite(FANPIN, HIGH);
  digitalWrite(FANLED, HIGH);
  digitalWrite(ALARMLED, HIGH);
  digitalWrite(TEMPOK, HIGH);
    for(int x = 0; x < 5; x++){                // Test Alarm 
        tone(ALARMPIN, 220 * x, 75);
        delay(100);                  
       }
   Serial.print("Fan Test Started (5 Seconds) "); 
    for(int x = 0; x < 5; x++){
     Serial.print(".");
     delay(1000); 
    }
    Serial.println("Done");
  digitalWrite(FANPIN, LOW);
  digitalWrite(FANLED, LOW);
  digitalWrite(ALARMLED, LOW);
  digitalWrite(TEMPOK, LOW);
  noTone(ALARMPIN);
 }

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);
  temp = 0;

    Serial.print("Realtime Temp: \t");

  for (int x = 0; x < numReadings; x++){
    float f = dht.readTemperature(true);            // Read temperature as Fahrenheit
    Serial.print(f);
    Serial.print("\t");
    temp = f + temp;
    delay(3000);                                    // delay in between reads for stability  
  }    
  Serial.println();
  
  avetemp = temp / numReadings;                    // calculate the average
  Serial.print("Average Temp is ");
  Serial.println(avetemp);                         // send it to the computer as ASCII digits

     // Check if any reads failed and exit early (to try again).
  if (isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Set off alarm if overheating  
    if (avetemp>ALARMTEMP) {
      digitalWrite(ALARMLED, HIGH);
      Serial.print("Temperature is over ");
      Serial.print(ALARMTEMP);
      Serial.println(", Alarm is on");
       for(int x = 0; x < 3; x++){                  // Sound the alarm for 5 seconds 
        tone(ALARMPIN, 660, 1000);                  // Fan should already be running from last loop, if not, it will start right after alarm sounds
        delay(500);
        tone(ALARMPIN, 440, 1000);
        delay(500);
        }
     noTone(ALARMPIN);
     checkdelay = 30000;                            // Switch the normal 5 min delay to 30 seconds before going through the loop again 
   } else {
    digitalWrite(ALARMLED,LOW);
    Serial.print("Temperature is under ");
    Serial.print(ALARMTEMP);
    Serial.println(", Alarm is off");
    checkdelay = 300000;                            // Unless temp is over 120*F the fan runs for 5 minutes before temp is checked again
  }

// Turn on fan if cabinet is warm
  if (avetemp>FANTEMP) {
    digitalWrite(FANPIN, HIGH);
    digitalWrite(FANLED, HIGH);
    digitalWrite(TEMPOK, LOW);
    Serial.print("Temperature is over ");
    Serial.print(FANTEMP);
    Serial.print(", Fan is on (for ");
    Serial.print(checkdelay / 1000 / 60);
    Serial.println(" minutes)");
    delay(checkdelay);                               // turn on minimum of 5 min (unless alarm is going off, then it loops after 30 seconds)
   } else {
    digitalWrite(FANPIN,LOW);
    digitalWrite(FANLED,LOW);
    digitalWrite(TEMPOK,HIGH);
    Serial.print("Temperature is under ");
    Serial.print(FANTEMP);
    Serial.println(", Fan is off");                   // When fan is off, Temp is read every 30 seconds
  }
    
  Serial.println();
  Serial.println();
  
}

Credits

Drbudro
BuddyC
9 projects • 61 followers
I collect hobbies.
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog