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:

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.

Arduino UNO R4 fan

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.
Fan Pinout
image source: diyables.io

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

The wiring diagram between Arduino UNO R4 Fan

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.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-controls-fan */ #define RELAY_PIN 3 // The Arduino UNO R4 pin 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

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

The wiring diagram between Arduino UNO R4 cooling fan MOSFET module

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

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-controls-fan */ const int MOSFET_PIN = 9; // Arduino UNO R4 pin 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); }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' 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
Arduino Uno R4 WiFi on COM15
2

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

Video Tutorial

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