user62: I'm currently working with the Talon SR Speed Controller and it requires a specific Input PWM Signal of 1-2 ms @ 333 Hz and an input resolution of 10-bit (1024 steps). Currently I use the Servo library to handle these requirements, but was having some issues of the motors jumping their dead-band from forwards to backwards really fast causing the motors to rock back and forwards a few millimeters. If the servo1.write(pwmValue); isn't my issues please let me know?
If there is a way to specifically achieve the 333Hz PWM signal for 4 motors of the same Uno or Due (most likely will transfer to a Due in a few days) I would like to implement that.
From my understand I can modify the timers registers for the arduino specifically, but don't know exactly how to do that or to achieve 333Hz. If someone could please help me understand this I would greatly appreciate it.
Speed SR  Speed Controller Information:
Input voltage 6-28 VDC
Continuous current 60 A
Peak current 100 A
Input PWM signal 1-2 ms @ 333 Hz
Input resolution 10-bit (1024 steps)
Output resolution 10-bit (1024 steps)
Output switching frequency 15 kHz
4% neutral dead band


user3485: This will command a Talon SR from an Arduino.  I used an Uno and had the Talon hooked up to a CIM motor & gearbox and it ran fine.  I don't understand the code well enough to know if it's generating the 1-2ms PWM signal you asked about and your control resolution will definitely be limited to roughly 1% of range - all I know is that it works.
First, make sure your wiring is set up:
Black PWM wire goes to GND
Red PWM wire goes to 5V (right next to GND on the Arduino)
White PWM wire goes to pin 3 (or some other Arduino PWM pin)
Then try this code:
#include <Servo.h>  //This comes with the Arduino

int talon_pin = 3;

Servo talon;

/* The 'Servo.h' library allows you to control motors and servos by passing any value between
0 and 180 into the "write" method (talon.write() in this program).
For a motor:   0 is full power in one direction,
             180 is full direction in the opposite direction,
              90 is stop.
For a servo:   0 is all the way one direction,
             180 is all the way in the opposite direction,
              90 is the middle.
*/

int talon_max_forward = 180;  //this might not actually be forward
int talon_max_reverse = 00;    //this might not actually be reverse
int talon_stop = 90;          //this is definitely stop (if the Talon is calibrated right)

void setup() {  
  talon.attach(talon_pin);  //This tells the arduino to control this pin like it's a servo/motor  
}

void loop() {  
  talon.write(talon_max_forward);   //full power one way
  delay(2000);                      //wait 2 seconds
  talon.write(talon_stop);          //full power the other way
  delay(2000);                      //wait 2 seconds
  talon.write(talon_max_reverse);   //full power one way
  delay(2000);                      //wait 2 seconds
  talon.write(talon_stop);          //full power the other way
  delay(2000);                      //wait 2 seconds
}

The Talon I used was fresh out of the box and had never been calibrated before, so the "stop" didn't actually stop - it just moved slowly.
To calibrate it, use a paper clip or pen / pencil to press and release the CAL button on the Talon (next to the LED).  You have to press AND release when commanded to stop (after going full + and full - in between), but the motor doesn't turn when the button is pressed.  I calibrated by running the program above with the motor attached, waiting for the motor to "stop", then pressing and holding for 8 seconds.  If you have trouble hitting it, increase one of the delays after a talon_stop command.

user304: I am playing on a Arduino UNO, you would have to compare the datasheets to determine differences.
For my research on this answer I am looking at:

Code/PwmFrequency (for a quick rundown)
Tutorial/PWM
TimerPWMCheatsheet (explains the prescaller and resulting frequencies)
varying the pwm frequency of timer0 and timer2
feature request: adjustable pwm frequencies
pwm frequency library (this is the one you REALLY want)
and the datasheet for the ATMega328

Also:

we know PWM pins are D3, D5, D6, D9, D10, D11
I am using D11 for this since I can attach my oscilloscope to the MOSI pin of the ISCP
D11 maps to PB3 (MOSI/OC2A/PCINT3) (see datasheet or this cool gfx)


Normal usage
First, let's see what happens when we set an arbitrary PWM on pin 11
int DUTY = 25;
analogWrite(11, DUTY * 256 / 100);

results in:

Freq: ~487Hz
Duty: ~25%
Time: ~2.05ms

This first point I will make is that with the given 490Hz the PWM duration is actually 2ms which seems to be close to what you want (you said between 1-2ms), maybe my misunderstanding there.

