You are here: Home » News » DC Axial Fan Knowledge » Can You Use PWM To Control Fan Speed with Arduino?

Can You Use PWM To Control Fan Speed with Arduino?

Views: 222     Author: Jasmine     Publish Time: 2025-03-24      Origin: Site

Inquire

wechat sharing button
line sharing button
twitter sharing button
facebook sharing button
linkedin sharing button
pinterest sharing button
whatsapp sharing button
sharethis sharing button

Content Menu

Understanding PWM

>> PWM Basics

>> Why Use PWM?

Hardware Requirements

>> Wiring Diagram for 2-Wire/3-Wire Fan

>> Wiring Diagram for 4-Wire Fan

Software Setup: Arduino Code

>> Basic Code for PWM Control

>> Explanation:

>> Advanced Code with Temperature Sensing

>> Code Explanation:

>> High-Frequency PWM

>> Complete Code Example with High-Frequency PWM

Considerations for Different Fan Types

>> 2-Wire Fans

>> 3-Wire Fans

>> 4-Wire Fans

Practical Applications

Advanced Techniques

Troubleshooting

Conclusion

FAQ

>> 1. Can I use any Arduino board to control a fan with PWM?

>> 2. What is the difference between 2-wire, 3-wire, and 4-wire fans?

>> 3. What PWM frequency should I use for a 4-wire fan?

>> 4. Do I need a MOSFET to control a 4-wire fan?

>> 5. How can I read the speed of a 3-wire fan with Arduino?

Citations:

Pulse Width Modulation (PWM) is a highly effective technique for controlling the amount of power delivered to electrical devices, and it's especially useful for managing the speed of an arduino miniature fan. In this article, we'll delve into how to utilize PWM with an Arduino to gain precise control over a fan's speed. This level of control is invaluable in scenarios where temperature or other environmental conditions need to be carefully regulated, such as in computer cooling systems, climate control setups, and robotics applications. We will cover the fundamental principles of PWM, the necessary hardware setup, and the code implementation required to ensure everything operates smoothly.

arduino miniature fan2f

Understanding PWM

PWM is a method of regulating the average power supplied to an electrical device by modulating the duty cycle of a square wave. The duty cycle represents the proportion of time the signal remains HIGH compared to the time it spends LOW within a single period. For example, a 50% duty cycle indicates that the signal is HIGH for half of the time and LOW for the remaining half. By rapidly switching the signal between HIGH and LOW states, we can effectively manipulate the average voltage applied to the fan, thereby achieving control over its speed.

PWM Basics

- Frequency: The frequency of the PWM signal dictates how quickly the signal transitions between HIGH and LOW states. Higher frequencies allow for smoother control but might introduce more noise.

- Duty Cycle: The duty cycle, expressed as a percentage, quantifies the ratio of the time the signal is HIGH to the total period of the signal. A higher duty cycle means more power is delivered to the fan.

Why Use PWM?

1. Efficiency: PWM is highly efficient because the control device (e.g., a transistor) operates either fully ON or fully OFF, minimizing power loss and heat generation. This makes it ideal for battery-powered applications and systems where energy conservation is critical.

2. Precision: PWM offers exceptional precision in controlling the speed of the fan by allowing fine adjustments to the duty cycle. This is particularly important when maintaining a specific temperature or airflow is essential.

3. Versatility: PWM can be applied to various types of fans, including 2-wire, 3-wire, and 4-wire models. This adaptability makes it a universal solution for fan speed control.

4. Software Controllability: By using Arduino, you can dynamically adjust fan speed based on sensor readings or other inputs, enabling intelligent and responsive cooling systems.

Hardware Requirements

To control an arduino miniature fan using PWM, you will need the following hardware components:

1. Arduino Board: Any Arduino board, such as the Arduino Uno, Nano, or Mega, is suitable for this project. Choose a board with enough digital pins for your specific application.

2. arduino miniature fan: Select a fan that matches the voltage compatibility of your Arduino board (typically 5V or 12V). Consider the airflow and noise levels of the fan to meet your project's requirements.

3. N-Channel MOSFET (for 2-wire and 3-wire fans): Employ a MOSFET, such as the IRF520 or similar, to regulate the power supplied to the fan. The MOSFET acts as a switch controlled by the Arduino's PWM signal.

4. Resistor (10 kΩ): Utilize this resistor as a pull-up resistor for 3-wire fans to facilitate the reading of fan speed. It ensures a stable signal when the fan's speed sensor output is high.

5. Power Supply: Employ a power supply that meets the voltage specifications of your fan. Ensure the power supply has sufficient current capacity to power both the Arduino and the fan.

