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:
- A relay-controlled fan circuit driven by the ESP32 S3
- Arduino code that cycles the fan on and off every 10 seconds
- Serial Monitor output showing real-time fan status
- A foundation for building temperature-triggered cooling systems

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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:

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

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.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the relay, fan, and power supply to the ESP32 S3 following the wiring diagram above.
- Connect the ESP32 S3 to your computer using the USB Type-C cable.
- Open the Arduino IDE on your computer.
- Select ESP32 S3 and its COM port from the Arduino IDE board selector.
- Copy the code above and paste it into a new Arduino IDE sketch.
- Click the Upload button to compile and flash the code to your ESP32 S3.
- Watch the fan turn on and off automatically in 10-second intervals.
- Open the Serial Monitor (set to 115200 baud) to view the fan status messages in real time.
- 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:
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.
- Temperature-controlled cooling system: Automatically turn the fan on when temperature exceeds a set threshold using a DHT or DS18B20 sensor.
- Humidity ventilation system: Activate the fan when humidity climbs too high in a room or enclosure.
- Computer case cooling: Build a smart cooling solution that reacts to measured component temperatures.
- Greenhouse climate control: Regulate air circulation to maintain optimal growing conditions for plants.
- Bathroom exhaust timer: Run the fan automatically for a fixed duration after detecting elevated humidity.
- 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.
- Beginner: Change the on/off timing from 10 seconds to 5 seconds and observe the difference.
- Beginner: Add a push button so you can manually toggle the fan on and off instead of relying on automatic timing.
- Intermediate: Connect a DHT11 sensor and turn the fan on only when the temperature exceeds 25°C.
- Intermediate: Control two fans independently using two separate relays and two different GPIO pins.
- Advanced: Implement PWM speed control to vary the fan speed smoothly from 0 to 100%.
- Advanced: Build a web interface hosted on the ESP32 S3 to control fan speed and monitor temperature remotely.