ESP8266 - Cooling System using DS18B20 Temperature Sensor

This tutorial instructs you how to use ESP8266 to control the temperature with a fan and a DS18B20 temperature sensor.

If you would prefer to use a DHT11 or DHT22 sensor instead of the DS18B20, please refer to ESP8266 - Cooling System using DHT Sensor.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
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

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Cooling Fan and DS18B20 Temperature Sensor

The tutorial utilizes a cooling fan that is powered by a 12v supply. If power is provided, the fan will turn on and if it is removed, the fan will turn off. To control the fan with an ESP8266, a relay must be used as an intermediary.

If you are unfamiliar with temperature sensors and fans (including pinouts, how they work, and how to program them), the following tutorials can help:

Wiring Diagram

  • Wiring diagram using a breadboard.
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.

  • Wiring diagram using a terminal adapter (recommended).
The wiring diagram between ESP8266 NodeMCU and control relay

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring.

How System Works

  • The ESP8266 checks the temperature using the temperature sensor.
  • If the temperature is higher than the set upper limit, the fan is activated by the ESP8266.
  • If the temperature is lower than the set lower limit, the ESP8266 shuts off the fan.

This procedure is repeated without end in a cycle.

If you wish to switch the fan on and off when the temperature is above or below a certain point, simply set the upper and lower thresholds to the same value.

ESP8266 Code for Cooling System with DS18B20 sensor

/* * This ESP8266 NodeMCU code was developed by * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include <OneWire.h> #include <DallasTemperature.h> #define THRESHOLD_ON 30 // upper temperature threshold #define THRESHOLD_OFF 15 // lower temperature threshold #define SENSOR_PIN D7 // The ESP8266 pin connected to DS18B20 sensor's DQ pin #define FAN_PIN D1 // The ESP8266 pin connected to relay OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. DS18B20.begin(); // initialize the DS18B20 sensor } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float temperature = DS18B20.getTempCByIndex(0); // read temperature in Celsius 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 } delay(500); }

In the code above, when the temperature is greater than 25°C, the ESP8266 will activate the fan. The fan will remain on 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.
  • Plug in the USB cable to connect ESP8266 to PC
  • Launch Arduino IDE, choose the appropriate board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Dallas”, then locate the DallasTemperature library by Miles Burton.
  • Press the Install button to install the DallasTemperature library.
ESP8266 NodeMCU Dallas Temperature library
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
ESP8266 NodeMCU onewire library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button to transfer the code to the ESP8266.
  • Change the temperature of the environment around the sensor.
  • Check the status of the fan on the Serial Monitor.

Advanced Knowledge

The method of control described above is the on-off controller, which is also referred to as a signaller or "bang-bang" controller. This method is quite straightforward to put into practice.

An alternative approach known as the PID controller exists. This method makes it possible to achieve a more stable desired temperature, however, it is complicated to comprehend and implement. As a result, the PID controller is not commonly used for temperature control.

Video Tutorial


  • 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!