Arduino Nano ESP32 - Control Heating Element

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

The Nano ESP32 can switch the heating element through a relay or through a MOSFET module. The relay gives only two states: heating at full power, or no heating. The MOSFET module can do the same ON and OFF, but it can also set the heat level (the power) with PWM. It makes no sound and has no contacts to wear out, even when it switches very often. This page shows the relay way first, then the MOSFET way.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Relay
1×Alternatively, MOSFET Module (AOD4184, LR7843 or FR120N)
1×Heating Element
1×12V Power Adapter
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

Or you can buy the following kits:

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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Heating Element

Pinout

Arduino Nano 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 Arduino Nano ESP32 and Heating Element. Arduino Nano 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 Arduino Nano ESP32 - Relay tutorial

We can also put a MOSFET module in place of the relay. New to this module? Read the Arduino Nano ESP32 - MOSFET Module tutorial first.

Heating Element: Relay vs MOSFET Module

Relay MOSFET Module
Heat control full heat or no heat full heat, no heat, or any level between (PWM)
Sound clicks on every switch silent
Switching time milliseconds (slow) very short
Wear contacts burn and wear when switching often no moving parts, no contact wear
Supply AC or DC DC only
Minimum supply voltage none (within the relay rating) about 6V
Isolation from the board yes, coil and contacts yes, PC817 optocoupler
Use it for AC heaters, simple ON/OFF DC heaters, power control, frequent switching

A heating system often turns the heater ON and OFF many times to hold a temperature. A relay wears out fast with that, so for a 12V DC heating element the MOSFET module is a good choice. For a mains (AC) heater, you must stay with the relay.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Heating Element

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

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

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-controls-heating-element */ #define RELAY_PIN D2 // The Arduino Nano ESP32 pin connected to the heating element via the relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely 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); }

Detailed Instructions

  • Connect Arduino Nano 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 Arduino Nano 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 Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!

Arduino Nano ESP32 Code - Heating Element Power with MOSFET Module

Here, a MOSFET module drives the heating element, so the Nano ESP32 can set how much heat it gives.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and heating element MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
Arduino Nano ESP32 D9 MOSFET module PWM pin
Arduino Nano ESP32 GND MOSFET module GND pin
12V adapter + + terminal
Heating element + (red) + terminal (shared 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 code is the one from the MOSFET module tutorial. It powers the heater fully for 2 seconds and turns it off for 2 seconds. Then it raises the power from 10% to 100% and lowers it back to 0%. A heater warms up and cools down slowly, so you feel the change in heat only after some time, not at each step.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-controls-heating-element */ const int MOSFET_PIN = D9; // Arduino Nano ESP32 pin 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); }

Detailed Instructions

  • Prepare the board and the Arduino IDE in the same way as the relay Quick Steps above.
  • Make the wiring in the diagram, with the 12V adapter not plugged in yet.
  • Upload the code above to the Arduino Nano ESP32.
  • Plug in the 12V adapter.
  • Open the Serial Monitor with a baud rate of 9600 and see the power level.
  • Do not touch the heating element. It gets hot.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' 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
Arduino Nano ESP32 on COM15
2

Note: use the MOSFET module only with a DC heating element and a supply of about 6V or more. A heater on AC mains needs the relay.

Video Tutorial

※ OUR MESSAGES

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