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.

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

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)
ESP32 Fan Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and controls fan using breadboard (powered via Vin pin)
ESP32 Fan Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and controls fan
How to wire ESP32 and controls fan

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.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-controls-fan */ #define RELAY_PIN 16 // ESP32 pin GPIO16, which connects to the fan the via the relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on fan 10 seconds delay(10000); digitalWrite(RELAY_PIN, LOW); // turn off fan 10 seconds delay(10000); }

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

ESP32 cooling fan MOSFET module wiring diagram

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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-controls-fan */ const int MOSFET_PIN = 18; // ESP32 GPIO18 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); }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev 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
ESP32 Dev Module on COM15
2

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.

※ OUR MESSAGES