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×Relay
1×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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

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

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!

Video Tutorial