Pages: [1]   Go Down
Author Topic: PWM Question  (Read 718 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Guys,

im not sure if this is in the correct area but how is it possible to set the PWM frequency to 25k. I have seen a few examples but to be honest they confuse me somewhat. What is the simplest way to go about this?

Secondly, are you able to run multiple fans off the same PWM pin if you want the same Duty Cycle and each fan has its 12v supplies going to them?

Cheers
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how is it possible to set the PWM frequency to 25k.

Yes.

Quote
What is the simplest way to go about this?

Read the datasheet and code.
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5661
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im not sure if this is in the correct area but how is it possible to set the PWM frequency to 25k. I have seen a few examples but to be honest they confuse me somewhat. What is the simplest way to go about this?

Change the timer prescaler value. It's in the datasheet, look at the registers for the timers. You want TCCRnB.

Secondly, are you able to run multiple fans off the same PWM pin if you want the same Duty Cycle and each fan has its 12v supplies going to them?

There'll be a way, yes, but our telepathic powers are weak so you'll to come up with a lot more information than that.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the code I used for driving PC fans on a Uno. Use this code in setup:

Code:
  // Only timer/counter 1 is free because TC0 is used for system timekeeping (i.e. millis() function),
  // and TC2 is used for our 1-millisecond tick. TC1 controls the PWM on Arduino pins 9 and 10.
  // However, we can only get PWM on pin 10 (controlled by OCR1B) because we are using OCR1A to define the TOP value.
  // Using a prescaler of 8 and a TOP value of 80 gives us a frequency of 16000/(8 * 80) = 25KHz exactly.
  TCCR1A = (1 << COM1B1) | (1 << COM1B0) | (1 << WGM11) | (1 << WGM10);  // OC1A (pin 9) disconnected, OC1B (pin 10) = inverted fast PWM 

  OCR1AH = 0;
  OCR1AL = 79;  // TOP = 79
  TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);  // TOP = OCR0A, prescaler = 8

  OCR1BH = 0;
  OCR1BL = 80;  // max fan speed (i.e. pin 5 initially low all the time) 

  TCNT1H = 0;
  TCNT1L = 0;

Call this code to set the speed:

Code:
// Set the fan speed
void setTransistorFanSpeed(uint8_t fanPercent)
{
  OCR1BH = 0;
  OCR1BL = (fanPercent * 80u)/100;
}

PC PWM fans are designed to be driven from an open collector or open drain output, so I drive them from the Arduino pin using an NPN transistor, or 2N7000 mosfet, or an opto isolator if I need isolation. It's safest to use a separate transistor to drive each fan, although the PWM fans I have bought come with cables that just connect the PWM input in parallel with the one for the CPU fan.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So does this mean that we can not use Pin 9 for anything?

Matt
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can still use pin 9 for digital input or output, just not for PWM.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: