Go Down

Topic: changing PWM frequency (Read 13043 times) previous topic - next topic

zeveland

I'm driving a coreless DC motor with PWM, and the motor's manufacturer recommends at least a 20 kHz PWM frequency.  Arduino 003 provides a 32 kHz PWM frequency, but along with 004 adding the third PWM channel it changed the frequency to 1 kHz (nominally - I'm seeing 500 Hz on OC1A).

I'm guessing this means a pre-scaler of 32 or 64 using the 16 MHz clock.  Does anyone have any thoughts on changing the pre-scale value back so I can take advantage of 004's features?

Benoît ROUSSEAU

Hello Zeveland,

 Yes, you can do it, by changing some code in the file wiring.c. Look in the main function and in files timer.c and timer.h to understand what is done.

 The wiring.c file is shared by all your Arduino projects, save a copy before modifications.

 If you don't want to modify your wiring.c file, I think you can do something like this in the Setup function of your Arduino project :

Code: [Select]

#define TIMER_CLK_STOP            0x00      ///< Timer Stopped
#define TIMER_CLK_DIV1            0x01      ///< Timer clocked at F_CPU
#define TIMER_CLK_DIV8            0x02      ///< Timer clocked at F_CPU/8
#define TIMER_CLK_DIV64            0x03      ///< Timer clocked at F_CPU/64
#define TIMER_CLK_DIV256            0x04      ///< Timer clocked at F_CPU/256
#define TIMER_CLK_DIV1024            0x05      ///< Timer clocked at F_CPU/1024

void Setup()
{
[...]

TCCR1B = (TCCR1B & ~TIMER_PRESCALE_MASK) | TIMER_CLK_DIV8);

[...]
}


I does not test this code ! ! ! But I think it could work. Tell me if it work.

Best regards,
Benoît ROUSSEAU
Cordialement,
Benoît ROUSSEAU

zeveland

Thanks!  I ended up directly assigning a new value to TCCR1B in setup and it worked.  Making TCCR1B = 0x09 changes the timer to no pre-scaling and doesn't seem to have broken anything, though I haven't had a chance to test it extensively yet.  

With the change, OC1A runs at 64 kHz at 50% duty cycle, and about 10 kHz at the lower and 25 kHz around the upper limit of the eight-bit range.

Thanks again for your help.

izoard056

I have a question referred to the pwm frequency of the different pwm pins. I read that this frequency is generally 500Hz (see pwm site). But here http://www.arduino.cc/playground/Code/PwmFrequency it says that the frequency is higher (62kHz for pin 5 and 6). Could anyone shed some lite into this issue.
Thanks everybody.
-izoard

gravelbar

izoard056, did you ever get this sorted out?  I'm trying to simply drive a 12v pump with PWM, but want a frequency above 20khz or so that's not audible; I'm new to this and read that the pwm output on all pins is ~500hz, which contradicts what I'm reading in your post.  Help?  Thanks!

georgew77

gravelbar

http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM

gravelbar

Thanks much, this is a very good (and complete and clear!) article.  But we're looking for a simple way to do this so students can build it, and messing with the timers is too much I think.  I'm about to decide there is no simple way to get >500hz, and am wondering if low freqs, like 20 or 30 hz would work with our little 12v pumps.  The don't have a flywheel at all, but of course are nicely cushioned since they're only pushing on water. :)

This is for a non profit educational venture--any help much appreciated!

tomCaad

For better understanding Timers and their usage, I collected some knowledge and additional links (including georgew77 s link) in those two articles - hope it helps:
How to set prescalers, some background on bit-Math, how to control a strong motor:
http://www.embedded.arch.ethz.ch/Examples/TimerDcMotorBackground
Use a photointerrupter as external clock at T1 and count the slots passed:
http://www.embedded.arch.ethz.ch/Examples/Photointerrupter
please do NOT post comments / improvements here, feel free to email (contact in articles) thanks.

Go Up