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
Or you can buy the following kits:
| 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 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

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

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