Arduinos original PWM

Hello everybody,

i´ve read a lot about the PWM at Arduinos ATmega328.
Somewhere it´s said: ....the base frequency for pins 5 and 6 is 62500 Hz....
and somewhere else it´s said: ....In other words, with Arduino's PWM frequency at about 500Hz....

Can anybody please give me a tip, which is the right PWM Frequency at my UNO R3 (Timer 0...2) or where i can find a good site for understanding this stuff :expressionless:
Actually i am writing an essay about my work with Arduino and how to control a DC-Motor(module).

Thank you buddies.
K86

If you do a standard
analogWrite (pin, value);
to the pins marked as PWM, the frequency will be 488-490 Hz.
If you want a different frequency, than changes are needed.

But why is it said here: Arduino Playground - PwmFrequency

// Set pin 6's PWM frequency to 62500 Hz (62500/1 = 62500)
// Note that the base frequency for pins 5 and 6 is 62500 Hz
setPwmFrequency(6, 1);

???

That requires your sketch to include the "function" that is inlcuded at the bottom of the page.
If your sketch does not have this function, you cannot use this command

setPwmFrequency(6, 1);

and the analogWrite frequency will be ~490 Hz.

void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}

Ah okay, thank you very much.

But i have a second question :slight_smile:
I am using a Motor Driver (Pololu High-Power Motor Driver 24v12) which is controlled by the PWM at Pin6. While i am driving the DC-motor, i can hear a continous sound like peeeeeeeeeeep
I thought this could be the hearable PWM, but 500HZ is far under the limit of 3,5KHz...

How could i fix this? Any idea?

Kelevra86:
But i have a second question :slight_smile:
I am using a Motor Driver (Pololu High-Power Motor Driver 24v12) which is controlled by the PWM at Pin6. While i am driving the DC-motor, i can hear a continous sound like peeeeeeeeeeep
I thought this could be the hearable PWM, but 500HZ is far under the limit of 3,5KHz...

Even if it was within the audible frequency range, it won't be the PWM signal itself. The only way you could directly hear the signal would be if you were feeding the PWM into some sort of sound transducer, like a piezo disk or more traditional speaker.

Instead the sound is probably being produced by the DC motor. Although much quieter than internal combustion engines, electrical motors often aren't completely silent especially when they are moving a non-trivial load. From your description it's difficult to determine if the sound is normal for the motor and the conditions it's working under. However, unless it is loud it likely is nothing to worry about.

The PWM waveform is essentially a square wave which has a lot of high frequency harmonics so it doesn't surprise me that you're hearing something in the 3.5Khz range.... also, the BEMF pulse coming back from a coil that has been briefly powered and then abruptly disengaged (like in a PWM waveform) is filled with even more high frequency harmonics.

Actually, it's a common Radio-control hack to use a motor as an audio transducer. By feeding in a signal at audio frequencies that doesn't actually move the motor, you can vibrate the armature and/or windings enough to generate a clearly audiable tone.

Isn't the PWM frequency about 1kHz on some pins, anf 500kHz on others? The timer shared with the system clock runs at 1kHz and does "simple" PWM, while the timers dedicated to only PWM do "phase correct pwm", which runs at half the frequency. Or something like that.

...sound like peeeeeeeeeeep
I thought this could be the hearable PWM, but 500HZ is far under the limit of 3,5KHz...

