PWM library acts up on Mega

Hi everyone,

for a robot project I need 4 PWM channels with somewhere between 15-25kHz on an Arduino Mega2560. I’m trying to use Sam Knight’s PWM library, but even the following minimal example behaves weird:

#include <PWM.h>

void setup() {
  InitTimersSafe();
  pinMode(3, OUTPUT);
  SetPinFrequency(2, 15000);
  pwmWriteHR(2, 32768);
}

void loop() {
}

This generates a beautiful 15kHz PWM with 50% duty-cycle, but on Pin 3 (!) instead of Pin 2.
Pin 2 is dead silent.

I’ve been trying for 8 hours to find a pattern, but I just don’t get what is happening here.

Ok, so I bruteforced the whole thing to find what numbers I have to give to the lib to drive the pins I want using this sketch and watching the oscilloscope for every pin

#include <PWM.h>

void setup() {
  Serial.begin(9600);
  InitTimersSafe();

  for (int j = 2; j <= 53; j++) {
    pinMode(j, OUTPUT);
  }

  for (int i = 2; i <= 53; i++) {
    if (4 == i) {
      continue;
    }
    for (int j = 2; j <= 53; j++) {
      digitalWrite(j, LOW);
    }
    Serial.print("Trying Pin ");
    Serial.println(i);
    pinMode(i, OUTPUT);
    SetPinFrequency(i, 15000);
    pwmWriteHR(i, 32768);
    delay(1000);
  }



}

void loop() {
}

Pin I want to drive: Pin number I have to give to the PWM library:
2 5
3 2
7 6

8 7
11 11
12 12

45 46
44 45

I still don’t understand what causes this and have to check if I can control the duty cycles individually.