Playing with pre-scalers
From several of the links posted above, you will see that you have different pre-scalers available depending on which timer you are using: eg.
Pins 5 and 6: controlled by Timer 0 in fast PWM mode (cycle length = 256)
Setting     Divisor     Frequency
0x01        1           62500
0x02        8           7812.5
0x03        64          976.5625
0x04        256         244.140625
0x05        1024        61.03515625

TCCR0B = TCCR0B & 0b11111000 | <setting>;

Pins 9 and 10: controlled by timer 1 in phase-correct PWM mode (cycle length = 510)
Setting     Divisor     Frequency
0x01        1           31372.55
0x02        8           3921.16
0x03        64          490.20
0x04        256         122.55
0x05        1024        30.64

TCCR1B = TCCR1B & 0b11111000 | <setting>;

Pins 11 and 3: controlled by timer 2 in phase-correct PWM mode (cycle length = 510)
Setting     Divisor     Frequency
0x01        1           31372.55
0x02        8           3921.16
0x03        32          980.39
0x04        64          490.20
0x05        128         245.10
0x06        256         122.55
0x07        1024        30.64

TCCR2B = TCCR2B & 0b11111000 | <setting>;
 
(All frequencies are in Hz and assume a 16000000 Hz system clock.)

These are relatively simple to setup and use (the setting is at the bottom of each of the above code blocks), but you have to be mindful of playing with the pre-scaler of timer0 (which controls millis() and delay()). So if you have a choice, steer clear of timer0, or pwm pins 5 and 6.
Example of using just the inbuilt pre-scalers without any additional libraries:
#define PIN 11
void setup(){
    pinMode(PIN,OUTPUT);  // not absolutely required
    int DUTY = 20;
    TCCR2B = TCCR2B & 0b11111000 | 6;
}


void loop() {
  static long counter = 0;
  if(millis()%10==0){
      counter++;
      analogWrite(PIN, counter%255);
  }
}

I know you can manipulate the frequency better than that, and I have been playing with this for hours and then I finally stumbled upon the final link (which believe it or not, I found via YouTube!).

Using an External Library
To use the PWM library you will need to download it from Google Code, save/import/get it into your library folder, and restart the IDE. Once you do that, you can explicitly set the frequency (within limits, 333Hz is within those limits), and you should be set.
Code example:
#include <PWM.h>
int32_t frequency = 333;

#define PIN 10

void setup(){
    pinMode(PIN,OUTPUT);
    int DUTY = 20;
    InitTimersSafe();                    // won't touch timer0
    SetPinFrequencySafe(PIN,frequency);  // again, not timer0
    pwmWrite(PIN, DUTY * 256 / 100);
}

There is even a pwmWriteHR which allows for higher resolution, but that does not work with timer2 (which is only an 8-bit timer).
The matching, non-safe (ie initialises and uses timer0 as well) functions are:
InitTimer();  // and
SetPinFrequency(PIN,frequency);

The only limitation I have found so far is that it doesn't work with PWM pin D11. I have tested D3, D9 and D10 (they all worked fine), but I did not get around to testing D5 or D6.
Good luck, I hope this helps.

62: <p>I'm currently working with the <code>Talon SR Speed Controller</code> and it requires a specific Input PWM Signal of <code>1-2 ms @ 333 Hz</code> and an input resolution of <code>10-bit (1024 steps)</code>. Currently I use the <code>Servo</code> library to handle these requirements, but was having some issues of the motors jumping their dead-band from forwards to backwards really fast causing the motors to rock back and forwards a few millimeters. If the <code>servo1.write(pwmValue);</code> isn't my issues please let me know?</p>

<p>If there is a way to specifically achieve the 333Hz PWM signal for 4 motors of the same Uno or Due (most likely will transfer to a Due in a few days) I would like to implement that.</p>

<p>From my understand I can modify the timers registers for the arduino specifically, but don't know exactly how to do that or to achieve 333Hz. If someone could please help me understand this I would greatly appreciate it.</p>

<p><strong>Speed SR  Speed Controller Information:</strong></p>

