ESP32 - 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 ESP32 - Cooling System using DS18B20 Sensor.
|1||×||ESP-WROOM-32 Dev Module|
|1||×||Micro USB Cable|
|1||×||Temperature and Humidity Sensor DHT11|
|1||×||12V Cooling Fan|
|1||×||12V Power Adapter|
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 ESP32.
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 ESP32, we need to use a relay in between.
If you do not have knowledge of temperature sensor and fan (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about them in the following tutorials:
How System Works
- ESP32 reads the temperature from the temperature sensor
- If the temperature exceeds an upper threshold, ESP32 turn on the fan
- If the temperature falls below a lower threshold, ESP32 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.
ESP32 Code for Cooling System with DHT11 sensor
ESP32 Code for Cooling System with DHT22 sensor
In the above codes, the ESP32 turn on the fan when the temperature exceeds 25°C, and keep the fan on until the temperature is below 20°C
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
- Connect the ESP32 board to your PC via a micro USB cable
- Open Arduino IDE, select the right ESP32 board and COM port
- On Arduino IDE, Go to Sketch Include Library Manage Libraries
- Search “DHT”, then find the DHT sensor library by Adafruit
- Click Install button to install the library.
- You will be ased for intall some other library dependancies
- Click Install All button all library dependancies.
- Copy the above code corresponding to the sensor you have and open with Arduino IDE
- Click Upload button on Arduino IDE to compile and upload code to ESP32 board
- Make enviroment around sensor hotter or colder
- See the state of fan
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.
※ NOTE THAT:
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.