ESP32 - Cooling System using DS18B20 Temperature Sensor

In this tutorial, we are going to control temperature using a fan and DS18B20 temperature 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 DHT11 or DHT22 instead of the DS18B20 sensor, see ESP32 - Cooling System using DHT Sensor.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Temperature Sensor DS18B20
1×4.7 kΩ resistor
1×12V Cooling Fan
1×12V Power Adapter
n×Jumper Wires

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

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 DS18B20 Temperature 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:

Wiring Diagram

ESP32 cooling fan system wiring diagram

Image is developed using Fritzing. Click to enlarge image

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 DS18B20 sensor

/* * Created by * * This example code is in the public domain * * Tutorial page: */ #include <OneWire.h> #include <DallasTemperature.h> 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 SENSOR_PIN = 2; // ESP32 pin connected to DS18B20 sensor's DQ pin const int RELAY_FAN_PIN = A5; // ESP32 pin connected to relay which connected to fan OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // initialize serial sensors.begin(); // initialize the sensor } void loop() { sensors.requestTemperatures(); // send the command to get temperatures temperature = sensors.getTempCByIndex(0); // read temperature in Celsius 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 } delay(500); }

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

Quick Steps

  • 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
  • ESP32 add library
  • Search “OneWire”, then find the OneWire library by Paul Stoffregen
  • Click Install button to install OneWire library.
  • ESP32 onewire library
  • Search “Dallas”, then find the DallasTemperature library by Miles Burton.
  • Click Install button to install DallasTemperature library.
  • ESP32 Dallas Temperature library
  • Copy the above code and paste it to 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

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.


Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.


  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere