Changing PWM Frequency of the Arduino

userHead Digioso 2016-08-24 06:06:39 4055 Views1 Replies
Hi,

I am trying to setup a fan control using PWM and the analogwrite() function.
However when I set a value below 255 my cooling fan starts rotating but also gives a high tone.
I was told that the reason for this is most likely the PWM frequency of the Arduino and that it is probably too low.
It was suggested to change it to at least 31kHz.

I googled a bit around and found the following:
https://r6500.blogspot.de/2014/12/fast- ... nardo.html
http://playground.arduino.cc/Code/PwmFrequency

I am using pin 11 of the Arduino. From the links it looks like that 31kHz is not supported on pin 11. Instead I am using 62kHz.

The example from the playground link doesn't compile. I think this is not for a Arduino Leonardo so the registers do not exist.

The blogspot link compiles but I guess it doesn't change the frequency.
I am using the StandardFirmata on the Arduino and changed it to this:

I added this function:

Code: Select all

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 TCCR1A=1; // TCCR1B configuration // Clock mode and Fast PWM 8 bit TCCR1B=mode|0x08; // TCCR1C configuration TCCR1C=0; } And I changed the setup() function to this. Basically I just added a call to the other function.

Code: Select all

void setup() { Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); 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 Firmata.begin(57600); while (!Serial) { ; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101 } pwm91011configure(1); systemResetCallback(); // reset to default config } Is this the wrong way to do? Does it even work with the standard firmata?