ESP8266 - Cooling System using DHT Sensor

This tutorial instructs you how to regulate temperature with the help of 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 ESP8266 - Cooling System using DS18B20 Sensor.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro 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) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

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 in this tutorial requires a 12v power supply. If the power is supplied, the fan will turn on, and if not, it will stay off. To control the fan with an ESP8266, we must use a relay as an intermediary.

If you are unfamiliar with temperature sensor and fan (including pinout, how it works, and programming), the following tutorials can provide you with the necessary information:

Wiring Diagram

  • A diagram showing the connections of a DHT11 module.
The wiring diagram between ESP8266 NodeMCU and cooling fan system

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • A diagram showing the connections of a DHT22 module
  • An illustration of the wiring for a DHT22 module
The wiring diagram between ESP8266 NodeMCU and cooling fan system

This image is created using Fritzing. Click to enlarge image

How System Works

  • ESP8266 obtains the temperature from the temperature sensor.
  • If the temperature is higher than the upper threshold, ESP8266 activates the fan.
  • If the temperature is lower than the lower threshold, ESP8266 shuts off the fan.

This loop is repeated without end.

ESP8266 Code

ESP8266 Code for Cooling System with DHT11 sensor

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-cooling-system-using-dht-sensor */ #include <DHT.h> #define FAN_PIN D2 // The ESP8266 pin connected to relay #define DHT_SENSOR_PIN D7 // The ESP8266 pin connected to DHT11 sensor #define DHT_SENSOR_TYPE DHT11 #define THRESHOLD_ON 30 // upper temperature threshold #define THRESHOLD_OFF 15 // lower temperature threshold DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE); void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. dht_sensor.begin(); // initialize the DHT sensor } void loop() { float temperature = dht_sensor.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); } else { if (temperature > THRESHOLD_ON) { Serial.println("Turn the fan on"); digitalWrite(FAN_PIN, HIGH); // turn on } else if (temperature < THRESHOLD_OFF) { Serial.println("Turn the fan off"); digitalWrite(FAN_PIN, LOW); // turn off } } // wait a 2 seconds between readings delay(2000); }

ESP8266 Code for Cooling System with DHT22 sensor

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-cooling-system-using-dht-sensor */ #include <DHT.h> #define FAN_PIN D2 // The ESP8266 pin connected to relay #define DHT_SENSOR_PIN D7 // The ESP8266 pin connected to DHT22 sensor #define DHT_SENSOR_TYPE DHT22 #define THRESHOLD_ON 30 // upper temperature threshold #define THRESHOLD_OFF 15 // lower temperature threshold DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE); void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. dht_sensor.begin(); // initialize the DHT sensor } void loop() { float temperature = dht_sensor.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); } else { if (temperature > THRESHOLD_ON) { Serial.println("Turn the fan on"); digitalWrite(FAN_PIN, HIGH); // turn on } else if (temperature < THRESHOLD_OFF) { Serial.println("Turn the fan off"); digitalWrite(FAN_PIN, LOW); // turn off } } // wait a 2 seconds between readings delay(2000); }

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

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect an ESP8266 to a PC using a USB cable
  • Open the Arduino IDE, select the correct board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Look up “DHT” and locate the DHT sensor library from Adafruit.
  • Hit the Install button to add the library.
ESP8266 NodeMCU DHT sensor library
  • You will be asked to install some other library dependancies.
  • Click the Install All button to install all library dependancies.
ESP8266 NodeMCU Adafruit Unified sensor library
  • On serial monitor
  • Copy the code that is associated with the sensor you have and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
  • Change the temperature of the area around the sensor.
  • Check out the status of the fan on the serial monitor.

Advanced Knowledge

The above method of control is referred to as an on-off controller, which is also known as a signaller or "bang-bang" controller. This approach is very easy to set up.

An alternative approach is the PID controller. This method makes temperature regulation more stable, however it is complex and challenging to comprehend and apply. As a result, the PID controller is not widely used for temperature control.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!