ESP8266 - Fan

This tutorial instructs you how to use ESP8266 to switch a fan on or off. We will discuss controlling the fan's speed in another tutorial.

There are two parts you can put between the NodeMCU and the fan: a relay or a MOSFET module. A relay gives the fan only two states, ON and OFF. A MOSFET module does that too, and it can also make the fan spin slower or faster with PWM, without any click. This page shows you both ways, the relay first and the MOSFET module at the end.

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×12V DC Cooling Fan
1×Alternatively, 5V DC Cooling Fan
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 DC Fan

Pinout

Fan Pinout
image source: diyables.io

A DC fan usually has two pins:

  • Negative (-) pin (black): needs to be connected to the negative wire of DC power supply
  • Positive (+) pin (red): needs to be connected to the positive wire of DC power supply

The voltage of the DC power supply should be equal to the voltage specified by the fan. In this tutorial, we will use 12VDC and 5VDC fans.

How to Control Fan

  • If DC fan is powered by 12V/5V power supply, it run with full speed.
  • If DC fan is powered by 12V/5V PWM signal, The fan's speed can be controlled.

In this tutorial, we will be discussing how to use ESP8266 to turn a fan on or off. Controlling the speed of the fan will be addressed in a separate tutorial.

To switch the fan on or off, we will need to use a relay between ESP8266 and the fan. ESP8266 can then control the fan through the relay.

If you are unfamiliar with relays (pinouts, how they work, how to program them, etc.), please refer to the ESP8266 - Relay tutorial for more information.

The MOSFET module is new to you? The ESP8266 - MOSFET Module tutorial explains its pins and how it works.

Relay vs MOSFET Module for a Fan

Relay MOSFET Module
Fan control ON or OFF ON, OFF and speed (PWM)
Noise a click each time it switches no sound
How fast it switches slow, a few milliseconds very fast
Life span the metal contacts wear out no moving parts, long life
Type of fan AC or DC fan DC fan only
Fan supply voltage any, within the relay rating about 6V or more
Isolation from the NodeMCU yes, coil and contacts are apart yes, by a PC817 optocoupler
Good choice for AC fans, plain ON/OFF DC fans that need speed control

For a 12V DC fan, the MOSFET module is the better pick, because you can set the speed and it makes no noise. Keep the relay for an AC fan or for a 5V fan, since the module needs at least about 6V.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Fan

This image is created using Fritzing. Click to enlarge image

Please note that if you use 5V fan, you need to use 5V power adapter.

ESP8266 Code

The code below will cause the fan to turn 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-fan */ #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 digital pin D8 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on fan 10 seconds delay(10000); digitalWrite(RELAY_PIN, LOW); // turn off fan 10 seconds delay(10000); }

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.
  • Attach an ESP8266 to your computer using a USB cable.
  • Launch the Arduino IDE, select the correct board and port.
  • Copy the code provided and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
  • Check out the fan's state.

Code Explanation

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

How to Control Fan Speed with MOSFET Module

Now let's swap the relay for a MOSFET module. The NodeMCU sends a PWM signal from pin D5, and the module turns it into 12V pulses for the fan, so the fan can run at any speed between stop and full.

Wiring Diagram

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

From To
NodeMCU D5 (GPIO14) module PWM
NodeMCU G (GND) module GND
12V adapter + module +
Fan red wire (+) module + (the same terminal as the adapter +)
Fan black wire (−) module LOAD
12V adapter − module −

A fan has a motor coil inside, so put a flyback diode (1N4007 or 1N5819) across the two fan wires. The end with the stripe (cathode) goes to the fan + wire. If your fan has a third yellow wire, leave it free.

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-fan */ 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); }

The fan first runs at full speed for 2 seconds and stops for 2 seconds. After that, the power goes from 10% up to 100%, so the fan speeds up step by step, and then it goes back down, so the fan slows down until it stops. At a very low power, some fans do not start at all. This is normal.

Detailed Instructions

  • Set up Arduino IDE and the board in the same way as in the relay Quick Steps above.
  • Wire the fan and the MOSFET module as shown in the diagram. Keep the 12V adapter unplugged for now.
  • Copy the code above, paste it into Arduino IDE and upload it to the NodeMCU.
  • Plug in the 12V adapter.
  • Open the Serial Monitor at 9600 baud and listen to the fan.
∞
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

※ NOTE THAT:

The MOSFET module works only with DC, and the fan supply must be at least about 6V. For an AC fan, stay with 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!