6. Connecting Wires: Employ these wires to establish connections among all components. Choose wires of appropriate gauge to handle the current requirements.

7. Breadboard (Optional): A breadboard simplifies prototyping and allows for easy rearrangement of components.

Wiring Diagram for 2-Wire/3-Wire Fan

- Connect the positive (+) wire of the power supply to the drain of the MOSFET.

- Connect the source of the MOSFET to the positive (+) wire of the arduino miniature fan.

- Connect the negative (-) wire of the power supply to the negative (-) wire of the arduino miniature fan and the ground of the Arduino.

- Connect a digital pin on the Arduino (e.g., Pin 9, which supports PWM) to the gate of the MOSFET. This pin will provide the PWM signal to control the MOSFET.

- For 3-wire fans, connect the third wire (usually yellow or blue) to a digital pin on the Arduino through a 10 kΩ pull-up resistor to read the fan speed. This allows the Arduino to monitor the fan's RPM.

Wiring Diagram for 4-Wire Fan

- Connect the positive (+) and negative (-) wires of the fan to the power supply. Ensure correct polarity.

- Connect the PWM control wire (usually blue) of the fan directly to a PWM-enabled pin on the Arduino. This wire receives the PWM signal from the Arduino.

- Connect the speed sensor wire (usually yellow) to a digital pin on the Arduino through a 10 kΩ pull-up resistor. This enables the Arduino to read the fan's speed.

- Ensure that the ground of the Arduino is connected to the ground of the power supply. This is crucial for signal integrity and proper operation.

arduino miniature fan1f

Software Setup: Arduino Code

The Arduino code for controlling an arduino miniature fan is straightforward. It involves configuring the PWM pin, reading sensor data (if applicable), and adjusting the fan speed based on the input.

Basic Code for PWM Control

// Define the PWM pin

const int fanPin = 9;

void setup() {

 // Set the fan pin as an output

 pinMode(fanPin, OUTPUT);

}

void loop() {

 // Set the fan speed (0-255)

 int fanSpeed = 150; // Example speed

 analogWrite(fanPin, fanSpeed);

 delay(100); // Delay for stability

}

Explanation:

- const int fanPin = 9: Defines the digital pin that is connected to the gate of the MOSFET (or the PWM control wire of a 4-wire fan). Choose a PWM-enabled pin on your Arduino board.

- pinMode(fanPin, OUTPUT): Sets the defined pin as an output, allowing the Arduino to send a PWM signal.

- analogWrite(fanPin, fanSpeed): Sends a PWM signal to the specified pin. The fanSpeed variable ranges from 0 (off) to 255 (full speed), corresponding to the duty cycle of the PWM signal.

- delay(100): A small delay to ensure the fan speed stabilizes and prevents rapid changes.

Advanced Code with Temperature Sensing

For more sophisticated control, you can integrate a temperature sensor to dynamically adjust the fan speed based on temperature readings. This allows for automated and responsive cooling.

// Define the pins

const int fanPin = 9;

const int tempPin = A0; // Analog pin for temperature sensor

// Define variables

int temperature;

int fanSpeed;

void setup() {

 // Initialize serial communication

 Serial.begin(9600);

 // Set the fan pin as an output

 pinMode(fanPin, OUTPUT);

}

void loop() {

 // Read the temperature sensor value

 temperature = analogRead(tempPin);

 // Convert the analog value to temperature (example formula)

 float voltage = temperature * (5.0 / 1023.0);

 float temperatureC = (voltage - 0.5) * 100; // Example: LM35 sensor

 // Control fan speed based on temperature

 if (temperatureC < 25) {

   fanSpeed = 0; // Fan off

 } else if (temperatureC < 30) {

   fanSpeed = 100; // Low speed

 } else if (temperatureC < 35) {

   fanSpeed = 180; // Medium speed

 } else {

   fanSpeed = 255; // Full speed

 }

 // Write the fan speed

 analogWrite(fanPin, fanSpeed);

 // Print the temperature and fan speed to the serial monitor

 Serial.print("Temperature: ");

 Serial.print(temperatureC);

 Serial.print(" °C, Fan Speed: ");

 Serial.println(fanSpeed);

 delay(500); // Delay for stability

}

Code Explanation:

- const int tempPin = A0: Defines the analog pin connected to the temperature sensor. Use an appropriate analog pin on your Arduino.

- temperature = analogRead(tempPin): Reads the analog value from the temperature sensor. This value represents the voltage output of the sensor.

- The code then converts the analog reading to a temperature value. The exact formula depends on the type of temperature sensor you are using (e.g., LM35, DHT11). Refer to the sensor's datasheet for the correct conversion formula.

