Advanced PWM signalling.

My latest blog post is on how to do advanced PWM signaling. It covers in depth how to set our own custom frequency and how it works.

http://thecodebender.com/journal/2009/2/21/we-just-cant-leave-things-well-enough-alone.html

Hmmm .. some good to know info there!! :)

Just one thing, your article states that the PWM on the Arduino is based on the external crystal frequency e.g. 16MHz. I thought it was +- 490 Hz by default as per the wiki http://www.arduino.cc/en/Reference/AnalogWrite?

BTW, how does one decide on the correct frequency for a brushed dc motor? From what I have seen most folks recommend +-100Hz for simple (open loop) applications? :-? (Question moved to FAQ section ..)

Hey, thanks for the question, as best as I can figure, the default PWM analog out frequency is as you say the +- 490Hz, however once I invoke the registers that kinda gets shot to pieces and everything is based on the external clock. I did revise my article though, so once again, thanks.

The frequency chosen to control the motors in our case is more the frequency chosen to control the pololu motor controller boards. In this case they'll take a max of 40KHz, the higher you push the more amps they draw from our experience. I haven't investigated the motor controllers enough to know exactly whats going on in there. We tried to build our own but had problems with the PMOS chips constantly... exploding in our faces.

I also stumbled on something I failed to explain in the last code block, the code now reads:

void loop() {


 dutycycle = analogRead(0);

 //Here, you want to take your period and divide by 1024 
 to find what  scalar you need to multiply your duty cycle 
 in our case it was close enough that we didn't need a 
 multiplier here for testing purposes but your mileage 
 may vary.

 dutycycle = dutycycle;

 analogWrite(PWM3,dutycycle);
 digitalWrite(DIRECTION3,dir);

 //From here down, were changing the direction every 5000
 loops and printing the direction for debug purposes.

 if(count==5000){
   dir = (dir+1)%2;
   count = 0;
 }

 count++;

 Serial.print(dir);
 Serial.print('\n');


}

Thanks for clearing that up. Since these registry entries are just begging to be played with ::) are there any other implications to Arduino 'defaults' that you know of?

I'm doing a bit more research into things as I believe I may have made a false assumption that the second parameter of the Analog Write is infact the actual duty cycle(time high). The article shows the second parameter being a value from 0-255 but this may be a result from choosing the frequency to be about 490Hz... More investigating.

A 490Hz wave would correspond to a period of 2.04ms which doesn't quite line up with 255us. It's off by about a factor of ten.

And... I'm finding really conflicting things. If anyone knows one way or the other please let me know. I believe this might be another one of those "by invoking these registers I've shot the defaults" situations.

I've been watching the PWM frequency related threads lately. You may find these useful in clearing some things up. See specifically the posting by macegr in the first link below:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/8#8 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234764073

Thanks for the links koyaanisqatsi. I’ve made quite a few corrections now that I’ve found the time. The details should be ironed out now.

To anyone who read this previously, please re-read if you found my information helpful, after testing a lot of things I’ve found several crucial errors that have now been corrected:

http://thecodebender.com/journal/2009/2/21/we-just-cant-leave-things-well-enough-alone.html