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

1×ESP8266 NodeMCU
1×USB Cable Type-C
1×Relay
1×12V DC Cooling Fan
1×(Alternative) 5V DC Cooling Fan
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP8266
1×(Recommended) Power Splitter For ESP8266 Type-C

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.
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.

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

The wiring diagram between ESP8266 NodeMCU and Fan

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.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-fan */ #define RELAY_PIN D8 // The ESP8266 pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin D8 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 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!

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!