PWM speed

I'm wondering what speed the PWM operates? Also, is it possible to change the speed?

Many thanks!

I'm wondering what speed the PWM operates? Also, is it possible to change the speed?

Many thanks!

Found my own answer "The frequency of the PWM signal is approximately 490 Hz." Now that answers a part of my question. Anyone know if I can change this frequency?

Yes.

Example? :)

I don't know how to do it with Arduino library functions, but if you take a look at the mega168 datasheet you can see how to do it using the timer control registers.

1) configure the timer to run in fast PWM mode or phase-correct PWM mode with a specifiable TOP value. This can be accomplished using the TCCRxA and TCCRxB registers.

2) specify the timer clock in terms of a prescaled system clock. This can be accomplished using the last three bits of TCCRxB.

3) specify the count at which the timer overflows (this is known as the TOP). This can usually be accomplished using the OCRxA register.

In fast PWM mode, the PWM frequency will be

F_CPU / prescaler / TOP

In phase-correct PWM mode, the PWM frequency will be

F_CPU / prescaler / TOP / 2

Is this information sufficient or would you like to see a specific example? If so, do you have a preferred timer and/or frequency?

  • Ben

I don't know how to do it with Arduino library functions, but if you take a look at the mega168 datasheet you can see how to do it using the timer control registers.

1) configure the timer to run in fast PWM mode or phase-correct PWM mode with a specifiable TOP value. This can be accomplished using the TCCRxA and TCCRxB registers.

2) specify the timer clock in terms of a prescaled system clock. This can be accomplished using the last three bits of TCCRxB.

3) specify the count at which the timer overflows (this is known as the TOP). This can usually be accomplished using the OCRxA register.

In fast PWM mode, the PWM frequency will be

F_CPU / prescaler / TOP

In phase-correct PWM mode, the PWM frequency will be

F_CPU / prescaler / TOP / 2

Is this information sufficient or would you like to see a specific example? If so, do you have a preferred timer and/or frequency?

  • Ben

Bens, Thanks for your help! I've been looking through the datasheet for the 168 and I came across this as well. Unfortunately, I haven't a clue how to go about incorporating this into my Arduino code. I'm attempting to control a Firgelli L12 actuator. This actuator uses 1000Hz 5V PWM for control. Would this frequency be possible? And, certainly an example would be fantastic!

void setup()
{
  // configure hardware timer2 to generate a fast PWM on OC2B (Arduino digital pin 3)
  // set pin high on overflow, clear on compare match with OCR2B
  TCCR2A = 0x23;
  TCCR2B = 0x0C;  // select timer2 clock as 16 MHz I/O clock / 64 = 250 kHz
  OCR2A = 249;  // top/overflow value is 249 => produces a 1000 Hz PWM
  pinMode(3, OUTPUT);  // enable the PWM output (you now have a PWM signal on digital pin 3)
  OCR2B = 125;  // set the PWM to 50% duty cycle
}

void loop()
{
  OCR2B = 125;  // set the PWM to 50% duty cycle
  do some stuff;
  OCR2B = 50;  // set the PWM to 20% duty cycle
  do some stuff;
  pinMode(3, INPUT);  // turn off the PWM
  do some stuff;
  pinMode(3, OUTPUT);  // turn the PWM back on
}

I just tested this out using an oscilloscope and I get a 1.251 kHz PWM on a mega168 running at 20 MHz, so it should be 1 kHz on your 16 MHz mega168. If you need more resolution in your duty cycle, you can use timer1 (the 16-bit timer) instead of timer2 (an 8-bit timer). Let me know if you have any questions.

  • Ben

Ben, thank you very much! I'll try this out as soon as I get in to work tomorrow.

The easiest way that I can think of changing the frequency of your 'entire' system is to change out the crystal. If you look in the Digi-key catalogue you will find a slew of different crystals that are the same physical size and electronic config as the 16 MHz one used currently. It would be an easy job of desoldering the current one out and voila... I would suggest only going down in speed not up too much.

Try the Digi-key current catalogue, P1241 rt hand side.

Give it a try. Oaxacamatt1