- Based on the temperature, the fanSpeed variable is set to different values, controlling the fan speed. This allows for dynamic adjustment of the fan based on the measured temperature.

- The temperature and fan speed are printed to the serial monitor for debugging and monitoring. This helps in verifying the code's functionality and troubleshooting any issues.

High-Frequency PWM

Some arduino miniature fan models, particularly those used in computer CPUs, require a higher PWM frequency (around 25 kHz) to operate correctly. The default PWM frequency on many Arduino boards is around 490 Hz or 980 Hz, which may not be suitable for these fans. This can cause the fan to operate erratically or not at all.

To increase the PWM frequency, you need to modify the Arduino's timer settings. Here's an example of how to do this on an Arduino Uno:

void setup() {

 // Set PWM frequency to approximately 31 kHz

 TCCR1B = (TCCR1B & 0b11111000) | 0x01;

}

void loop() {

 // Your fan control code here

}

This code modifies the Timer1 control register to increase the PWM frequency on pins 9 and 10. Be aware that this will affect the PWM frequency on both pins. Modifying timer registers can have unintended consequences, so proceed with caution and thoroughly test your code.

Complete Code Example with High-Frequency PWM

// Define the PWM pin

const int fanPin = 9;

void setup() {

 // Set PWM frequency to approximately 31 kHz

 TCCR1B = (TCCR1B & 0b11111000) | 0x01;

 // Set the fan pin as an output

 pinMode(fanPin, OUTPUT);

}

void loop() {

 // Set the fan speed (0-255)

 int fanSpeed = 150; // Example speed

 analogWrite(fanPin, fanSpeed);

 delay(100); // Delay for stability

}

Considerations for Different Fan Types

2-Wire Fans

2-wire fans are the simplest type, with only power and ground connections. Speed control is achieved by varying the voltage using a MOSFET and PWM signal. The MOSFET acts as a switch, allowing the Arduino to control the flow of power to the fan.

3-Wire Fans

3-wire fans include an additional wire that provides feedback on the fan speed. This wire outputs two pulses per revolution, which can be measured using an interrupt on the Arduino. This feedback enables the Arduino to monitor and adjust the fan speed in real-time.

4-Wire Fans

4-wire fans have a dedicated PWM control input, allowing direct control of the fan speed without the need for an external MOSFET. These fans often require a higher PWM frequency, typically around 25 kHz, to operate correctly.

Practical Applications

1. Computer Cooling: Dynamically adjust fan speeds based on CPU or GPU temperature to minimize noise and power consumption. This can significantly extend the lifespan of electronic components.

2. HVAC Systems: Control ventilation fans to maintain optimal temperature and air quality in buildings. This can lead to significant energy savings and improved comfort.

3. Robotics: Regulate cooling fans in robotic systems to prevent overheating of motors and electronic components. This ensures reliable operation and prevents damage.

4. DIY Projects: Implement custom cooling solutions for any project that requires temperature regulation, such as 3D printers, amplifiers, and power supplies.

5. Greenhouses: Controlling ventilation fans in greenhouses to regulate temperature and humidity levels, optimizing plant growth and reducing energy consumption.

Advanced Techniques

1. PID Control: Implement a Proportional-Integral-Derivative (PID) controller to precisely regulate the fan speed based on temperature feedback. This allows for smooth and stable temperature control.

2. Fan Curve Implementation: Create a custom fan curve that maps temperature ranges to specific fan speeds. This enables customized cooling profiles tailored to specific applications.

3. Software Filtering: Apply software filters to temperature readings to reduce noise and improve the stability of the fan control system. This can prevent erratic fan behavior.

4. Data Logging: Log temperature and fan speed data to a file or database for analysis and optimization of the cooling system. This provides valuable insights into system performance.

Troubleshooting

1. Fan Not Spinning:

- Check all wiring connections to ensure they are secure and correct.

- Ensure the power supply is providing the correct voltage and current.

- Verify that the MOSFET is functioning correctly (if using a 2-wire or 3-wire fan).

- Make sure the fanSpeed value is not set to 0.

2. Fan Spinning at Full Speed Regardless of PWM Signal:

- For 4-wire fans, ensure the PWM frequency is within the required range (typically 21 kHz to 28 kHz).

- Check the wiring of the PWM control wire to ensure it is properly connected.

3. Inconsistent Fan Speed Readings:

- Ensure the pull-up resistor is correctly connected for 3-wire and 4-wire fans.

- Verify the interrupt code is correctly implemented and that the interrupt pin is properly configured.

4. Fan Makes a Whining Noise:

- This can occur if the PWM frequency is too low. Try increasing the PWM frequency to eliminate the noise.

