Arduino MKR WiFi 1010 - Control Fan

Learn to control DC cooling fans with your Arduino MKR WiFi 1010! Master ON/OFF control in this tutorial, then graduate to advanced variable speed control. Perfect for temperature-regulated cooling systems, ventilation projects, and thermal management applications.

What You'll Learn:

Applications:

Arduino MKR WiFi 1010 controls Fan

Tutorial Scope: This guide covers ON/OFF switching. For variable speed control (PWM-based), see our advanced fan speed control tutorial.

There are two ways to drive the fan from the MKR board: a relay or a MOSFET module. A relay can only give the fan full power or no power. A MOSFET module can do that too, and it can also make the fan spin slower or faster. This page shows the relay way first, and the MOSFET way near the end.

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×Relay
1×Alternatively, MOSFET Module (AOD4184, LR7843 or FR120N)
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

Pinout

Fan Pinout
image source: diyables.io

A DC fan normally has two pins:

  • Negative pin (black): Connect this to the negative wire of the DC power supply.
  • Positive pin (red): Connect this to the positive wire of the DC power supply.

The DC power supply's voltage must be the same as what the fan needs. In this lesson, we will use fans that operate on 12VDC and 5VDC.

How to Control Fan

  • When a DC fan gets power from a 12V/5V power supply, it runs at full speed.
  • When a DC fan gets a 12V/5V PWM signal, you can control its speed.

This guide shows you how to use the Arduino MKR WiFi 1010 to turn a fan on and off. Another guide will explain how to control the fan’s speed.

Since the fan uses high voltage, you cannot hook it up directly to the Arduino MKR WiFi 1010. Instead, you need to connect the fan to the Arduino through a relay. We have detailed guides about relays—including wiring, operation, and programming—in our Arduino MKR WiFi 1010 - Relay tutorial.

A MOSFET module can take the place of the relay. Its pins, its three modules and how it works are all covered in the Arduino MKR WiFi 1010 - MOSFET Module tutorial.

Relay or MOSFET Module for a DC Fan?

Relay MOSFET Module
Fan control full speed or stopped full speed, stopped, or any speed between (PWM)
Noise you hear a click each time it switches no sound at all
How fast it switches slow, a few milliseconds very fast
Life the metal contacts wear out after many switches nothing moves, so nothing wears out
Device type AC or DC DC only
Fan power supply any voltage the relay is rated for 6V or more (the gate needs it)
Board protection yes, the coil is separate from the contacts yes, by the PC817 optocoupler
Good choice for AC fans, simple ON/OFF 12V DC fans with speed control

For the 12V fan, the MOSFET module is the better pick if you want a quiet fan that runs only as fast as needed. A 5V fan is a problem for the MOSFET module, because its supply is below 6V, so use the relay for it.

Wiring Diagram

The wiring diagram between Arduino MKR WiFi 1010 Fan

This image is created using Fritzing. Click to enlarge image

Remember, if you use a fan that runs on 5V, you should use a 5V power adapter.

Arduino MKR WiFi 1010 Code

The code below switches the fan on and off every 10 seconds.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-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

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  • Connect the components to the Arduino MKR WiFi 1010 board as depicted in the diagram
  • Plug your Arduino MKR WiFi 1010 into your computer's USB port
  • Launch the Arduino IDE on your computer
  • Select the Arduino MKR WiFi 1010 board and its COM port
  • Copy the code above and paste it into the Arduino IDE
  • Click the Upload button in the Arduino IDE to compile and send the code to your Arduino MKR WiFi 1010 board.
  • Check the fan's status.

Line-by-line Code Explanation

The Arduino MKR WiFi 1010 code above explains every line. Please read all the comments in the code!

How to Control Fan Speed with MOSFET Module

Now let's swap the relay for a MOSFET module. With it, the MKR board can not only start and stop the 12V fan, but also choose how fast it turns.

Wiring Diagram

The wiring diagram between Arduino MKR WiFi 1010 cooling fan MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
MKR pin 6 PWM pin of the module
MKR GND GND pin of the module
12V adapter + + terminal
Fan red wire (+) + terminal (shared with the adapter +)
Fan black wire (−) LOAD terminal
12V adapter − − terminal

The fan has a motor coil inside, so add a flyback diode (1N4007 or 1N5819) across the two fan wires. The stripe side (cathode) goes to the fan + wire.

Arduino MKR WiFi 1010 Code - Fan Speed

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

The fan runs at top speed for 2 seconds and stops for 2 seconds. Then the code raises its power from 10% to 100%, so you hear the fan speed up, and lowers it again, so the fan slows down. Some fans do not start below about 30%.

Detailed Instructions

  • Set up the board and Arduino IDE the same way as in the relay Quick Steps above.
  • Wire the fan, the MOSFET module and the 12V adapter as shown in the diagram.
  • Copy the code above, paste it into Arduino IDE and click Upload.
  • Plug in the 12V adapter.
  • Open the Serial Monitor at 9600 baud and listen to the fan.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' 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 MKR WiFi 1010 on COM15
2

Tip: The MOSFET module works with DC only, and its load supply must be about 6V or more. For an AC fan, stay with 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!