ESP8266 - Heating System

In this tutorial, we will be utilizing the ESP8266, heating element and DS18B20 temperature sensor to control the temperature of a room.

Additionally, the code can be adapted for other temperature sensors such as DHT11, DHT22, or LM35 instead of the DS18B20 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×Heating Element
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 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.

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 Heating Element and DS18B20 Temperature Sensor

The heating element utilized in this tutorial requires a 12v power supply. If power is supplied to the heating element, it will generate heat. To control the heating element with ESP8266, a relay must be used as an intermediary.

If you are unfamiliar with temperature sensors and heating elements (pinouts, operation, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and heating 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.

How System Works

  • ESP8266 monitors the temperature from the temperature sensor.
  • If the temperature drops below a certain lower limit, ESP8266 will activate the heating elements.
  • When the temperature exceeds a specific upper limit, ESP8266 will deactivate the heating elements.

The loop is repeated continuously.

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 SENSOR_PIN D7 // The ESP8266 pin connected to DS18B20 sensor's DQ pin #define RELAY_PIN D1 // The ESP8266 pin connected to relay which connected to heating element const int THRESHOLD_OFF = 20; // upper threshold of temperature, change to your desire value const int THRESHOLD_ON = 15; // lower threshold of temperature, change to your desire value OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. DS18B20.begin(); // initialize the sensor pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures temperature = DS18B20.getTempCByIndex(0); // read temperature in Celsius if(temperature > THRESHOLD_OFF) { Serial.println("The heating element is turned off"); digitalWrite(RELAY_PIN, LOW); // turn off } else if(temperature < THRESHOLD_ON){ Serial.println("The heating element is turned on"); digitalWrite(RELAY_PIN, HIGH); // turn on } delay(500); }

In the code above, when the temperature is lower than 15°C, the ESP8266 will activate the heating element. The heating element will remain on until the temperature rises above 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 your ESP8266 to the computer with a USB cable
  • Open the Arduino IDE and select the correct 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 above and open it with 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 environment around the sensor.
  • Check out the temperature of the heating element and the room.

Advanced Knowledge

The above mentioned technique is the on-off controller, which is also referred to as a signaller or "bang-bang" controller. It is very easy to implement this method.

An alternative approach known as the PID controller exists. This method of temperature control is more stable, but complex and challenging to comprehend and put into practice. As a result, the PID controller is not widely used.