Arduino UNO R4 - Control Fan
In this tutorial, we will learn how to control a 12V or 5V fan using Arduino UNO R4. In detail, we will learn:
- How to connect the 12V/5V fan to Arduino UNO R4
- How to program Arduino UNO R4 to turn the fan on/off.
- How to use a MOSFET module instead of the relay, so the Arduino UNO R4 can also change the fan speed.
There are two ways to drive the fan. A relay is simple, but it gives only two states: the fan runs or it stops. A MOSFET module can do that too, and it can also make the fan spin faster or slower with PWM. We will try both ways in this tutorial.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables STEM V4B IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of DC Fan
Pinout
A DC fan typically has two pins.
- Negative (-) pin (black): connect to the negative wire of the DC power supply.
- Positive (+) pin (red): connect to the positive wire of the DC power supply.

Ensure that the voltage of the DC power supply matches the voltage required by the fan. In this guide, we will use fans that need 12VDC and 5VDC.
How to Control Fan
- If a DC fan is powered by a 12V/5V power supply, it will run at full speed.
- If a DC fan is powered by a 12V/5V PWM signal, you can control the fan's speed.
In this guide, we will learn how to switch a fan on and off using an Arduino UNO R4. We will cover how to control the fan speed in a different guide. To manage the fan, we use a relay between the Arduino UNO R4 and the fan. The Arduino UNO R4 controls the fan through the relay. If you are not familiar with relays (what they are, how they operate, how to program them), you can learn about them in the Arduino UNO R4 - Relay tutorial.
The MOSFET module is the second choice. The pins, the inside and the code of the module are explained in the Arduino UNO R4 - MOSFET Module tutorial.
Relay vs MOSFET Module for a DC Fan
| Relay | MOSFET Module | |
|---|---|---|
| What it can do | run or stop the fan | run or stop the fan, and set its speed (PWM) |
| Noise when switching | a click every time | no sound |
| How fast it switches | slow, a few milliseconds | very fast |
| Life | the contacts wear out after many switches | nothing moves, so it lasts long |
| Type of power | AC or DC | DC only |
| Fan power supply | any voltage the relay is rated for | about 6V or more |
| Isolation from the Arduino | yes, between coil and contacts | yes, by the PC817 optocoupler |
| Good for | AC fans, or a simple ON/OFF | DC fans that need speed control |
For a 12V DC fan, the MOSFET module is usually the better pick, because you can run the fan slowly when it is not hot and fast when it is. If your fan runs on AC mains, stay with the relay.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino UNO R4 Code
The following code continuously switches the fan ON for five seconds and then OFF for five resp.
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the Arduino Uno R4 to the fan according to the provided diagram.
- Connect the Arduino Uno R4 to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy and paste the provided code into the Arduino IDE.
- Press the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
- Observe the state of the fan.
Code Explanation
The explanation is in the comments of the above Arduino code.
How to Control Fan Speed with MOSFET Module
Now let's replace the relay with a MOSFET module. The fan can still turn on and off, but this time the Arduino UNO R4 can also change how fast it spins.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
| From | To |
|---|---|
| Arduino UNO R4 pin 9 | MOSFET module PWM |
| Arduino UNO R4 GND | MOSFET module GND |
| 12V adapter + | MOSFET module + |
| Fan red wire (+) | MOSFET module + (same terminal as the adapter +) |
| Fan black wire (−) | MOSFET module LOAD |
| 12V adapter − | MOSFET module − |
A fan has a motor coil inside, so put a flyback diode (1N4007 or 1N5819) across the fan. The side with the stripe (cathode) goes to the fan +, the other side goes to the fan −.
Arduino UNO R4 Code
What the fan does: it runs at full speed for 2 seconds and stops for 2 seconds. Then it speeds up from 10% to 100% power, step by step, and slows down again.
Detailed Instructions
- Do the same setup as in the Quick Steps of the relay part above.
- Wire the fan and the MOSFET module as shown in the diagram. Keep the 12V adapter unplugged.
- Upload the code above to the Arduino UNO R4.
- Plug in the 12V adapter.
- Open the Serial Monitor at 9600 baud.
- Listen to the fan: the sound goes up and down with the speed. At low power, some fans may not spin.
※ NOTE THAT:
The MOSFET module works with DC only, and the fan power supply must be about 6V or more. A 5V fan is not a good match for it. For a 5V fan or an AC fan, use the relay.