What 3.5kHz "limit" are you talking about? Somewhere around there is the UPPER frequency limit for phone systems, but the range of human hearing goes WAY down to 20Hz or so (at any rate, if you work with audio at all, 60Hz "leakage" from the AC supply becomes a clearly recognizable and not all THAT low a note. Middle-A is 440Hz. 1000Hz is up in the "peeep" range... (Youtube has a wide variety of test tones: 6KHz / 6000 Hz Test tone / Sound - Tweeter Test - YouTube )

westfw:
Actually, it's a common Radio-control hack to use a motor as an audio transducer. By feeding in a signal at audio frequencies that doesn't actually move the motor, you can vibrate the armature and/or windings enough to generate a clearly audiable tone.

Hmm, I knew that there are ways to vibrate an electric motor's armature without causing it to rotate, but I didn't know a generic DC motor could generate a sufficiently loud sound this way (at least not without possibly damaging the motor). Nor did I realize that people would do such a thing on purpose. :slight_smile: I've heard "music" made by steppers or old floppy disk drive motors but to the best of my knowledge those examples always involved rotating motors.

In any case, I didn't mean to imply that 500 Hz was below the range of human hearing. I was trying to make a general statement that to get the exact PWM as a tone there needs to be a proper audio transducer. Apparently a non-rotating DC motor can be such a transducer.

Kelevra86:
I´ve read a lot about the PWM at Arduinos ATmega328.
Somewhere it´s said: ....the base frequency for pins 5 and 6 is 62500 Hz....
and somewhere else it´s said: ....In other words, with Arduino's PWM frequency at about 500Hz....

This is really a particular library. You have quite a range of PWM frequencies available if you use the hardware timers directly. I think I had an example recently of a 4 MHz PWM signal. There is a trade-off here. The higher the frequency the less choice of duty cycles. The reason is simple, the higher the frequency the lower the period. So as an example, if the period is 16 (clock cycles) then you get a frequency of 1 MHz. However you only have 16 choices of duty cycle (14 if you don't count "all on" and "all off").

Kelevra86:
Hello everybody,

i´ve read a lot about the PWM at Arduinos ATmega328.
Somewhere it´s said: ....the base frequency for pins 5 and 6 is 62500 Hz....
and somewhere else it´s said: ....In other words, with Arduino's PWM frequency at about 500Hz....

Can anybody please give me a tip, which is the right PWM Frequency at my UNO R3 (Timer 0...2) or where i can find a good site for understanding this stuff :expressionless:
Actually i am writing an essay about my work with Arduino and how to control a DC-Motor(module).

Thank you buddies.
K86

Timer 0 is set to work in Fast PWM mode so it generates a PWM signal at 976 Hz. The output is on pins D5 & D6.
Timer 1 & timer 2 are se to work in Phase Correct PWM and they generate PWM signals at 490 Hz (pins D3, D9, D10 & D11).

490 Hz

Or 488.28....Hz.

the limit of 3,5KHz...

A limit that exists only in telephony, I think.

leo72:
Timer 0 is set to work in Fast PWM mode so it generates a PWM signal at 976 Hz. The output is on pins D5 & D6.
Timer 1 & timer 2 are se to work in Phase Correct PWM and they generate PWM signals at 490 Hz (pins D3, D9, D10 & D11).

Those settings can be changed. Both the mode, the prescaler, and what it counts to (if it is in a counting mode). What leo72 said was the default settings used for analogWrite.

dhenry:

490 Hz

Or 488.28....Hz.

To be exactly, 488 Hz is the nominal frequency. But we have to consider the tolerance of the ceramic resonators used on UNO boards.

Of course. The ones I published are the default values using Arduino "as it". Keep in mind that altering the timer 0 leads to a change in the behaviour of the delay and millis functions.

But we have to consider the tolerance of the ceramic resonators used on UNO boards.

And temperature the board is in, straight capacitance, voltage fluctuations, and about a gazillion other factors.

And did they actually use ceramic resonators? Ouch!

leo72:
Keep in mind that altering the timer 0 leads to a change in the behaviour of the delay and millis functions.

Absolutely. Which is why I usually use Timer 1 and Timer 2 before I fiddle with Timer 0.

dhenry:

But we have to consider the tolerance of the ceramic resonators used on UNO boards.

And temperature the board is in, straight capacitance, voltage fluctuations, and about a gazillion other factors.

And did they actually use ceramic resonators? Ouch!

It was just to say that I wrote approximate values, I didn't do any calculation to specify the correct frequencies because cer.res. are imprecise so the exaclty frequency changes between an Arduino and another.