Arduino Uno – High Frequency PWM

I did some tests to see what max PWM frequency you can archive without overclocking the Uno and running it with the stock 16Mhz crystal. The default Arduino PWM frequency is 500Hz. If you need a higher frequency you have to manually bit-bang one of the output pins.

This example shows how to do a 30% duty cycle. The delays (and the overhead of the other instructions) determine the PWM frequency. In this case I got a PWM frequency of 9.1kHz . I disabled all interrupts to avoid any interference from processing interrupt service routines.

const int PWM_PIN = 8;

void setup()
{
  pinMode(PWM_PIN, OUTPUT);
  noInterrupts();

  // Don't use loop() to avoid serialEventRun overhead
  while (true) {
    // 30% duty
    digitalWrite(PWM_PIN, HIGH);
    delayMicroseconds(30);
    digitalWrite(PWM_PIN, LOW);
    delayMicroseconds(70);
  }
}

void loop(){}

Continue reading “Arduino Uno – High Frequency PWM”

Arduino Uno – High Frequency PWM