How to drive a DC motor fan with Arduino?

 This guide covers how to control a DC motor fan (like a cooling fan) using an Arduino, including speed control with PWM, direction control with an H-bridge, and protection circuits.




1. Components Needed

ComponentPurpose
Arduino (Uno/Nano)Motor control logic
DC Motor Fan (5V/12V)Example: 12V PC fan
Transistor (TIP120, 2N2222)For small motors (≤500mA)
MOSFET (IRF540N, IRLB8721)For high-current motors (≥1A)
Diode (1N4007)Flyback protection
Resistor (220Ω-1kΩ)Limits base/gate current
Power SupplyMust match motor voltage (e.g., 9V battery, 12V adapter)


2. Basic On/Off Control (No Speed Control)

Circuit Diagram


Arduino 5V → Motor (+)  
Arduino GND → Motor (-)  

⚠️ Warning: Only works for very small motors (≤100mA). For larger motors, use a transistor/MOSFET.



3. Using a Transistor (TIP120) for Medium Loads

Wiring (NPN Transistor)


Arduino Pin 9 → Resistor (1kΩ) → TIP120 Base  
TIP120 Collector → Motor (+)  
TIP120 Emitter → GND  
Motor (-) → Power Supply (+)  
Power Supply (-) → GND  
Place 1N4007 diode across motor (reverse-biased).  


Arduino Code (On/Off)

cpp

void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  digitalWrite(9, HIGH);  // Motor ON
  delay(2000);
  digitalWrite(9, LOW);   // Motor OFF
  delay(2000);
}


4. Speed Control with PWM (Pulse Width Modulation)

Wiring (Same as Above, but PWM Pin)

Use Pin 3, 5, 6, 9, 10, or 11 (PWM-capable pins on Arduino Uno).

Arduino Code (Variable Speed)

cpp

void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  // Ramp up speed (0-255)
  for (int speed = 0; speed <= 255; speed++) {
    analogWrite(9, speed);
    delay(20);
  }
  // Ramp down speed
  for (int speed = 255; speed >= 0; speed--) {
    analogWrite(9, speed);
    delay(20);
  }
}


5. Using a MOSFET (IRF540N) for High-Current Motors

Wiring (N-Channel MOSFET)


Arduino Pin 9 → Resistor (220Ω) → IRF540N Gate  
IRF540N Drain → Motor (+)  
IRF540N Source → GND  
Motor (-) → Power Supply (+)  
Power Supply (-) → GND  
Add 1N4007 diode across motor.  

Arduino Code (Same as PWM Example)

MOSFETs handle higher currents (up to 20A with heatsink).



6. Direction Control (H-Bridge: L298N)

If your fan can reverse direction (uncommon for PC fans), use an H-bridge like the L298N.

Wiring (L298N)


Arduino IN1 → Pin 8  
Arduino IN2 → Pin 9  
L298N OUT1 → Motor (+)  
L298N OUT2 → Motor (-)  
Power Supply → L298N 12V Input  
GND → Common ground  


Arduino Code (Forward/Stop/Reverse)

cpp

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  // Forward
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  delay(2000);
  
  // Stop
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  delay(1000);
  
  // Reverse (if motor supports it)
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  delay(2000);
}


7. Adding Temperature-Based Fan Control

Circuit Additions

  • Temperature Sensor (DS18B20/DHT11) → Arduino analog pin.

  • PWM Fan Control → Adjust speed based on temperature.


Arduino Code (Auto Speed Control)

cpp

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2  // DS18B20 on Pin 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int fanPin = 9;

void setup() {
  pinMode(fanPin, OUTPUT);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);
  
  // Map temperature (e.g., 25°C-50°C → 0-255 PWM)
  int fanSpeed = map(tempC, 25, 50, 0, 255);
  fanSpeed = constrain(fanSpeed, 0, 255);
  
  analogWrite(fanPin, fanSpeed);
  delay(1000);
}


8. Safety & Protection

  1. Flyback Diode (1N4007) – Prevents voltage spikes from damaging Arduino.

  2. Separate Power Supply – Don’t power motors from Arduino’s 5V pin.

  3. Fuse (Optional) – Add a 1A fuse for overcurrent protection.



9. Common Issues & Fixes

ProblemSolution
Motor doesn’t spinCheck transistor/MOSFET wiring
Motor runs at full speed onlyVerify PWM code & pin
Arduino resets when motor startsUse a separate power supply
MOSFET gets hotAdd a heatsink or reduce load


Conclusion

  • Small motors (≤500mA): Use a transistor (TIP120).

  • Larger motors (≥1A): Use a MOSFET (IRF540N).

  • Speed control: Use PWM (analogWrite()).

  • Direction control (rare for fans): Use an H-bridge (L298N).

  • Automation: Add a temperature sensor for smart cooling.

评论

此博客中的热门博文

What is a DSP? (Digital Signal Processor)

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?