I need to increase frequency of my pins to 20Khz

void setup() {
  // put your setup code here, to run once:
      // 25kHz PWM frekansını üretip analogwrite fonksiyonuyla kullanabilmek için
  
    // Set the main system clock to 8 MHz.
    noInterrupts();

    
    CLKPR = _BV(CLKPCE);  // enable change of the clock prescaler
    CLKPR = _BV(CLKPS0);  // divide frequency by 2
    interrupts();

    // Configure Timer 0 for phase correct PWM @ 25 kHz.
    TCCR0A = 0;           // undo the configuration done by...
    TCCR0B = 0;           // ...the Arduino core library
    TCNT0  = 0;           // reset timer
    TCCR0A = _BV(COM0B1)  // non-inverted PWM on ch. B
        | _BV(WGM00);  // mode 5: ph. correct PWM, TOP = OCR0A
    TCCR0B = _BV(WGM02)   // ditto
        | _BV(CS00);   // prescaler = 1
    OCR0A  = 160;         // TOP = 160 // potansiyometreyi 160a böl

    // Timer 1.
    TCCR1A = 0;
    TCCR1B = 0;
    TCNT1  = 0;
    TCCR1A = _BV(COM1A1)  // non-inverted PWM on ch. A
        | _BV(COM1B1)  // same on ch. B
        | _BV(WGM11);  // mode 10: ph. correct PWM, TOP = ICR1
    TCCR1B = _BV(WGM13)   // ditto
        | _BV(CS10);   // prescaler = 1
    ICR1   = 160;

    // Timer 2.
    TCCR2A = 0;
    TCCR2B = 0;
    TCNT2  = 0;
    TCCR2A = _BV(COM2B1)  // non-inverted PWM on ch. B
        | _BV(WGM20);  // mode 5: ph. correct PWM, TOP = OCR2A
    TCCR2B = _BV(WGM22)   // ditto
        | _BV(CS20);   // prescaler = 1
    OCR2A  = 160;

   // 25kHz PWM üretildi, analogwrite fonksiyonu 25 kHz ile çalışacak
  
  
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(A0,INPUT);
  
  
}

void loop() {
  // put your main code here, to run repeatedly:
    
    

  
  float a=analogRead(A0);
  a = map(a, 0, 1023, 0, 255);  
  
  analogWrite(11, a );
  analogWrite(10, a);
  analogWrite(6, LOW);
  analogWrite(5, LOW);
  
}

Currently i use this code.It is a H-bridge DC motor using P-type MOSFETs on top and N-type MosFETs on bottom. But i don't know if this piece of code works for that frequency adjustment .Can you help me how to write the code for creating 25 khz pwm frequency.I'm beginner for the topic.

WARNING! The Arduino core uses Timer 0 for millis(), micros(), etc. YOU SHOULD NOT USE Timer0.
WARNING! If you accidentally turn on both MOSFETs on the same side you will short power to ground and likely fry something.

When you set TOP to 159 (not 160) to get 25 kHz PWM on an 8 MHz system, all PWM values higher than 159 will be the same: 100% duty cycle.

Why are you PWM-modulating all four MOSFETs? You only need to modulate the high side or the low side, not both.

You REALLY need to add logic to prevent both sides of the h-bridge being turned on at the same time. That can happen just due to transients during power-up and down, and WILL blow up the bridge at some point.

I'm a bit surprised anyone would bother building a discrete H-bridge at this point. There are TONS of off-the-shelf H-bridge boards available, that cost less than it would cost you could buy just the components.

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