Arduino Nano ESP32 - Control Fan

This tutorial provides instructions on how to use Arduino Nano ESP32 to control a fan.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Power Adapter
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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. We appreciate your support.

Overview of DC Fan


Fan Pinout
image source:

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

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 * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #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!

Video Tutorial


  • 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!