PWM control of 5 fans with the mega2560

Hi all,

i’d like to control the speed of 5 fans ( 2-3Pin fans and 3-4Pin fans) with a arduino mega2560. Furthermore I am using a DFRobot LCD Keypad Shield.
I know there are a bunch of topics to this issue, but still my fans won’t work properly.

Until now my plan was to set the PWM frequency of timer1 and timer3 to 25 kHz because of the Intel-specification for fans.
That’s my example code so far…

void setup() {
  // reset Timer 1
  TCCR1A = 0; // set TCCR1A register to 0
  TCCR1B = 0; // set TCCR1B register to 0

  // reset Timer 3
  TCCR3A = 0;
  TCCR3B = 0;

  // set Prescaler for timer 1
  TCCR1B |= (1 << CS10);

  // set Prescaler for timer 3
  TCCR3B |= (1 << CS30);

  // set Mode for timer 1
  TCCR1A |= (1 << WGM11);
  TCCR1B |= (1 << WGM13) | (1 << WGM12);

  // set Mode for timer 3
  TCCR3A |= (1 << WGM31);
  TCCR3B |= (1 << WGM33) | (1 << WGM32);

  // Compare Match Register for timer 1;
  ICR1 = 639;
  OCR1A = 0; // Pin 11 Duty-Cycle
  OCR1B = 0; // Pin 12 Duty-Cycle

  // Compare Match Register for timer 3;
  ICR3 = 639; 
  OCR3A = 0; // Pin 2 Duty-Cycle
  OCR3B = 0; // Pin 3 Duty-Cycle

  pinMode(2,OUTPUT); // enable PWM output
  pinMode(3,OUTPUT); // enable PWM output
  pinMode(11,OUTPUT); // enable PWM output
  pinMode(12,OUTPUT); // enable PWm output


void loop() {
  // put your main code here, to run repeatedly:
  delay(1000); // every second
  OCR1A = 100;
  OCR1A = 200;
  OCR1A = 300;
  OCR1A = 400;
  OCR1A = 500;
  OCR1A = 639;


First I set the prescaler of timer1 and timer3 to 1(no prescaler). Then I set the mode of the two timers to fast PWM mode (WGMn1,WGMn2 and WGMn3 to 1; WGMn0 remains zero because I reseted the registers before). After that I set the TOP value for the two timers with ICRn to get a 25 kHz PWM frequency(639). Now I want to manipulate the duty-cycle for each fan.
Timer1 is for Pin 11 and Pin 12, timer3 is for Pin 2 and Pin 3 and Pin 5, so I can set the duty-cycle of Pin 11 by changing OCR1A from 0-639, Pin 12 by changing OCR1B, Pin 2 by changing OCR3A and Pin 3 by changing OCR3B (Pin 5 is unused). Is that correct? As far as I understood this should be possible, only with 8-bit timers (0,2) you get problems changing the duty-cycle of both pins of a single timer. That’s the reason why I use ICRn as TOP value. But the code above won’t change my fan frequency.
If I don’t change anything (no timer frequncy manipulation) and just use analogWrite(PIN,0-255) I am able to change the speed of the fan. Maybe I have a false understanding of the registers? The two 3Pin fans will use a transistor of course, the 4Pin fans should be controled directly by the arduino PWM Pins.
Right now I am struggling to get the 4 Pin fans to work.

Any help is appreciated.


PS: The duty cycles of the two 3Pin fans will be the same, so I just need four pins for five fans.

What fans are you talking about? Sensored brushless PC fans?

What fans are you talking about? Sensored brushless PC fans?

I am using simple 4 Pin PC fans (and 3 Pin too). I think I am in the wrong subforum with this issue. I overread "big heavy things" :slight_smile:

However, I found a thread with an example code, which seems to work. But I'd really like to know why my code isn't working or what I forgot to change in the registers.

Here is the thread:

Don't ask me! I dont even know what a register is :slight_smile: