How to control a PWM-fan with Mega 2560 variable speed?

I can only find code for the UNO which doesn't work on my Mega 2560.

I try to run this fan nmb-mat bg0903-b049-p0s by PWM but can't get it right.

https://www.amazon.com/Genuine-Dell-BG0903-B049-P0S-Dimension-Optiplex/dp/B002MPVM6C

The fan is connected to +12V and ground and runs full speed. When i connect the blue (pwm) wire of the fan to ground it slows down the speed, after disconnecting blue wire from ground the speed comes up slowly untill full again.

I guess i have to use 25khz pwm but just can't get it right.

Can anybody please help me with code to change the pwm to 25khz so i can change the speed of it?

It has its own pwm line? And the specs say it has to be 25 kHz? And 12 V?

https://playground.arduino.cc/Code/PwmFrequency

Johan_Ha:
It has its own pwm line? And the specs say it has to be 25 kHz? And 12 V?

It has 4 wires, red black white blue....is used for servers....the company didn't put the datasheet for this model online though...

But i read a lot about controlling fan with arduino and mostly they need 25khz so i have to change that on my Mega2560.

I'm not 100% though that this is a pwm-fan...

    • Pins 9 and 10 are paired on timer1

This website says this: timer 2 for pin 10 and 9
https://forum.arduino.cc/index.php?topic=72092.0

Note that timer 0 is the one on which rely all time functions in Arduino: i.e., if you change this timer, function like delay() or millis() will continue to work but at a different timescale (quicker or slower!!!)

It's so confusing and i don't know who i can believe anymore as a noob.

Maybe this could help you ... OP.

http://www.formfactors.org/developer/specs/4_wire_pwm_spec.pdf

Also, might need to configure "internal" pull-up resistor in the arduino if you're not going to use an external pull-up resistor.

I don't know if your fan requires 25 kHz PWM. Maybe it does. And if it does, then your focus will be on generating 25 kHz PWM with the arduino.

I'm sure you'll be able to drive your fan. Just a matter of time. Pretty sure that somebody here will have some short code to provide 25 kHz PWM for the 2560.

Well the question has been asked before and this answer worked for this person, when i try to flash it i get the error:

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_nov20b:3: error: stray '\302' in program

  OCR4C = value;

^

sketch_nov20b:3: error: stray '\240' in program

sketch_nov20b:3: error: stray '\302' in program

sketch_nov20b:3: error: stray '\240' in program

sketch_nov20b:7: error: stray '\302' in program

