ESP32 - Control Heating Element

This tutorial instructs you how to use ESP32 to control a Heating Element. Based on this tutorial, you will learn how to make a heating system in another tutorial.

The ESP32 can switch the heating element through a relay or through a MOSFET module. The relay gives you two states only: heating at full power, or no heating. The MOSFET module also lets you pick a heat level in between, such as 30% or 70% of the power. It is silent too, and it does not wear out when it switches often. We will go through both methods here.

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - Controls Heating Element tutorial.

Introduction to Heating Element

Pinout

ESP32 Heating Element Pinout

Heating Element usually has two pins:

  • Positive (+) pin (red): needs to be connected to 12V of DC power supply
  • Negative (-) pin (black): needs to be connected to GND of DC power supply

How to Control Heating Element

If 12V Heating Element is powered by 12V power supply, it emits heat. To control a Heating Element, we need to use a relay in between ESP32 and Heating Element. ESP32 can control the Heating Element via the relay. If you do not know about relay (pinout, how it works, how to program ...), learn about relay in the ESP32 - Relay tutorial

A MOSFET module can stand in place of the relay. New to it? Read the ESP32 - MOSFET Module tutorial to see its pinout and how it works.

Relay vs MOSFET Module for Heating Element

Relay MOSFET Module
Heat control full heat or no heat any heat level from 0% to 100% (PWM)
Sound when switching click none
Switching time slow (milliseconds) very fast
Wear contacts get worn, more so with frequent switching no moving parts, no contact wear
Supply type AC or DC heaters DC heaters only
Minimum heater voltage none (within the relay rating) about 6V
Isolation yes, between coil and contacts yes, by a PC817 optocoupler
Use it for AC heaters, simple ON/OFF heating 12V DC heaters, adjustable heat, switching many times

For a 12V DC heating element, choose the MOSFET module when you want to hold a gentle heat or switch very often. If the heater runs on AC mains power, only the relay is suitable.

Wiring Diagram

  • How to connect ESP32 and controls heating element using breadboard (powered via USB cable)
ESP32 Heating Element Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and controls heating element using breadboard (powered via Vin pin)
ESP32 Heating Element Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and controls heating element
How to wire ESP32 and controls heating element

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

ESP32 Code

The below code repeatedly turns the Heating Element ON in five seconds and OFF in five seconds,

/* * 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-controls-heating-element */ #define RELAY_PIN 16 // ESP32 pin GPIO16 connected to the heating element via the relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on heating element 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // turn off heating element 5 seconds delay(5000); }

Quick Instructions

  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Check the Heating Element's temperature

WARNING

Please be careful. It can burn you and your house. This is a serious topic, and we want you to be safe. If you’re NOT 100% sure what you are doing, do yourself a favor and don’t touch anything. Ask someone who knows! We do NOT take any responsibility for your safety.

Code Explanation

The above ESP32 code contains line-by-line explanation. Please read the comments in the code!

ESP32 Code - Control Heating Element Power with MOSFET Module

In this part, the MOSFET module takes the place of the relay. Instead of only ON and OFF, the ESP32 sets how much power goes into the heating element.

Wiring Diagram with MOSFET Module

ESP32 heating element MOSFET module wiring diagram

This image is created using Fritzing. Click to enlarge image

Connect it like this:

  • ESP32 GPIO18 → PWM pin of the module
  • ESP32 GND → GND pin of the module
  • 12V adapter + → + terminal of the module
  • Heating element + (red) → also the + terminal, sharing it with the adapter +
  • Heating element − (black) → LOAD terminal
  • 12V adapter − → − terminal

A heating element has no coil, so no flyback diode is needed.

Code

The heater gets full power for 2 seconds and no power for 2 seconds. Then the code raises the power step by step from 10% to 100% and brings it back down. A heater warms up and cools down slowly, so you will not feel each step. You will feel the change in temperature only after a while.

/* * 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-controls-heating-element */ const int MOSFET_PIN = 18; // ESP32 GPIO18 connected to the PWM pin of the MOSFET module void setup() { Serial.begin(9600); pinMode(MOSFET_PIN, OUTPUT); analogWrite(MOSFET_PIN, 0); // start with the load OFF } void setLoad(int percent) { int duty = map(percent, 0, 100, 0, 255); // convert 0-100% to 0-255 analogWrite(MOSFET_PIN, duty); Serial.print("Load power: "); Serial.print(percent); Serial.println("%"); } void loop() { // turn the load fully ON, then fully OFF Serial.println("Load: ON"); setLoad(100); delay(2000); Serial.println("Load: OFF"); setLoad(0); delay(2000); // increase the power step by step (PWM) for (int percent = 10; percent <= 100; percent += 10) { setLoad(percent); delay(500); } // decrease the power step by step (PWM) for (int percent = 90; percent >= 0; percent -= 10) { setLoad(percent); delay(500); } delay(1000); }

Quick Instructions

  • Prepare the ESP32 and Arduino IDE as described in the relay Quick Steps above.
  • Make the wiring as shown in the MOSFET diagram, with the 12V adapter not yet plugged in.
  • Paste the code into Arduino IDE and click Upload.
  • Plug in the 12V adapter.
  • Open the Serial Monitor and set the baud rate to 9600.
  • Read the power level on Serial Monitor. Do not touch the heating element to check it, because it can be very hot.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Tip: keep the heater supply at about 6V or higher, and use DC only. For an AC heater, stay with the relay.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES