Skip to Content

How To Control Fan using Arduino – A Complete Guide

How To Control Fan using Arduino – A Complete Guide

In this article, I will show you how to control a fan with an Arduino UNO board. 

Fans are a common household item often used to provide a cooling breeze or circulate air in a room.

However, did you know that fans can also be controlled using a microcontroller like the Arduino UNO? 

By using a few simple components and some basic programming, you can create your fan and Arduino project that allows you to adjust the speed and direction of your fan with ease. 

In this article, we will discuss the necessary hardware and software components and provide step-by-step instructions for setting up and using your fan with an Arduino UNO. 

In the later sections, you will find the Arduino code and a collection of frequently asked questions about the fan projects with answers.

Whether you are a beginner starting to learn more about Arduino UNO and fan-driving projects or an experienced maker looking for a new project, this article has something for everyone. 

So let’s dive in and get started!

Components Needed To Build Arduino And Fan Project

Hardware Components

Software is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on

Basics of Controlling Fan Using Arduino UNO

In this section, we will learn about the types of fans, circuits available to drive fans using Arduino, and the applications for using Arduino and fans.

There are several types of fans available. The most common one is shown below.

It is used to cool down the modules. It will bring the hot air from the systems outside, hence the name exhaust fans.

Basics of Controlling Fan Using Arduino UNO

You can find these fans on desktops, signal generators, tabletop multimeters, and more. The second type of fan is a blower fan.

blower fan

The blower fans are used in 3D printer applications, HVAC applications, and more.

Irrespective of the type of fan, the drive circuit remains almost the same. 

Some fans provide you feedback on the speed. An extra wire will provide a PWM output or an analog output.

The output value is directly proportional to the speed of the fan. 

As the demand for size reduction is increasing (notebooks, lite notebooks, Ultralite notebooks, for example), the fans are also evolving.

You still have to use fans to push the hot air out and let the semiconductor components cool down. 

As the demand for small size and high-performance rise, the fans play a vital role in keeping the system cooler. 

The fan speed can be controlled. You can turn on the fan at full speed when needed.

You can control the speed depending on whether a laptop is connected to the mains or running on the battery. 

In Arduino and fan applications, you can sense the environment to control the fan speed.

If the temperature reduces, you can also slowly reduce the fan speed to reduce the power. 

You will find fans with two, three, and four wires.

Understanding the purpose of the wires helps you to build more valuable applications.

build more valuable applications

Other types of fans you come across depending on the control type present.

A few fans are simple ON/OFF fans. They are more affordable and easy to use with simple circuitry. 

You can also find linear DC-powered fans. The speed of the fan increases as you increase the DC voltage.

Other types of fans include PWM-controlled fans.

You can use the PWM pins of the Arduino to control the fans. 

Two Wire Fans

With two wire fans, you will have power and a ground wire. Arduino cannot sense the fan speed.

Arduino cannot know whether the fan is running or not. Hence, it is an open-loop system.

Two Wire Fans

You can do a low-frequency PWM on the DC pin to vary the fan speed.

If you want to know the fan status, you have to opt for either the three-wire fan or a four-wire fan. 

Three Wire Fans

The driving method remains the same for three and two wires fans. For three-wire fans, a third wire called speed sense is available. 

You can read the third wire to find out the fan speed and whether the fan is running or not.

Hence the three-wire fans are also called closed-loop fans.

Three Wire Fans

Arduino can sense the speed of the fan hence it can regulate the fan speed to create a fixed airflow in the system. 

Four Wire Fan

Four Wire Fan

In the case of four-wire fans, you will have a dedicated PWM speed control wire.

Arduino can drive the PWM signal to control the fan.

 The four wires are:

  1. Supply
  2. Ground
  3. PWM speed control wire
  4. The tachometer signal (speed indicator). 

Let’s see different driving options based on the fan types.

The above image represents a primary drive circuit. You can do PWM to turn ON and OFF the fan.

The fan speed will now depend on the duty cycle of the PWM. The higher the duty cycle the faster the fan speed. 

Applications of Fans

Fans have been used in several industries for several decades. Here are a few applications of the fans. 

  1. Dust collection
  2. Heat removal
  3. Fresh Air circulation
  4. Focused cool down in 3D printer applications
  5. HVAC systems that use fans to control the room temperature, etc

In the next section, we will build a simple fan drive circuit with Arduino UNO.

Step-By-Step Instructions To Connect The Fan To Arduino UNO

In this section, we will build a project using Arduino UNO and a DC fan module. Let’s get started.

Step-By-Step Instructions To Connect The Fan To Arduino UNO

We will power the two-wire fan using Arduino UNO.

How To Connect The DC Fan Module To The Arduino UNO?

Below is the step-by-step guide to complete the hardware connections needed to connect the Arduino and the Fan.  

Step 1: Connect the FET with the Fan

Connect the FET with the Fan

Connect the fan ground pin to the drain of the MOSFET

Step 2: Connect the GND Pin to the MOSFET

Connect the GND Pin to the MOSFET

You can connect any GND pins available on the Arduino UNO board.

Connect the DC fan’s source pin to the Arduino’s GND pin.

Always start with the ground connections.

Connecting the GND pins first avoids accidental damage to the boards.

Step 3: Connect the Power pin

Connect the Power pin

Connect the Arduino UNO’s pin 5 to the MOSFET gate.

Notice that Pin 5 of the UNO is a PWM compatible pin.

You can control the speed of a two-wire DC fan using PWM. 

Step 4: Connect the pull-down resistor

Connect the pull-down resistor

Choose a resistor value between 220 Ohms to 100 kOhms.

Connecting the resistor between the ground and the GATE helps keep the MOSFET in OFF condition when the Arduino is not connected or accidentally the gate connection between the Arduino UNO and the GATE pin of the MOSFET opens.

Step 5: Connect the safety flyback diode

Connect the safety flyback diode

Why diode?

Notice the diode connections. The anode of the diode goes to the negative terminal of the fan.

The cathode of the diode goes to the positive terminal of the fan. 

Connect the positive supply voltage to the DC fan’s positive cable (red).

Make sure the voltage rating matches the supply voltage.

Step 6: Verify the complete connections

Verify the complete connections

Congratulations. We can now proceed with the required Arduino program to run the fan.

Arduino Code Example For The Arduino And The Fan Project

In this section, we will walk through the example Arduino code to test the Fan circuit. 

You can connect a couple of push buttons to control the fan speed, or you can update the code to change the fan speed at a fixed interval of time.

Project 1: Control the fan speed connected to the Arduino pin 

The Arduino code is presented below, and you can use two buttons to control the fan speed.

#define Up 2                             // Up button on pin 2
#define Down 3                           // Down button on pin 3
int stateUp;                             // Variable for state of Up button
int stateDown;                           // Variable for state of Down button
void setup() {
 pinMode(Up, INPUT_PULLUP);        // Apply internal pull up for Up button
 pinMode(Down, INPUT_PULLUP);      // Apply internal pull up for Down button
  DDRB |= (1 << DDB1) | (1 << DDB2);                     
  // Set ports
  TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11); 
  // Fast PWM mode    
  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10);   
  // Fast PWM mode, no clock prescaling possible
  OCR1A = 3240;              // Start PWM just below MOSFET turn on
  ICR1 = 8191;              // Set the number of PWM steps
  Serial.begin(9600);         // Start communication with serial monitor
void loop() {
 stateUp = digitalRead(Up);                 // Read Up button state
 if(stateUp == LOW) {                       // If pressed...
   OCR1A++;                                   // Increase PWM setting
 stateDown = digitalRead(Down);             // Read Down button state
 if(stateDown == LOW) {                     // If pressed...
   OCR1A--;                                   // Decrease PWM setting
 Serial.println(OCR1A);       // Print current PWM setting on serial monitor

Project 2: A complete project on temperature sensor and fan-based room temperature regulator

A complete project on temperature sensor and fan-based room temperature regulator

The Arduino code is shared below

#include "LiquidCrystal.h"

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int tempPin = A0; // the output pin of LM35
int fan = 11; // the pin where fan is
int led = 8; // led pin
int temp;
int tempMin = 30; // the temperature to start the fan 0%
int tempMax = 60; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;

int readTemp() { // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16, 2);
void loop()
  temp = readTemp(); // get the temperature
  Serial.print( temp );
  if (temp < tempMin) // if temp is lower than minimum temp
    fanSpeed = 0; // fan is not spinning
    analogWrite(fan, fanSpeed);
    fanLCD = 0;
    digitalWrite(fan, LOW);
  if ((temp >= tempMin) && (temp <= tempMax)) // if temperature is higher than minimum temp
    fanSpeed = temp;
    fanSpeed = 1.5 * fanSpeed;
    fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD100
    analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
  if (temp > tempMax) // if temp is higher than tempMax
    digitalWrite(led, HIGH); // turn on led
  else // else turn of led
    digitalWrite(led, LOW);
  lcd.print("TEMP: ");
  lcd.print(temp); // display the temperature
  lcd.print("C ");
  lcd.setCursor(0, 1); // move cursor to next line
  lcd.print("FANS: ");
  lcd.print(fanLCD); // display the fan speed

FAQs About The Fan and the Arduino Projects

I have included a list of the most frequently asked questions about projects built using Arduino and Fan modules.

If you have further questions, kindly post them in the comments section.

I will be glad to answer them. 

1. Can Arduino run a fan?

Yes. Arduino can run a fan. If the fans are tiny, you can power the fans using the 5V pin of the UNO.

If the fans demand more current or higher voltage, you must use other driver boards or a BJT/MOSFET-based switch for driving the fans.

In this article, you have found one way of driving the fan using an Arduino UNO. 

2. Can Arduino power a 5 V fan?

Arduino can power a 5 V fan. I don’t recommend you connect a 5 V fan directly to an Arduino UNO pin.

The current drawn by the fan can be very high which may lead to the destruction of the Pin.

Please use a fan driver IC or a fan controller board to drive the fan.

Do not connect the fan directly to the UNO unless you are sure that the fan consumes less current ( < 10 mA). 

3. How do I automate Arduino fans?

Arduino boards help you in automating several tasks.

One of the tasks can be keeping the fan on as soon as the temperature rises above the preset settings.

You can use a temperature sensor with the Arduino UNO to detect the room temperature. 

If the temperature sensed doesn’t exceed the preset temperature, the Arduino will keep the fan in the off condition.

You will turn on the fan if the temperature read exceeds the preset temperature.

You can also modulate the fan speed using PWM capability.


In this article, we understood the basics of driving a fan with Arduino.

I showed you how to make all the connections and shared some example code that you can use as well.

Arduino allows makers and DIY enthusiasts to create custom fan control systems to suit their specific needs. 

I have built a fan project using ATTiny85. I used the fan as a cooler for my friends’ 3D printer project. 

As always, I welcome feedback from my readers.

Please let us know in the comments section below if you have any thoughts or suggestions on Arduino and fan control.

Your feedback is important to us, and we would love to hear from you.

Thank you for reading!

Don’t forget to share the articles with other Arduino enthusiasts too.

Mathieu Girard

Sunday 3rd of December 2023


I'm working on a project right now and I basically want my fan to be powered by the amount of light that is going through a LDR. At first, I thought about only swapping your LM35 in your picture for the project 2 for a LDR and change a little bit the code that you provide but I seem to go nowhere... My question is: how would you change the circuit and the code for it to work with a LDR instead of a LM35?

Mathieu Girard

Tuesday 5th of December 2023

Here is two picture of my circuit. It doesn't work very well and I don't seem to know why... The resistors are 1kohm and the capacitor is 1uF, I tried with different values and it doesn't help at all

Mathieu Girard

Tuesday 5th of December 2023

If I want to remove all of the diodes do I need to change the circuit?

Mathieu Girard

Tuesday 5th of December 2023

Alright I'll try that thanks!

Stefan Maetschke

Monday 4th of December 2023

Hi, You would have to replace the LM35 by an LDR in a voltage divider configuration (see here: The code can largely unchanged. Just replace readTemp() by readLight() and change the scaling factors and thresholds depending on your needs.