Arduino Nano ESP32 - Control Fan
This tutorial provides instructions on how to use Arduino Nano ESP32 to control a fan.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
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
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!