Arduino Nano - Cooling System using DHT Sensor

This tutorial instructs you how to use Arduino Nano to control temperature with a fan and either a DHT11 or DHT22 sensor.

If you would prefer to use a DS18B20 sensor instead of a DHT sensor, please refer to Arduino Nano - Cooling System using DS18B20 Sensor.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×DHT11 Temperature and Humidity Sensor
1×Relay
1×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

You can use DHT22 sensor instead of DHT11 sensor.

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Cooling Fan and DHT Sensor

The fan for this tutorial requires a 12v power supply. If the power is supplied, the fan will turn on and if not, it will remain off. To control the fan with Arduino Nano, a relay must be used as an intermediary.

If you are not familiar with temperature sensor and fan (pinout, how it works, how to program ...), the following tutorials can help you understand them:

Wiring Diagram

  • A schematic of the wiring for a system that includes a DHT11 module.
The wiring diagram between Arduino Nano and cooling fan system

This image is created using Fritzing. Click to enlarge image

  • A diagram showing the connections between the DHT22 module and other components.
The wiring diagram between Arduino Nano and cooling fan system

This image is created using Fritzing. Click to enlarge image

How System Works

  • Arduino Nano obtains the temperature from the temperature sensor.
  • If the temperature is higher than the upper threshold, Arduino Nano turns on the fan.
  • If the temperature is lower than the lower threshold, Arduino Nano turns off the fan.

The loop is repeated continuously.

Arduino Nano Code

Arduino Nano Code for Cooling System with DHT11 sensor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-cooling-system-using-dht-sensor */ #include <DHT.h> #define FAN_PIN 6 // The Arduino Nano pin connected to relay which connected to fan #define DHT_PIN 8 // The Arduino Nano pin connected to DHT11 sensor #define DHT_TYPE DHT11 const int THRESHOLD_ON = 25; // upper threshold of temperature, change to your desire value const int THRESHOLD_OFF = 20; // lower threshold of temperature, change to your desire value DHT dht(DHT_PIN, DHT_TYPE); float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. dht.begin(); // initialize the sensor pinMode(FAN_PIN, OUTPUT); // initialize digital pin as an output } 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 > THRESHOLD_ON){ Serial.println("The fan is turned on"); digitalWrite(FAN_PIN, HIGH); // turn on } else if(temperature < THRESHOLD_OFF){ Serial.println("The fan is turned off"); digitalWrite(FAN_PIN, LOW); // turn on } } }

Arduino Nano Code for Cooling System with DHT22 sensor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-cooling-system-using-dht-sensor */ #include <DHT.h> #define FAN_PIN 6 // The Arduino Nano pin connected to relay which connected to fan #define DHT_PIN 8 // The Arduino Nano pin connected to DHT22 sensor #define DHT_TYPE DHT22 const int THRESHOLD_ON = 25; // upper threshold of temperature, change to your desire value const int THRESHOLD_OFF = 20; // lower threshold of temperature, change to your desire value DHT dht(DHT_PIN, DHT_TYPE); float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. dht.begin(); // initialize the sensor pinMode(FAN_PIN, OUTPUT); // initialize digital pin as an output } 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 > THRESHOLD_ON){ Serial.println("The fan is turned on"); digitalWrite(FAN_PIN, HIGH); // turn on } else if(temperature < THRESHOLD_OFF){ Serial.println("The fan is turned off"); digitalWrite(FAN_PIN, LOW); // turn on } } }

In the codes shown above, the Arduino Nano will activate the fan when the temperature is greater than 25°C and keep it running until the temperature drops below 20°C.

Detailed Instructions

  • Connect Arduino Nano to a computer using a USB cable
  • Launch the Arduino IDE, select the appropriate board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “DHT” and locate the Adafruit DHT sensor library.
  • Then, press the Install button to complete the installation.
Arduino Nano DHT sensor library
  • You will be asked to install some other library dependencies.
  • Click the Install All button to install all library dependencies.
Arduino Nano Adafruit Unified sensor library
  • On serial monitor
  • Copy the code that corresponds to the sensor you have and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Change the temperature of the environment around the sensor.
  • Check the status of the fan on the serial monitor.

Advanced Knowledge

The above technique of regulation is the on-off controller, also referred to as a signaller or "bang-bang" controller. This approach is quite easy to execute.

An alternate approach known as the PID controller exists. This method is more effective in maintaining a steady temperature, however it is complex and challenging to comprehend and apply. As a result, the PID controller is not commonly used for temperature regulation.

Video Tutorial