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
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of DC Fan
Pinout

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

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.
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

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
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.
※ 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.