Arduino Nano ESP32 - Control Fan
This tutorial provides instructions on how to use Arduino Nano ESP32 to control a fan.
There are two ways to drive the fan from the Nano ESP32: a relay or a MOSFET module. A relay is a simple switch, so the fan is either fully ON or fully OFF. A MOSFET module also turns the fan ON and OFF, and on top of that it lets you set the fan speed with PWM, with no click sound at all. We will go through both ways on this page.
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.
This tutorial provides instructions on how to use Arduino Nano ESP32 to turn on/off a fan. Controlling the speed of a fan will be presented in another tutorial.
Because the fan uses the high voltage, we cannot connect it directly to ESP32, we need to connect fan to Arduino Nano ESP32 indirectly via a relay. We have specific tutorials about relay (pinout, how it works, how to program ...), learn about relay in the Arduino Nano ESP32 - Relay tutorial
The other way is a MOSFET module. Its pinout, how it works and its basic code are in the Arduino Nano ESP32 - MOSFET Module tutorial.
Relay or MOSFET Module for a Fan?
| Relay | MOSFET Module | |
|---|---|---|
| What you can do with the fan | turn it ON or OFF | turn it ON or OFF, and change its speed (PWM) |
| Noise | a click each time it switches | no noise |
| How fast it switches | slow, a few milliseconds | very fast |
| How long it lasts | the metal contacts wear out | nothing moves inside, so it lasts long |
| Fan type | AC or DC fan | DC fan only |
| Fan power supply | any voltage within the relay rating | about 6V or higher |
| Board protection | coil is separate from the contacts | PC817 optocoupler |
| Good choice when | the fan runs on AC, or ON/OFF is enough | a DC fan needs speed control or switches often |
For a 12V DC cooling fan, the MOSFET module is the better pick, since a cooling system can run the fan slowly when it is a bit warm and fast when it is hot. Keep the relay for AC fans and for 5V fans, because the MOSFET module needs a supply of about 6V or more.
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.
Arduino Nano ESP32 Code
The below code periodically turns the fan ON/OFF in every 10 seconds.
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM 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
- See the fan's state
Line-by-line Code Explanation
The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!
How to Control Fan Speed with MOSFET Module
In this part, we swap the relay for a MOSFET module. The Nano ESP32 then changes the fan speed, not just ON and OFF.
Wiring Diagram with MOSFET Module

This image is created using Fritzing. Click to enlarge image
- Arduino Nano ESP32 pin D9 → PWM pin of the MOSFET module
- Arduino Nano ESP32 GND → GND pin of the MOSFET module
- 12V adapter + → + terminal
- Fan red wire (+) → the same + terminal
- Fan black wire (−) → LOAD terminal
- 12V adapter − → − terminal
- Flyback diode (1N4007 or 1N5819) across the two fan wires: the stripe side (cathode) to fan +, the other side to fan −. The fan has a motor coil inside, so the diode protects the module.
Arduino Nano ESP32 Code - Fan Speed Control
This is the same code as in the MOSFET module tutorial. The fan runs at full speed for 2 seconds, stops for 2 seconds, then speeds up from 10% to 100% and slows down again. A fan may not start at a very low power, so it can stay still for the first steps.
Detailed Instructions
- Set up the Arduino IDE as in the relay Quick Steps above.
- Wire the fan and the MOSFET module as in the diagram. Keep the 12V adapter unplugged.
- Copy the code above, paste it into the Arduino IDE and upload it to the Arduino Nano ESP32.
- Plug in the 12V adapter.
- Open the Serial Monitor at 9600 baud.
- Listen to the fan: it speeds up and slows down again and again.
Tip: the MOSFET module works only with DC, and the fan supply must be about 6V or more. For an AC fan, use the relay way.