Changing PWM frequency on Leonardo

Hi all,

I bought myself a LattePanda Mini-PC. The nice thing is that it has an Arduino Leonardo integrated as well which can be accessed through COM-Ports.
I am now trying to setup a cooling fan control for it.
My fan only has a 2-pin connector so there it does not have a PWM connector. There are no pwm-cooling fans on the market that fit in the case I have for the board.

Therefore I created a 1-transistor board that acts as a switch for the cooling fan. The transistor is connected to pin 11 of the Arduino.
I have the standard firmata running on the Arduino and on the PC I have a C# program running that reads the CPU temperature of the board and uses analogWrite() to adjust the Voltage of the cooling fan.
analogWrite(11, 0) and analogWrite (11, 255) are working fine. So switching the cooling fan on or off is no problem.
However when I choose values in between the cooling fan gives a high tone. Most likely the default pwm frequency of the Arduino is too low. Therefore I am now trying to adjust the default pwm frequency. A value of 31kHz might be a good idea.
I’ve googled around and found this:

Also here in the forum are lots of threads as well but I could not make out a final solution in any of these. Most of them end with the thread creator saying “I fiddled around. Now it’s working.” Or no answer at all.

Looking at the above link it seems like 31KHz is not supported on Pin 11 though. But 62kHz is what I am now trying to do.
Looking at everything I found Pin 11 might not have been a good choice anyway due to the Arduinos internal functions relying on the timer this pin uses. Although in one thread I found that it might be possible to change the timer for this pin.
Anyway I’d like to give it a try without resoldering to another pin for now.

With the help of the above link I added this function to the StandardFirmata:

void pwm91011configure(int mode)
// TCCR1A configuration
//  00 : Channel A disabled D9
//  00 : Channel B disabled D10
//  00 : Channel C disabled D11
//  01 : Fast PWM 8 bit

// TCCR1B configuration
// Clock mode and Fast PWM 8 bit

// TCCR1C configuration

And then I changed the setup() function to this. Basically I just added a call to the new function.

void setup()

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
  Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback);
  Firmata.attach(START_SYSEX, sysexCallback);
  Firmata.attach(SYSTEM_RESET, systemResetCallback);

  // to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega,
  // Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this:
  // Serial1.begin(57600);
  // Firmata.begin(Serial1);
  // However do not do this if you are using SERIAL_MESSAGE

  while (!Serial) {
    ; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101
  systemResetCallback();  // reset to default config

The new firmata sketch compiles and uploads fine.
I don’t have an oscilliscope to measure whether it’s working or not unfortunately.
But the cooling fan still gives a high tone when I use analogWrite with anything other than 0 or 255. So I guess that my code is not working.

What am I doing wrong here?

Thanks a lot. :slight_smile:

So, have you found a solution?