Arduino - Cooling System using DHT Sensor

In this tutorial, we are going to control temperature control temperature using a fan and DHT11 or DHT22 sensor. When the temperature is too hot, turn on the cooling fan. When the temperature is cool, turn off the cooling fan. If you want to use DS18B20 sensor instead of DHT sendsor, see Arduino - Cooling System using DS18B20 Sensor .

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Temperature and Humidity Sensor DHT11
1×Relay
1×12V Cooling Fan
1×12V Power Adapter
n×Jumper Wires

Or you can use 5V Cooling Fan instead of 12V Cooling Fan, and DHT22 sensor instead of DHT11 sensor. Even you use 5V Cooling Fan, you still need a relay and 5V Power Adapter. You can not power fan using 5V from Arduino.

Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Cooling Fan and DHT Sensor

The cooling fan used in this tutorial uses the 12v power supply. If power is supplied for the fan, the fan on and vice verse. To control fan by Arduino, we need to use a relay in between.

If you do not know about temperature sensor and fan (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

  • Wiring Diagram with DHT11 module
  • Arduino cooling fan system wiring diagram

    Image is developed using Fritzing. Click to enlarge image

  • Wiring Diagram with DHT22 module
  • Arduino cooling fan system wiring diagram

    Image is developed using Fritzing. Click to enlarge image

How System Works

  • Arduino reads the temperature from the temperature sensor
  • If the temperature exceeds an upper threshold, Arduino turn on the fan
  • If the temperature falls below a lower threshold, Arduino turn off the fan

The above process is repeated infinitely in the loop.

If you want to turn on and turn off the fan when the temperature is above and below a specific value respectively, you just need to set the upper threshold and lower threshold to the same value.

Arduino Code

Arduino Code for Cooling System with DHT11 sensor

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-cooling-system-using-dht-sensor */ #include "DHT.h" #define RELAY_FAN_PIN A5 // Arduino pin connected to relay which connected to fan #define DHTPIN 12 // Arduino pin connected to relay which connected to DHT sensor #define DHTTYPE DHT11 const int TEMP_THRESHOLD_UPPER = 25; // upper threshold of temperature, change to your desire value const int TEMP_THRESHOLD_LOWER = 20; // lower threshold of temperature, change to your desire value const int RELAY_FAN_PIN = 3; // Arduino pin connected to relay which connected to fan DHT dht(DHTPIN, DHTTYPE); float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // initialize serial dht.begin(); // initialize the sensor } void loop() { // wait a few seconds between measurements. delay(2000); temperature = dht.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); } else { if(temperature > TEMP_THRESHOLD_UPPER){ Serial.println("The fan is turned on"); digitalWrite(RELAY_FAN_PIN, HIGH); // turn on } else if(temperature < TEMP_THRESHOLD_LOWER){ Serial.println("The fan is turned off"); digitalWrite(RELAY_FAN_PIN, LOW); // turn on } } }

Arduino Code for Cooling System with DHT22 sensor

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-cooling-system-using-dht-sensor */ #include "DHT.h" #define RELAY_FAN_PIN A5 // Arduino pin connected to relay which connected to fan #define DHTPIN 12 // Arduino pin connected to relay which connected to DHT sensor #define DHTTYPE DHT22 const int TEMP_THRESHOLD_UPPER = 25; // upper threshold of temperature, change to your desire value const int TEMP_THRESHOLD_LOWER = 20; // lower threshold of temperature, change to your desire value DHT dht(DHTPIN, DHTTYPE); float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // initialize serial dht.begin(); // initialize the sensor } void loop() { // wait a few seconds between measurements. delay(2000); temperature = dht.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); } else { if(temperature > TEMP_THRESHOLD_UPPER){ Serial.println("The fan is turned on"); digitalWrite(RELAY_FAN_PIN, HIGH); // turn on } else if(temperature < TEMP_THRESHOLD_LOWER){ Serial.println("The fan is turned off"); digitalWrite(RELAY_FAN_PIN, LOW); // turn on } } }

In the above codes, the Arduino turn on the fan when the temperature exceeds 25°C, and keep the fan on until the temperature is below 20°C

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to Sketch Include Library Manage Libraries
  • Arduino add library
  • Search “DHT”, then find the DHT sensor library by Adafruit
  • Click Install button to install the library.
  • Arduino DHT sensor library
  • You will be ased for intall some other library dependancies
  • Click Install All button all library dependancies.
  • Arduino Adafruit Unified sensor library
  • Copy the above code corresponding to the sensor you have and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Make enviroment around sensor hotter or colder
  • See the state of fan

Advanced Knowledge

The above controlling method is the on-off controller, also known as a signaller or "bang-bang" controller. This method is very simple to implement.

There is an alternative method called the PID controller. With the PID controller, the desired temperature is more stable but very difficult to understand and implement. Therefore, the PID controller is not popular in temperature control.

※ OUR MESSAGES