Set PWM frequency in Arduino UNO

I am making a drive for BLDC motor with IRF3710 MOSFET and controller as arduino UNO. I want to increase the switching frequency to above 5 kHZ in all pwm pins. Does anyone have the code for it?

This will get you a 7.8 KHz PWM on Timer2 (Pin B3) in FastPWM mode
OCR2A will determine your duty cycle.

/**
  * URL: https://dbuezas.github.io/arduino-web-timers/#mcu=ATMEGA328P&timer=2&timerMode=FPWM&CompareOutputModeA=clear-on-match%2C+set-at-max&OCR2A=137&clockPrescalerOrSource=8
  * Mode     : FPWM
  * Period   : 128 us
  * Frequency: 7.8125 kHz
  * Outputs  : 
  *  - B3: 53.52%, clear-on-match, set-at-max
  */
void setup(){
  noInterrupts();
  TCCR2A = 
    1 << COM2A1 |
    1 << WGM21 |
    1 << WGM20;
  TCCR2B = 
    1 << CS21;
  DDRB = 
    1 << DDB3;
  OCR2A = 136;
  interrupts();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.