5. Temperature Readings are Inaccurate:

- Ensure the temperature sensor is properly calibrated and that the correct conversion formula is used in the code.

- Check for any sources of interference that may be affecting the temperature readings.

Conclusion

Using PWM to control an arduino miniature fan with an Arduino is a versatile and efficient method for managing temperature and airflow in various applications. Whether you are building a custom computer cooling system, regulating ventilation in a smart home, or optimizing plant growth in a greenhouse, the ability to precisely control fan speed can significantly improve performance and energy efficiency. By understanding the basics of PWM, the different types of fans, and the code required, you can implement sophisticated cooling solutions tailored to your specific needs. The combination of Arduino's flexibility and PWM's efficiency makes this a powerful technique for any project requiring thermal management.

add fan to miniature refrigerator3

FAQ

1. Can I use any Arduino board to control a fan with PWM?

Yes, you can use virtually any Arduino board to control a fan using PWM. However, ensure that the board has PWM-enabled pins. Common boards such as the Arduino Uno, Nano, and Mega all support PWM functionality. Check the board's documentation to identify the PWM-enabled pins.

2. What is the difference between 2-wire, 3-wire, and 4-wire fans?

2-wire fans are the most basic type, having only power and ground connections. Speed control necessitates an external MOSFET and PWM signal. 3-wire fans include a speed sensor wire that allows for monitoring the fan speed. 4-wire fans have a dedicated PWM control input, enabling direct speed control from the Arduino, often requiring a higher PWM frequency.

3. What PWM frequency should I use for a 4-wire fan?

4-wire fans typically require a higher PWM frequency to function correctly, often around 25 kHz. However, it is crucial to consult the fan's datasheet for the specific recommended frequency range to ensure optimal performance and prevent damage.

4. Do I need a MOSFET to control a 4-wire fan?

No, a MOSFET is not necessary for controlling a 4-wire fan. These fans feature a dedicated PWM control input, which allows you to directly connect the PWM signal from the Arduino to the fan without the need for an external MOSFET.

5. How can I read the speed of a 3-wire fan with Arduino?

To read the speed of a 3-wire fan with Arduino, connect the speed sensor wire (typically yellow or blue) to a digital pin on the Arduino through a 10 kΩ pull-up resistor. Then, use an interrupt to count the pulses generated by the fan, which typically outputs two pulses per revolution. This allows you to calculate the fan's RPM accurately.

Citations:

[1] https://www.instructables.com/How-to-Control-AC-Fan-Speed-Using-Arduino-Generate/

[2] https://www.youtube.com/watch?v=UJK2JF8wOu8

[3] https://www.youtube.com/watch?v=YBNEXWp-gf0

[4] https://www.instructables.com/Temperature-Control-With-Arduino-and-PWM-Fans/

[5] https://forum.arduino.cc/t/controlling-fan-speed-via-pwm/1115661

[6] https://projecthub.arduino.cc/MyName1sSimon/control-pwm-fans-with-an-arduino-de9895

[7] https://fdossena.com/?p=ArduinoFanControl%2Fi.md

[8] https://projecthub.arduino.cc/virtualblack/control-the-speed-of-a-pc-fan-by-pwm-f221c0

Content Menu
Author: Doris Wan
Xingdong has more than 20 experienced R&D engineers. Our range of products, including DC axial fans, DC blowers, cross flow fans, AC axial fans, and EC axial fans, meets rigorous industry standards and certifications. Having a professional service team and senior technical personnel in the industry, with 7 * 24-hour online service, timely response.
In line with our business philosophy of sincerity, pursuit of excellence, professional service, and win-win cooperation, we foster an environment where collaboration thrives. Our talent philosophy emphasizes pragmatism, enterprising spirit, rigor, and teamwork, ensuring that we attract and retain the best professionals in the industry. Join us on this journey as we continue to set new benchmarks in the cooling solutions market. Together, we can achieve remarkable results and create lasting partnerships that benefit everyone involved. Let us be your trusted partner in navigating the challenges of today’s dynamic environment.
Welcome to Contact Us
Get Touch With Us
Our cooling fans are widely used in home appliances, automobiles, security, communications, medical, industrial control, electric power, printing, military, welding machines, instruments, computers, power supplies and other fields. Our company has an independent industrial park with nearly 300 employees.

CONTACT US

Phone:+86-13528507673
Email: info02@d-fan.com.cn
Add:No. 2, Hengfeng 2nd Road, DaWo, Changping Town, Dongguan City, Guangdong Province, China

QUICK LINKS

PRODUCTS CATEGORY

Get Touch With Us
Copyright © Dongguan Xingdong Electronics Co., Ltd.All Rights Reserved.