ESP32 - Heating System

In this guide, we'll learn how to make a heating sytem that controls room temperature using an ESP32, a DS18B20 temperature sensor, and a heating element. It's as simple as this: when the room feels too chilly, we'll kickstart the heating element, and when it warms up, we'll switch it off. You can tweak the code to work with other temperature sensors like the DHT11, DHT22, or LM35, so you have the flexibility to pick the sensor that suits your preferences.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
1×Relay
1×Heating Element
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

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 in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Buy Note: Numerous DS18B20 sensors available in the market are of poor quality. We strongly advise purchasing the sensor from the DIYables brand via the link above; we conducted tests, and it performed reliably.

Introduction to Heating Element and DS18B20 Temperature Sensor

The heating element we're using in this tutorial operates with a 12-volt power supply. When power is provided to the heating element, it generates heat. To enable ESP32 control over the heating element, we need to use a relay. This relay serves as the switch that allows the ESP32 to turn the heating element on or off as needed.

If you do not know about temperature sensor and heating element (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

  • Wiring diagram with breadboard
ESP32 heating system wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with adapter (recommended)
ESP32 teamperature controls heating system wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How System Works

  • ESP32 reads the temperature from the temperature sensor
  • If the temperature falls below an lower threshold, ESP32 turn on the heating elements
  • If the temperature rises above a upper threshold, ESP32 turn off the heating element

The above process is repeated infinitely in the loop.

ESP32 Code for Cooling System with DS18B20 sensor

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-heating-system */ #include <OneWire.h> #include <DallasTemperature.h> #define TEMP_UPPER_THRESHOLD 30 // upper temperature threshold #define TEMP_LOWER_THRESHOLD 15 // lower temperature threshold #define SENSOR_PIN 23 // ESP32 pin GPIO23 connected to DS18B20 sensor's DQ pin #define RELAY_PIN 18 // ESP32 pin GPIO18 connected to relay OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); void setup() { Serial.begin(9600); // initialize serial 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 > TEMP_UPPER_THRESHOLD) { Serial.println("Turn the heating element on"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (temperature < TEMP_LOWER_THRESHOLD) { Serial.println("Turn the heating element off"); digitalWrite(RELAY_PIN, LOW); // turn off } delay(500); }

In the above code, the ESP32 turn on the heating element when the temperature falls below 15°C, and keep the heating element on until the temperature is above 20°C

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “DallasTemperature” on the search box, then look for the DallasTemperature library by Miles Burton.
  • Click Install button to install DallasTemperature library.
ESP32 Dallas Temperature library
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
ESP32 onewire library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Make enviroment around sensor hotter or colder
  • Check the temperature of heating element and your room

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.

※ OUR MESSAGES