ESP32 S3 UNO - Control Fan

This ESP32 S3 Uno fan tutorial shows you how to turn a 12V or 5V DC cooling fan on and off with a relay. The ESP32 S3 Uno form board cannot power a fan directly, so a relay switches the fan's own power supply. You can use the same idea for a cooling fan in a box, a small air vent, or any DC fan project.

There are two ways to switch the fan: a relay or a MOSFET module. A relay can only start and stop the fan. A MOSFET module can also do that, and on top of it, it can make the fan spin faster or slower with PWM, without any click. This page covers both ways.

In this tutorial, you will:

  1. Learn the pinout of a DC cooling fan
  2. Wire a 12V fan and a relay module to the ESP32 S3 Uno
  3. Write Arduino code to turn the fan on and off
  4. Upload the code from Arduino IDE and test the fan
  5. Swap the relay for a MOSFET module and change the fan speed step by step
ESP32 S3 Uno fan

Overview of DC Fan

A DC fan is a simple part, but it needs more power than a board pin can give. Knowing its pins and voltage helps you wire it safely.

Pinout

Most small DC fans have only two wires. The black wire is the negative (-) pin and goes to the negative side of the DC power supply. The red wire is the positive (+) pin and goes to the positive side of the power supply.

Fan Pinout
image source: diyables.io

Always match the power supply to the fan's rated voltage. This tutorial uses a 12V fan, and a 5V fan works the same way with a 5V supply.

How to Control Fan

There are two common ways to drive a DC fan. You can switch it fully on and off, or you can change its speed.

Full speed on/off

When the fan gets its full rated voltage (12V or 5V), it spins at full speed. Cutting the power stops it.

Speed control

When the fan gets a 12V or 5V PWM signal, you can change its speed. That topic is covered in a separate tutorial.

Why use a relay

In this tutorial, you only switch the fan on and off. The ESP32 S3 Uno pins give just 3.3V and a small current, so they cannot run the fan. A relay sits between the board and the fan. The board sends a small signal to the relay, and the relay connects or cuts the fan's own power supply. If relays are new to you, read the ESP32 S3 Uno relay guide first to learn how they work and how to program them.

The MOSFET module can take the place of the relay. Its pins, its inside parts and its basic code are all explained in the ESP32 S3 Uno - MOSFET Module tutorial, so read it first if this part is new to you.

Relay vs MOSFET Module for DC Fan

Relay MOSFET Module
What you can do with the fan start and stop only start, stop and set the speed (PWM)
Noise a click at every switch no sound
Switching time slow, a few milliseconds very fast
Life the contacts wear out slowly nothing moves, so nothing wears
Device type AC or DC fans DC fans only
Fan supply any voltage the relay is rated for about 6V or more (the gate needs it)
Separation from the board yes, coil and contacts yes, PC817 optocoupler
Good choice for AC fans, plain ON/OFF 12V DC fans with speed control, frequent switching

For a 12V DC cooling fan, the MOSFET module is the better pick, because you can run the fan slowly when the box is only a little warm. Keep the relay if your fan runs on AC power or if you use a 5V fan (5V is too low for the module).

ESP32 S3 Uno Pinout

The pinout diagram below shows the ESP32 S3 Uno form board. Use it to find the Uno header pins (D3, 5V, GND…) and their GPIO numbers while you wire the fan circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The relay input goes to D3 (GPIO17). The fan and the 12V power adapter connect through the relay's screw terminals.

The wiring diagram between ESP32 S3 Uno Fan

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Relay VCC 5V
Relay GND GND
Relay IN D3 (GPIO17)

On the high-power side, connect the positive wire of the 12V power adapter (through the DC power jack) to the relay COM terminal. Connect the relay NO terminal to the fan's red (+) wire. Connect the fan's black (-) wire to the negative wire of the power adapter.

WARNING

Never power the fan from the ESP32 S3 Uno pins. The 12V supply must only go to the relay terminals and the fan. The ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant. The relay module may use 5V on its VCC pin, because its IN pin only receives a signal from the board.

ESP32 S3 Uno Code

This sketch turns the fan on for five seconds and then off for five seconds, again and again. It sets D3 (GPIO17) as an output and writes HIGH or LOW to it to switch the relay.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-controls-fan */ #define RELAY_PIN 17 // The ESP32 S3 Uno pin D3 (GPIO17) connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 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); }

Detailed Instructions

Do these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Plug in the 12V power adapter for the fan.
  8. Watch the fan: it should run for five seconds, stop for five seconds, and repeat.
  9. Tip: you should hear the relay click each time the fan changes state. If the fan works the opposite way, your relay module may be active LOW, so swap HIGH and LOW in the code.

Code Explanation

The comments inside the code above explain each line. In short, pinMode() makes GPIO17 an output in setup(), and loop() writes HIGH (fan on) and LOW (fan off) with a 5000 ms delay() between them.

