Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
tylerpeppy
Published © GPL3+

25 kHz 4 Pin PWM Fan Control with Arduino Uno

Control the speed of your PWM fan easily. No need for other circuits.

IntermediateFull instructions provided107,028
25 kHz 4 Pin PWM Fan Control with Arduino Uno

Things used in this project

Hardware components

Axial Fan, 12 VDC
Axial Fan, 12 VDC
4-Wire Variant with CONTROL wire; tachometer only won't work
×1
Arduino UNO
Arduino UNO
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

pwm_fan_ctrl_6xm3Jti8le.png

Code

The Code

Arduino
Download this file and run it under the Arduino IDE.
const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;

const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency (Frequency in HZ!) (Set currently to 25kHZ)
const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ);

void setup() {
  
  pinMode(OC1A_PIN, OUTPUT);

  // Clear Timer1 control and count registers
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

  // Set Timer1 configuration
  // COM1A(1:0) = 0b10   (Output A clear rising/set falling)
  // COM1B(1:0) = 0b00   (Output B normal operation)
  // WGM(13:10) = 0b1010 (Phase correct PWM)
  // ICNC1      = 0b0    (Input capture noise canceler disabled)
  // ICES1      = 0b0    (Input capture edge select disabled)
  // CS(12:10)  = 0b001  (Input clock select = clock/1)
  
  TCCR1A |= (1 << COM1A1) | (1 << WGM11);
  TCCR1B |= (1 << WGM13) | (1 << CS10);
  ICR1 = TCNT1_TOP;
}

void loop() {

    setPwmDuty(0);
    delay(5000);
    setPwmDuty(25); //Change this value 0-100 to adjust duty cycle
    delay(5000);
//    setPwmDuty(50);
//    delay(20000);
//    setPwmDuty(75);
//    delay(20000);
//    setPwmDuty(100);
//    delay(20000);
}

void setPwmDuty(byte duty) {
  OCR1A = (word) (duty*TCNT1_TOP)/100;
}

Credits

tylerpeppy
2 projects • 42 followers
Love Arduino and IoT projects!

Comments