<pre><code>Input voltage 6-28 VDC
Continuous current 60 A
Peak current 100 A
Input PWM signal 1-2 ms @ 333 Hz
Input resolution 10-bit (1024 steps)
Output resolution 10-bit (1024 steps)
Output switching frequency 15 kHz
4% neutral dead band
</code></pre>
 3485: <p>This will command a Talon SR from an Arduino.  I used an Uno and had the Talon hooked up to a CIM motor &amp; gearbox and it ran fine.  I don't understand the code well enough to know if it's generating the 1-2ms PWM signal you asked about and your control resolution will definitely be limited to roughly 1% of range - all I know is that it works.</p>

<p>First, make sure your wiring is set up:</p>

<p>Black PWM wire goes to GND
Red PWM wire goes to 5V (right next to GND on the Arduino)
White PWM wire goes to pin 3 (or some other Arduino PWM pin)</p>

<p>Then try this code:</p>

<pre><code>#include &lt;Servo.h&gt;  //This comes with the Arduino

int talon_pin = 3;

Servo talon;

/* The 'Servo.h' library allows you to control motors and servos by passing any value between
0 and 180 into the "write" method (talon.write() in this program).
For a motor:   0 is full power in one direction,
             180 is full direction in the opposite direction,
              90 is stop.
For a servo:   0 is all the way one direction,
             180 is all the way in the opposite direction,
              90 is the middle.
*/

int talon_max_forward = 180;  //this might not actually be forward
int talon_max_reverse = 00;    //this might not actually be reverse
int talon_stop = 90;          //this is definitely stop (if the Talon is calibrated right)

void setup() {  
  talon.attach(talon_pin);  //This tells the arduino to control this pin like it's a servo/motor  
}

void loop() {  
  talon.write(talon_max_forward);   //full power one way
  delay(2000);                      //wait 2 seconds
  talon.write(talon_stop);          //full power the other way
  delay(2000);                      //wait 2 seconds
  talon.write(talon_max_reverse);   //full power one way
  delay(2000);                      //wait 2 seconds
  talon.write(talon_stop);          //full power the other way
  delay(2000);                      //wait 2 seconds
}
</code></pre>

<p>The Talon I used was fresh out of the box and had never been calibrated before, so the "stop" didn't actually stop - it just moved slowly.</p>

<p>To calibrate it, use a paper clip or pen / pencil to press and release the CAL button on the Talon (next to the LED).  You have to press AND release when commanded to stop (after going full + and full - in between), but the motor doesn't turn when the button is pressed.  I calibrated by running the program above with the motor attached, waiting for the motor to "stop", then pressing and holding for 8 seconds.  If you have trouble hitting it, increase one of the delays after a talon_stop command.</p>
 304: <p>I am playing on a Arduino UNO, you would have to compare the datasheets to determine differences.</p>
<p>For my research on this answer I am looking at:</p>
<ul>
<li><a href="http://playground.arduino.cc/Code/PwmFrequency#.Uw7UNfSSxfg" rel="nofollow noreferrer">Code/PwmFrequency (for a quick rundown)</a></li>
<li><a href="http://arduino.cc/en/Tutorial/PWM#.Uw7P6fSSxfh" rel="nofollow noreferrer">Tutorial/PWM</a></li>
<li><a href="http://playground.arduino.cc/Main/TimerPWMCheatsheet#.Uw8UafSSxfg" rel="nofollow noreferrer">TimerPWMCheatsheet (explains the prescaller and resulting frequencies)</a></li>
<li><a href="http://forum.arduino.cc/index.php/topic,16612.0.html" rel="nofollow noreferrer">varying the pwm frequency of timer0 and timer2</a></li>
<li><a href="http://forum.arduino.cc/index.php/topic,46949.0.html" rel="nofollow noreferrer">feature request: adjustable pwm frequencies</a></li>
<li><a href="http://forum.arduino.cc/index.php?topic=117425.30" rel="nofollow noreferrer">pwm frequency library (this is the one you REALLY want)</a></li>
<li>and the datasheet for the <a href="http://www.atmel.com/ru/ru/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf" rel="nofollow noreferrer">ATMega328</a></li>
</ul>
<p>Also:</p>
<ul>
<li>we know PWM pins are D3, D5, D6, D9, D10, D11</li>
<li>I am using D11 for this since I can attach my oscilloscope to the MOSI pin of the ISCP</li>
<li>D11 maps to PB3 (MOSI/OC2A/PCINT3) (see datasheet or <a href="http://arduino.cc/en/Hacking/PinMapping168#.Uw7gxfSSxfg" rel="nofollow noreferrer">this cool gfx</a>)</li>
</ul>
<hr />
<h2>Normal usage</h2>
<p>First, let's see what happens when we set an arbitrary PWM on pin 11</p>
<pre><code>int DUTY = 25;
analogWrite(11, DUTY * 256 / 100);
</code></pre>
<p>results in:</p>
<ol>
<li>Freq: ~487Hz</li>
<li>Duty: ~25%</li>
<li>Time: ~2.05ms</li>
</ol>
<p>This first point I will make is that with the given 490Hz the PWM duration is actually 2ms which seems to be close to what you want (you said between 1-2ms), maybe my misunderstanding there.</p>
<hr />
<h2>Playing with pre-scalers</h2>
<p>From several of the links posted above, you will see that you have different pre-scalers available depending on which timer you are using: eg.</p>
<p><strong>Pins 5 and 6: controlled by Timer 0 in fast PWM mode (cycle length = 256)</strong></p>
<pre><code>Setting     Divisor     Frequency
0x01        1           62500
0x02        8           7812.5
0x03        64          976.5625
0x04        256         244.140625
0x05        1024        61.03515625