{ Â Â

^

sketch_nov20b:7: error: stray '\240' in program

sketch_nov20b:7: error: stray '\302' in program

sketch_nov20b:7: error: stray '\240' in program

sketch_nov20b:8: error: stray '\302' in program

  TCCR4A = 0;

^

sketch_nov20b:8: error: stray '\240' in program

sketch_nov20b:8: error: stray '\302' in program

sketch_nov20b:8: error: stray '\240' in program

sketch_nov20b:9: error: stray '\302' in program

  TCCR4B = 0;

^

sketch_nov20b:9: error: stray '\240' in program

sketch_nov20b:9: error: stray '\302' in program

sketch_nov20b:9: error: stray '\240' in program

sketch_nov20b:10: error: stray '\302' in program

  TCNT4  = 0;

^

sketch_nov20b:10: error: stray '\240' in program

sketch_nov20b:10: error: stray '\302' in program

sketch_nov20b:10: error: stray '\240' in program

sketch_nov20b:10: error: stray '\302' in program

sketch_nov20b:10: error: stray '\240' in program

sketch_nov20b:12: error: stray '\302' in program

  // Mode 10: phase correct PWM with ICR4 as Top (= F_CPU/2/25000)

^

sketch_nov20b:12: error: stray '\240' in program

sketch_nov20b:12: error: stray '\302' in program

sketch_nov20b:12: error: stray '\240' in program

sketch_nov20b:13: error: stray '\302' in program

  // OC4C as Non-Inverted PWM output

^

sketch_nov20b:13: error: stray '\240' in program

sketch_nov20b:13: error: stray '\302' in program

sketch_nov20b:13: error: stray '\240' in program

sketch_nov20b:14: error: stray '\302' in program

  ICR4  = (F_CPU/25000)/2;

^

sketch_nov20b:14: error: stray '\240' in program

sketch_nov20b:14: error: stray '\302' in program

sketch_nov20b:14: error: stray '\240' in program

sketch_nov20b:14: error: stray '\302' in program

sketch_nov20b:14: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

  OCR4C  = ICR4/2;           // default: about 50:50

^

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:15: error: stray '\302' in program

sketch_nov20b:15: error: stray '\240' in program

sketch_nov20b:16: error: stray '\302' in program

  TCCR4A = _BV(COM4C1) | _BV(WGM41);

^

sketch_nov20b:16: error: stray '\240' in program

sketch_nov20b:16: error: stray '\302' in program

sketch_nov20b:16: error: stray '\240' in program

sketch_nov20b:17: error: stray '\302' in program

  TCCR4B = _BV(WGM43) | _BV(CS40);

^

sketch_nov20b:17: error: stray '\240' in program

sketch_nov20b:17: error: stray '\302' in program

sketch_nov20b:17: error: stray '\240' in program

sketch_nov20b:19: error: stray '\302' in program

  Serial.begin(115200);

^

sketch_nov20b:19: error: stray '\240' in program

sketch_nov20b:19: error: stray '\302' in program

sketch_nov20b:19: error: stray '\240' in program

sketch_nov20b:21: error: stray '\302' in program

  // Set the PWM pin as output.

^

sketch_nov20b:21: error: stray '\240' in program

sketch_nov20b:21: error: stray '\302' in program

sketch_nov20b:21: error: stray '\240' in program

sketch_nov20b:22: error: stray '\302' in program

  pinMode( 8, OUTPUT);

^

sketch_nov20b:22: error: stray '\240' in program

sketch_nov20b:22: error: stray '\302' in program

sketch_nov20b:22: error: stray '\240' in program

sketch_nov20b:27: error: stray '\302' in program

  int w = Serial.parseInt();

^

sketch_nov20b:27: error: stray '\240' in program

sketch_nov20b:27: error: stray '\302' in program

sketch_nov20b:27: error: stray '\240' in program

sketch_nov20b:28: error: stray '\302' in program

  if (w>0) {

^

sketch_nov20b:28: error: stray '\240' in program

sketch_nov20b:28: error: stray '\302' in program

sketch_nov20b:28: error: stray '\240' in program

sketch_nov20b:29: error: stray '\302' in program

    analogWrite25k(w);

^

sketch_nov20b:29: error: stray '\240' in program

sketch_nov20b:29: error: stray '\302' in program

sketch_nov20b:29: error: stray '\240' in program

sketch_nov20b:29: error: stray '\302' in program

sketch_nov20b:29: error: stray '\240' in program

sketch_nov20b:29: error: stray '\302' in program

sketch_nov20b:29: error: stray '\240' in program

sketch_nov20b:30: error: stray '\302' in program

    Serial.println(w);

^

sketch_nov20b:30: error: stray '\240' in program

sketch_nov20b:30: error: stray '\302' in program

sketch_nov20b:30: error: stray '\240' in program

sketch_nov20b:30: error: stray '\302' in program

sketch_nov20b:30: error: stray '\240' in program

sketch_nov20b:30: error: stray '\302' in program

sketch_nov20b:30: error: stray '\240' in program

sketch_nov20b:31: error: stray '\302' in program

  }

^

sketch_nov20b:31: error: stray '\240' in program

sketch_nov20b:31: error: stray '\302' in program

sketch_nov20b:31: error: stray '\240' in program

exit status 1
stray '\302' in program

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Found a properly working answer for the question finally; Thanks to KIV's answer on Need help to set PWM frequency to 25kHz on pin 8 of Arduino Mega to control speed of a 4-wire cpu fan an additional usful thing he has added is to allow setting of duty cycle from serial monitor input for convenience while testing;

void analogWrite25k(int value)
{
OCR4C = value;
}

void setup()
{
TCCR4A = 0;
TCCR4B = 0;
TCNT4 = 0;

// Mode 10: phase correct PWM with ICR4 as Top (= F_CPU/2/25000)
// OC4C as Non-Inverted PWM output
ICR4 = (F_CPU/25000)/2;
OCR4C = ICR4/2; // default: about 50:50
TCCR4A = _BV(COM4C1) | _BV(WGM41);
TCCR4B = _BV(WGM43) | _BV(CS40);

Serial.begin(115200);

// Set the PWM pin as output.
pinMode( 8, OUTPUT);
}

void loop()
{
int w = Serial.parseInt();
if (w>0) {
analogWrite25k(w);
Serial.println(w);
}
}

Well it works now guys...the above code works but my windows10 copied the code wrongly so i got that error....

I copied the same code again and now it works, the fan is great.

Thanks for your help!

This is for other users who want to use a Mega2560 and pwm fan.

Use the code below and use pin 8 on the mega, connect that to the pwm-wire of the fan. (fan also needs separate plus and minus).

In the serial monitor you can type a number 0-300 which will change the speed.

I don't dare to try 300 anymore cause my fan goes very fast....it's a 2.6A 12V fan i'm using, very powerfull but exactly what i needed....it can also be whispering quiet and still running.

Thanks to user: Technice i found this.


void analogWrite25k(int value)
{
OCR4C = value;
}

void setup()
{
TCCR4A = 0;
TCCR4B = 0;
TCNT4 = 0;

// Mode 10: phase correct PWM with ICR4 as Top (= F_CPU/2/25000)
// OC4C as Non-Inverted PWM output
ICR4 = (F_CPU/25000)/2;
OCR4C = ICR4/2; // default: about 50:50
TCCR4A = _BV(COM4C1) | _BV(WGM41);
TCCR4B = _BV(WGM43) | _BV(CS40);

Serial.begin(115200);

// Set the PWM pin as output.
pinMode( 8, OUTPUT);
}

void loop()
{
int w = Serial.parseInt();
if (w>0) {
analogWrite25k(w);
Serial.println(w);
}
}

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Some copy and pasting copies format artifacts that you cannot see in the IDE.

Tom... :slight_smile:

TomGeorge:
Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Some copy and pasting copies format artifacts that you cannot see in the IDE.

Tom... :slight_smile:

Okay Tom:

Error 302 means this:

Duhh, now i don't see the codebutton in this reply-screen.

Well here it is:
One cause of the /(302) error is copy and paste code from a word processor. You have ASCII codes copied that add spaces, etc to your code. Go through each identified line and remove any extra spaces at the beginning and end of any identified line. Then, (Arduino IDE) go to TOOLS, Auto Format. At least, this cleared up the problem for me.

It would be nice if Arduino just gave me the above message instead of error 302....but the Ascii-errors are unreadable for us so it's a pain anyway.

Also it would be nice if my Arduino didn't change my com-port from 3 to 1 by itself....i got my first superlong errormessage (which i copied from the arduino error-copy-button) and posted it here but that ruined my whole thread, i dont understand why Arduino gives such long errormessages anyway but i'm happy it works finally after many hours research.

At least i didn't give up and install a non-pwm-fan...it was worth all the efforts, PWM-fans are so much better. I'm glad it works perfect now.

Hi,

Is it possible with this code to have multiple fans wired to pin 6, 7 and 8 and set different fan speed for each pins?