ESP8266 - Fan
This tutorial instructs you how to use ESP8266 to switch a fan on or off. We will discuss controlling the fan's speed in another tutorial.
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.
In this tutorial, we will be discussing how to use ESP8266 to turn a fan on or off. Controlling the speed of the fan will be addressed in a separate tutorial.
To switch the fan on or off, we will need to use a relay between ESP8266 and the fan. ESP8266 can then control the fan through the relay.
If you are unfamiliar with relays (pinouts, how they work, how to program them, etc.), please refer to the ESP8266 - Relay tutorial for more information.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
Please note that if you use 5V fan, you need to use 5V power adapter.
ESP8266 Code
The code below will cause the fan to turn ON every five seconds and OFF every five seconds, . repeatedly.
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Attach an ESP8266 to your computer using a USB cable.
- Launch the Arduino IDE, select the correct board and port.
- Copy the code provided and open it in the Arduino IDE.
- Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
- Check out the fan's state.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!