PWM at 500kHz

I am trying to control some Victor 884 motor controllers with pwm. The problem is I need pwm at 500kHz not the default 500Hz.

Can I do that like this, but when I try to do it with different duty cycles on multiple pins the code is hard to keep track of.

#define LED_PIN 13

void setup() {
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_PIN, HIGH);
    delayMicroseconds(1500);
    digitalWrite(LED_PIN, LOW);
    delayMicroseconds(5000);
}

So can I make the arduino have pwm that fast or do I need to build an external circuit?

500 kHz (subject line)? 50 kHz? Which?

Does the frequency have to be exactly 500 kHz (or 50 kHz)?

http://arduino.cc/forum/index.php/topic,16612.0.html

Sorry about that, it was a typo. I fixed it.

To answer your second question, yes it need to be exactly 500kHz.

With a 16 MHz processor you will only get 32 steps which is about 3% per step. Is that acceptable?

32 steps is not great, but I think it will work.

See: http://www.gammon.com.au/forum/?id=11504

Scroll down to: "Modulating 38 KHz signal"

Change the constant timer1_OCR1A_Setting as appropriate.

So after using that code, the pwm on pins 11 and 12(I am using an arduino mega) should be running at my desired speed?

That particular code was for a Uno.

I note that your recent reply was the first time a Mega was mentioned in this thread.

You can help us to help you, by specifying as much detail as possible.

Sorry about that, I haven't got into any of nitty-gritty chipset specific code before now.

So the code will not work with my mega?

    digitalWrite(LED_PIN, HIGH);
    delayMicroseconds(1500);
    digitalWrite(LED_PIN, LOW);
    delayMicroseconds(5000);

500Khz -> 2us period.

Your code has a period of at least 6500us -> 130hz? Off by about 99%.

Why on Earth does an ESC need a half megahertz signal?

Groove:
Why on Earth does an ESC need a half megahertz signal?

I agree it sounds crazy. I think he might be confusing the PWM rate the controller uses to power the motor windings rather then the PWM input signal it uses for speed control. From a quick goggle search at http://www.firstwiki.net/index.php/Victor_884 it looks to me that you control that controller via a standard servo library output commands, 1msec to 2msec pulse duration at a 50Hz frame rate.

Programming

The PWM outputs on the Robot Controller can be set across the normal hobby servo range, 1 to 2 ms. The 8-bit PWM channels used on the PIC microprocessor of the Robot Controller (and Vex controller) has a resolution of 256 values, which fit in an unsigned char. Zero commands full reverse, 127 commands stop, and 254 commands full forwards. The command 255 should be avoided on older RCs because it acts as a special signal command when transmitted between the Robot Controller and OI. Newer versions of the RC seem to have no issue with a 255 command. However, for code portability, 254 should be the maximum value used (besides, using 254 results in symmetric forward and reverse resolutions).
Note that a neutral PWM command to a factory calibrated Victor consists of a pulse of about 1.5 ms duration. As such, when no PWM input is applied, the Victor will detect this and flash it's LEDs yellow.

Lefty

Not sure if you have realized but the avr can do 16Mhz pwm. So 500Khz is piece of cake.

Piece of cake or not, I'd still like to know what sort of device requires PWM at that frequency.
There's going to be some serious RFI issues there.

The code could be adjusted. This low-level stuff requires getting the internal registers right on a per-chip basis.

However I strongly agree with the other posters. It looks like you have misread the datasheet.

500 KHz is in the radio frequency range.