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

1×Arduino Nano ESP32
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×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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.

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

The wiring diagram between Arduino Nano ESP32 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.

Arduino Nano ESP32 Code

The below code periodically turns the fan ON/OFF in every 10 seconds.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-controls-fan */ #define RELAY_PIN D2 // The Arduino Nano ESP32 pin connected to the fan the via the relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin A5 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 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

The wiring diagram between Arduino Nano ESP32 and cooling fan 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.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-controls-fan */ const int MOSFET_PIN = D9; // Arduino Nano ESP32 pin 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); }

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

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.

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!