TCCR0B = TCCR0B &amp; 0b11111000 | &lt;setting&gt;;
</code></pre>
<p><strong>Pins 9 and 10: controlled by timer 1 in phase-correct PWM mode (cycle length = 510)</strong></p>
<pre><code>Setting     Divisor     Frequency
0x01        1           31372.55
0x02        8           3921.16
0x03        64          490.20
0x04        256         122.55
0x05        1024        30.64

TCCR1B = TCCR1B &amp; 0b11111000 | &lt;setting&gt;;
</code></pre>
<p><strong>Pins 11 and 3: controlled by timer 2 in phase-correct PWM mode (cycle length = 510)</strong></p>
<pre><code>Setting     Divisor     Frequency
0x01        1           31372.55
0x02        8           3921.16
0x03        32          980.39
0x04        64          490.20
0x05        128         245.10
0x06        256         122.55
0x07        1024        30.64

TCCR2B = TCCR2B &amp; 0b11111000 | &lt;setting&gt;;
 
(All frequencies are in Hz and assume a 16000000 Hz system clock.)
</code></pre>
<p>These are relatively simple to setup and use (the setting is at the bottom of each of the above code blocks), but you have to be mindful of playing with the pre-scaler of timer0 (which controls <code>millis()</code> and <code>delay()</code>). So if you have a choice, steer clear of timer0, or pwm pins 5 and 6.</p>
<p>Example of using just the inbuilt pre-scalers without any additional libraries:</p>
<pre><code>#define PIN 11
void setup(){
    pinMode(PIN,OUTPUT);  // not absolutely required
    int DUTY = 20;
    TCCR2B = TCCR2B &amp; 0b11111000 | 6;
}


void loop() {
  static long counter = 0;
  if(millis()%10==0){
      counter++;
      analogWrite(PIN, counter%255);
  }
}
</code></pre>
<p>I know you can manipulate the frequency better than that, and I have been playing with this for hours and then I finally stumbled upon the final link (which believe it or not, I found via YouTube!).</p>
<hr />
<h2>Using an External Library</h2>
<p>To use the PWM library you will need to download it from <a href="http://code.google.com/p/arduino-pwm-frequency-library/downloads/list" rel="nofollow noreferrer">Google Code</a>, save/import/get it into your library folder, and restart the IDE. Once you do that, you can explicitly set the frequency (within limits, 333Hz is within those limits), and you should be set.</p>
<p>Code example:</p>
<pre><code>#include &lt;PWM.h&gt;
int32_t frequency = 333;

#define PIN 10

void setup(){
    pinMode(PIN,OUTPUT);
    int DUTY = 20;
    InitTimersSafe();                    // won't touch timer0
    SetPinFrequencySafe(PIN,frequency);  // again, not timer0
    pwmWrite(PIN, DUTY * 256 / 100);
}
</code></pre>
<p>There is even a <code>pwmWriteHR</code> which allows for higher resolution, but that does not work with timer2 (which is only an 8-bit timer).</p>
<p>The matching, non-safe (ie initialises and uses timer0 as well) functions are:</p>
<pre><code>InitTimer();  // and
SetPinFrequency(PIN,frequency);
</code></pre>
<p>The only limitation I have found so far is that it doesn't work with PWM pin D11. I have tested D3, D9 and D10 (they all worked fine), but I did not get around to testing D5 or D6.</p>
<p>Good luck, I hope this helps.</p>
