ESP8266 - Heating Element

This tutorial instructs you how to ESP8266 to control a heating element. Subsequently, you will be able to apply this knowledge to create a heating system in a different tutorial.

You can drive the heating element from the NodeMCU through a relay or through a MOSFET module. The relay only switches the heater fully ON or fully OFF. The MOSFET module switches it too, but it can also give the heater part of the power with PWM, so you choose how much heat it makes. It is silent and has no contacts that wear out when it switches often. We will go through both methods here.

Hardware Preparation

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12F (SMD-form)
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×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266

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

The Heating Element Pinout

ESP8266 NodeMCU heating element pinout

The heating element typically has two pins:

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

How to Control Heating Element using ESP8266

If a 12V heating element is powered by a 12V power supply, it will emit heat. In order to control the heating element, a relay must be used between the ESP8266 and the heating element. The ESP8266 can then control the heating element through the relay.

If you are not familiar with relays (pinout, how it works, how to program, etc.), please refer to the ESP8266 - Relay tutorial for more information.

For the second method, you need the MOSFET module. Its pinout and its inner working are covered in the ESP8266 - MOSFET Module tutorial.

Relay vs MOSFET Module for a Heating Element

Feature Relay MOSFET Module
What it can do full heat or no heat full heat, no heat, or any heat level in between (PWM)
Sound clicks on every switch quiet
Speed slow (milliseconds) very fast
Wear contacts wear out, more so if it switches often nothing moves, so nothing wears out
Heater type AC or DC DC only
Heater supply any value the relay is rated for 6V or higher
Isolation yes, between coil and contacts yes, through the PC817 optocoupler
Use it for AC heaters, plain ON/OFF DC heaters, heat level control, frequent switching

A temperature control loop often switches the heater many times per minute. For a 12V DC heating element, the MOSFET module handles this better, and it lets you set the heat level. An AC heater still needs the relay.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and heating element

This image is created using Fritzing. Click to enlarge image

ESP8266 Code

The code below will turn the heating element ON every five seconds and OFF every five seconds, . repeatedly.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-heating-element */ #define RELAY_PIN D8 // The ESP8266 pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize the ESP8266 pin 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

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 the USB cable into the ESP8266 and your computer.
  • Launch the Arduino IDE, select the appropriate board and port.
  • Paste the code into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the ESP8266.
  • Check out the temperature of the heating element.

WARNING

Take caution. It can cause harm to you and your home. This is an important matter and we want you to be secure. If you are not completely certain of what you are doing, it is best to not meddle with anything. Consult someone who is knowledgeable! We cannot be held responsible for your safety.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

ESP8266 - Set Heat Level with MOSFET Module

In this part, a MOSFET module takes the place of the relay. Pin D5 of the NodeMCU sends PWM to the module, and the heating element gets more or less power.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and NodeMCU heating element MOSFET module

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.

  • D5 (GPIO14) on the NodeMCU goes to PWM on the module.
  • G (GND) on the NodeMCU goes to GND on the module.
  • The plus wire of the 12V adapter and the red (+) wire of the heating element both go into the + terminal.
  • The black (−) wire of the heating element goes to the LOAD terminal.
  • The minus wire of the 12V adapter goes to the − terminal.

A heating element is just a resistor, not a coil, so no flyback diode is needed.

ESP8266 Code

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

This code gives the heater full power for 2 seconds, then no power for 2 seconds. Next, the power rises from 10% to 100% and falls back to 0%. A heater warms up and cools down slowly, so you will not feel each step at once. To feel a clear change, keep it at one power level for a longer time.

Detailed Instructions

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

Tip: the MOSFET module switches DC only, and it needs a heater supply of about 6V or more. For a heater that runs on AC mains, use 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!