How to Control Fan Speed with MOSFET Module

Now let's replace the relay with a MOSFET module. The fan can still go fully on and off, but you can also slow it down and speed it up from code.

Wiring Diagram

The wiring diagram between ESP32 S3 Uno cooling fan MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
ESP32 S3 Uno D9 (GPIO46) MOSFET module PWM
ESP32 S3 Uno GND MOSFET module GND
12V adapter + (through DC power jack) MOSFET module +
Fan red (+) wire MOSFET module + (shared with the adapter +)
Fan black (-) wire MOSFET module LOAD
12V adapter − (through DC power jack) MOSFET module −

A fan has a motor coil inside, so put a flyback diode (1N4007 or 1N5819) across the two fan wires. The stripe side (cathode) goes to the fan + wire. If your fan has a yellow third wire, leave it unconnected.

WARNING

D9 (GPIO46) is a boot strapping pin. The module input is safe for it, but if an upload fails, unplug the PWM wire from D9, upload, and plug it back in.

ESP32 S3 Uno Code

This is the same sketch as in the MOSFET module tutorial. The fan runs at full speed for 2 seconds and stops for 2 seconds. Then it speeds up from 10% to 100% and slows down again, step by step.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-controls-fan */ const int MOSFET_PIN = 46; // ESP32 S3 Uno pin D9 (GPIO46) connected to the PWM pin of the MOSFET module void setup() { Serial.begin(9600); pinMode(MOSFET_PIN, OUTPUT); analogWrite(MOSFET_PIN, 0); // start with the load OFF } void setLoad(int percent) { int duty = map(percent, 0, 100, 0, 255); // convert 0-100% to 0-255 analogWrite(MOSFET_PIN, duty); Serial.print("Load power: "); Serial.print(percent); Serial.println("%"); } void loop() { // turn the load fully ON, then fully OFF Serial.println("Load: ON"); setLoad(100); delay(2000); Serial.println("Load: OFF"); setLoad(0); delay(2000); // increase the power step by step (PWM) for (int percent = 10; percent <= 100; percent += 10) { setLoad(percent); delay(500); } // decrease the power step by step (PWM) for (int percent = 90; percent >= 0; percent -= 10) { setLoad(percent); delay(500); } delay(1000); }

Detailed Instructions

  1. Set up the board and Arduino IDE in the same way as in the relay Quick Steps above.
  2. Wire the fan and the MOSFET module as shown in the diagram, with the 12V adapter unplugged.
  3. Copy the code, paste it into Arduino IDE and upload it.
  4. Plug in the 12V adapter and open the Serial Monitor at 9600 baud.
  5. Listen to the fan: the air flow gets stronger, then weaker. Many fans do not spin below about 30%, which is normal.
∞
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
Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Tip: the MOSFET module works with DC only and needs a fan supply of about 6V or more. For an AC fan or a 5V fan, stay with the relay.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Can I connect a 12V fan directly to the ESP32 S3 Uno?

No. An ESP32 S3 Uno pin gives only 3.3V and a few milliamps. A 12V fan needs much more voltage and current. Use a relay or a transistor/MOSFET with its own power supply.

Can I power the relay module from the 3.3V pin of the ESP32 S3 Uno?

Most relay modules have a 5V coil, so power their VCC from the 5V pin. The IN pin can still take the 3.3V signal from the ESP32 S3 Uno on most modules. If your relay does not click, use a 3V relay module or a module with a transistor driver.

Can I control the fan speed with the ESP32 S3 Uno?

Not with a relay. A relay can only switch the fan fully on or off. To change the speed, use a PWM signal with analogWrite() and a MOSFET driver, or a 4-pin PWM fan.

Can I use a 5V fan instead of a 12V fan?

Yes. The wiring and code stay the same. Just use a 5V power supply for the fan on the relay side, so the voltage matches the fan.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 are I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 are I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For more fans or relays, GPIO35 to GPIO42 are a good choice. For a temperature sensor, use GPIO15 or GPIO16. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver is missing or the cable is charge-only Install the CP210x or CH340 driver, try a data USB cable, or hold BOOT while pressing RESET
Relay never clicks Relay VCC or GND is not connected, or the relay needs 5V Power the relay VCC from 5V and check the GND wire
Relay clicks but the fan does not spin The 12V adapter is not plugged in or the fan is on the NC terminal Plug in the adapter and move the fan wire to the NO terminal
Fan runs the opposite way to the code The relay module is active LOW Swap HIGH and LOW in the digitalWrite() lines
Fan spins slowly or stutters The power supply voltage or current is too low Use a supply that matches the fan voltage and has enough current
Board resets when the fan starts Fan and board share a weak supply Power the fan from its own adapter and keep only the signal on the board

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