ESP32 S3 - Control Fan

Learn how to control a DC cooling fan with your ESP32 S3 using a relay module. This beginner-friendly project walks you through wiring, coding, and testing a fan that turns on and off automatically with simple Arduino code.

What you'll build:

  1. A relay-controlled fan circuit driven by the ESP32 S3
  2. Arduino code that cycles the fan on and off every 10 seconds
  3. Serial Monitor output showing real-time fan status
  4. A foundation for building temperature-triggered cooling systems
ESP32 S3 - Controls Fan

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Relay
1×12V DC Cooling Fan
1×Alternatively, 5V DC Cooling Fan
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following kits:

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

A DC fan is an electric cooling device that runs on direct current power and moves air for ventilation or temperature regulation. These fans are widely used in electronics projects because they operate on standard voltages and are easy to integrate with microcontrollers like the ESP32 S3.

Key Specifications

DC fans commonly run on 5V, 12V, or 24V DC and connect via two wires: a positive (red) lead and a negative (black) lead. When supplied with a steady voltage they spin at constant speed, and when driven with a PWM signal the speed becomes variable. They draw relatively little current, operate quietly, and are available at low cost, making them an ideal choice for beginner cooling and ventilation projects.

Pinout

A DC fan has two connection wires for power:

Fan Pinout
image source: diyables.io
  • Negative (-) pin (black wire): Connect to the negative/ground of your DC power supply
  • Positive (+) pin (red wire): Connect to the positive of your DC power supply

How to Control Fan

There are several ways to control a DC fan from a microcontroller. Running the fan at full speed simply requires connecting it directly to its rated voltage. Speed control is achieved with a PWM signal that varies the average power delivered to the motor. On/off control — the method covered in this tutorial — uses a relay to switch the fan's power supply on and off, which keeps the high-voltage fan circuit completely isolated from the ESP32 S3's GPIO pins. Learn more about relays in our ESP32 S3 - Relay tutorial.

Wiring Diagram

Connect your ESP32 S3, relay, fan, and power supply following the diagram below. The relay sits between the ESP32 S3 and the fan, protecting the microcontroller from the fan's operating voltage.

Safety Notes

Always match your power supply voltage to your fan's rated voltage — using 12V on a 5V fan or vice versa can damage the component. Never connect the fan's power wires directly to ESP32 S3 GPIO pins; the relay is essential for safe isolation. Double-check all connections before powering the circuit.

The wiring diagram between ESP32 S3 Fan

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Pin
Relay Signal (IN) D7
Relay VCC 5V
Relay GND GND
Fan Positive (+) Relay COM (Common)
Fan Negative (-) Power Supply Negative (-)
Relay NO (Normally Open) Power Supply Positive (+)

ESP32 S3 Code

The following code demonstrates on/off fan control using the ESP32 S3 and a relay. It uses a simple timed loop to cycle the fan on for 10 seconds and off for 10 seconds, printing the current status to the Serial Monitor so you can verify the relay is switching correctly without needing to watch the fan itself.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-controls-fan */ #define RELAY_PIN 7 // The ESP32 S3 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the relay, fan, and power supply to the ESP32 S3 following the wiring diagram above.
  3. Connect the ESP32 S3 to your computer using the USB Type-C cable.
  4. Open the Arduino IDE on your computer.
  5. Select ESP32 S3 and its COM port from the Arduino IDE board selector.
  6. Copy the code above and paste it into a new Arduino IDE sketch.
  7. Click the Upload button to compile and flash the code to your ESP32 S3.
  8. Watch the fan turn on and off automatically in 10-second intervals.
  9. Open the Serial Monitor (set to 115200 baud) to view the fan status messages in real time.
  10. Pro Tip: Add an LED in parallel with the relay coil indicator to visually confirm switching even when the fan is disconnected.

Line-by-line Code Explanation

The above ESP32 S3 code contains line-by-line explanation. Please read the comments in the code!

Serial Monitor Output

Open the Serial Monitor in Arduino IDE to see the fan control status:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-01-15 10:23:01] Fan is ON [2026-01-15 10:23:11] Fan is OFF [2026-01-15 10:23:21] Fan is ON [2026-01-15 10:23:31] Fan is OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Applications and Project Ideas

Fan control with the ESP32 S3 opens the door to a wide range of practical cooling and automation projects across home, greenhouse, and industrial settings.

  1. Temperature-controlled cooling system: Automatically turn the fan on when temperature exceeds a set threshold using a DHT or DS18B20 sensor.
  2. Humidity ventilation system: Activate the fan when humidity climbs too high in a room or enclosure.
  3. Computer case cooling: Build a smart cooling solution that reacts to measured component temperatures.
  4. Greenhouse climate control: Regulate air circulation to maintain optimal growing conditions for plants.
  5. Bathroom exhaust timer: Run the fan automatically for a fixed duration after detecting elevated humidity.
  6. IoT smart ventilation: Control the fan remotely over WiFi using the ESP32 S3's built-in wireless connectivity.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

These hands-on challenges will help you deepen your understanding of fan control and relay-based switching on the ESP32 S3.

  1. Beginner: Change the on/off timing from 10 seconds to 5 seconds and observe the difference.
  2. Beginner: Add a push button so you can manually toggle the fan on and off instead of relying on automatic timing.
  3. Intermediate: Connect a DHT11 sensor and turn the fan on only when the temperature exceeds 25°C.
  4. Intermediate: Control two fans independently using two separate relays and two different GPIO pins.
  5. Advanced: Implement PWM speed control to vary the fan speed smoothly from 0 to 100%.
  6. Advanced: Build a web interface hosted on the ESP32 S3 to control fan speed and monitor temperature remotely.

※ 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!