Arduino Nano - Fan

This tutorial instructs you how to use Arduino Nano to turn a fan on and off. Instructions on how to adjust the speed of the fan will be presented in a separate tutorial.

There are two ways to switch the fan from the Nano: a relay or a MOSFET module. A relay gives you only two states, ON and OFF. A MOSFET module also does ON and OFF, and on top of that it lets the Nano change how fast the fan spins. This page shows you both ways.

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B 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×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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 usually has two pins:

  • Negative (-) pin (black): needs to be connected to the negative wire of DC power supply
  • Positive (+) pin (red): needs to be connected to the positive wire of DC power supply

The voltage of the DC power supply should be equal to the voltage specified by the fan. In this tutorial, we will use 12VDC and 5VDC fans.

How to Control Fan

  • If DC fan is powered by 12V/5V power supply, it run with full speed.
  • If DC fan is powered by 12V/5V PWM signal, The fan's speed can be controlled.

In this tutorial, we will be teaching how to use Arduino Nano to turn a fan on and off. Controlling the fan's speed will be discussed in a different tutorial.

To control a fan on off, we need to utilize a relay between the Arduino Nano and the fan. The Arduino Nano can then use the relay to turn the fan on or off.

If you are unfamiliar with relay (pinout, functionality, programming, etc.), please refer to the Arduino Nano - Relay tutorial for more information.

New to the MOSFET module? The Arduino Nano - MOSFET Module tutorial explains its pins and how it works.

Relay vs MOSFET Module for a DC Fan

Relay MOSFET Module
What you can control fan ON or OFF fan ON or OFF, plus fan speed (PWM)
Noise when switching a click each time no sound
How fast it switches slow, a few milliseconds very fast
Wear the contacts wear out after many switches nothing moves, so nothing wears out
Works with AC or DC DC only
Fan supply voltage any, as rated about 6V or more
Signal isolation yes, coil and contacts are separate yes, through a PC817 optocoupler
Good choice for AC fans, simple ON/OFF 12V DC fans when you want speed control

For a 12V DC fan, the MOSFET module is the better pick, because the Nano can make the fan quiet at low speed and fast when it is needed. Keep the relay for an AC fan or for a 5V fan.

Wiring Diagram

The wiring diagram between Arduino Nano and Fan

This image is created using Fritzing. Click to enlarge image

Please note that if you use 5V fan, you need to use 5V power adapter. You MUST not use the Arduino's 5V pin to power fan because it cannot provide enough current for the fan.

Arduino Nano Code

The code below will cause the fan to turn on every five seconds and then off again after the same amount of time.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-fan */ #define RELAY_PIN 2 // The Arduino Nano pin that controls the fan via relay module // The setup function runs once on reset or power-up void setup() { // initialize digital pin D2 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely 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

  • Connect your Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE and select the appropriate board and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Check out the fan's state.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

How to Control Fan Speed with MOSFET Module

Here we replace the relay with a MOSFET module. Now the Nano can do more than ON and OFF: it can also set the fan speed with a PWM signal.

Wiring Diagram

The wiring diagram between Arduino Nano and cooling fan MOSFET module

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Nano and other components.

From To
Arduino Nano pin D9 MOSFET module PWM pin
Arduino Nano GND MOSFET module GND pin
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 wires. The stripe side (cathode) goes to fan +, and the other side goes to fan −.

Arduino Nano Code

This code turns the fan fully ON for 2 seconds and OFF for 2 seconds. Then it raises the power from 10% to 100%, so the fan speeds up step by step, and lowers it again, so the fan slows down.

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

Detailed Instructions

  • Prepare the Arduino IDE as in the relay Quick Steps above.
  • Wire the fan and the MOSFET module as shown in the diagram. Leave the 12V adapter unplugged for now.
  • Copy the code above, paste it into Arduino IDE and upload it to the Arduino Nano.
  • Plug in the 12V power adapter.
  • Open the Serial Monitor at 9600 baud and listen to the fan change its speed.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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 Nano on COM15
2

Tip: the MOSFET module works only with DC and needs a fan supply of about 6V or more. A 5V fan or an AC fan should stay on 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!