ESP32 - Control Fan

ESP32 can not only turn on/off the fan but also control the fan's speed.

In this tutorial, we are going to learn how to turn on/off a fan using ESP32. Controlling the fan's speed will be presented in another tutorial.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Relay
1×12V Cooling Fan
1×12V Power Adapter
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

How to Control Fan

  • If 12V fan is powered by 12V power supply, it run with full speed.
  • If 12V fan is powered by 12V PWM signal, The fan's speed can be controlled.

In this tutorial, we are going to learn how to turn on/off a fan using ESP32. Controlling the fan's speed will be presented in another tutorial. To turn on/off a fan, we need to use a relay in between ESP32 and fan. ESP32 can turn on/off the fan via the relay. If you do not have knowledge of relay (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about relay in the ESP32 - Relay tutorial

Wiring Diagram

ESP32 Fan Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 Code

The below code repeatedly turns the fan ON in five seconds and OFF in five seconds,

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-controls-fan */ // constants won't change const int RELAY_PIN = 27; // ESP32 pin GIOP27, which connects to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on fan 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // turn off fan 5 seconds delay(5000); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • See the fan's state

Code Explanation

Read the line-by-line explanation in comment lines of code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere