ESP32 - Control Fan
This tutorial instructs you how to use ESP32 to control a fan.
There are two ways to drive the fan: a relay or a MOSFET module. A relay can only switch the fan ON or OFF. A MOSFET module can do that too, and it can also change how fast the fan spins. This page shows both ways, step by step.
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 Fan tutorial.
Hardware Used In This Tutorial
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Introduction to 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.
This tutorial instructs you how to use ESP32 to turn on/off a fan with a relay. Controlling the speed of a fan with a MOSFET module is shown at the end of this tutorial.
Because the fan uses the high voltage, we cannot connect it directly to ESP32, we need to connect fan to ESP32 indirectly via a relay. We have specific tutorials about relay (pinout, how it works, how to program ...), learn about relay in the ESP32 - Relay tutorial
The MOSFET module is the other choice. Its pins, wiring and working principle are explained in the ESP32 - MOSFET Module tutorial.
Relay vs MOSFET Module for Fan
| Relay | MOSFET Module | |
|---|---|---|
| What you can control | ON and OFF only | ON, OFF and fan speed (PWM) |
| Noise | a click each time it switches | no sound |
| How fast it switches | slow, a few milliseconds | very fast |
| Life | the contacts wear out slowly | nothing moves inside |
| Fan type | AC or DC fan | DC fan only |
| Fan supply voltage | any, within the relay rating | about 6V or more |
| Isolation from ESP32 | yes, coil and contacts are separate | yes, PC817 optocoupler |
| Good choice when | the fan runs on AC, or ON/OFF is enough | a DC fan needs speed control |
For a 12V DC fan, the MOSFET module is the better pick, because you can run the fan slowly and quietly when full speed is not needed. The 5V fan does not suit the MOSFET module, because the module needs a supply of about 6V or more. Use the relay for a 5V fan or an AC fan.
Wiring Diagram
- How to connect ESP32 and controls fan using breadboard (powered via USB cable)

This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and controls fan using breadboard (powered via Vin pin)

This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and controls fan using screw terminal block breakout board (powered via USB cable)

- How to connect ESP32 and controls fan using screw terminal block breakout board (powered via Vin pin)

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 periodically turns the fan ON/OFF in every 10 seconds.
Quick Instructions
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
- Do the wiring as above image.
- Connect the ESP32 board to your PC via a USB Type-C cable
- Open Arduino IDE on your PC.
- Select the right ESP32 board (e.g. ESP32 Dev Module) and COM 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
- See the fan's state
Line-by-line Code Explanation
The above ESP32 code contains line-by-line explanation. Please read the comments in the code!
How to Control Fan Speed with MOSFET Module
Now let's swap the relay for a MOSFET module. The ESP32 sends a PWM signal to the module, and the 12V fan speeds up or slows down with it.
Wiring Diagram for Fan and MOSFET Module

This image is created using Fritzing. Click to enlarge image
| From | To |
|---|---|
| ESP32 GPIO18 | Module PWM pin |
| ESP32 GND | Module GND pin |
| 12V adapter + | Module + terminal |
| Fan + (red wire) | Module + terminal (together with the adapter +) |
| Fan − (black wire) | Module LOAD terminal |
| 12V adapter − | Module − terminal |
A fan has a coil inside, so put a flyback diode (1N4007 or 1N5819) across the two fan wires. The striped end (cathode) goes to the fan + wire. If your fan has a third yellow wire, leave it free.
ESP32 Code for Fan Speed
The code first runs the fan at full speed for 2 seconds and stops it for 2 seconds. After that, it raises the power from 10% to 100%, so the fan spins faster and faster, then lowers it again, so the fan slows down. Note that many fans do not start at very low power, so the fan may stay still at 10% or 20%.
Quick Instructions
- Set up Arduino IDE and the board the same way as in the relay Quick Steps above.
- Wire the fan, the MOSFET module and the 12V adapter as in the diagram above. Keep the adapter unplugged.
- Copy the above code, paste it to Arduino IDE and upload it to the ESP32.
- Plug in the 12V adapter.
- Open the Serial Monitor with baud rate 9600.
- Listen and watch the fan change its speed, and check the Serial Monitor.
Tip: the MOSFET module works with DC only and needs a fan supply of about 6V or higher. For an AC fan, keep using 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.