Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Set PWM frequency to 25 kHz

I currently can set four PWM pins to around 31 kHz with the following code:

void setup()
{
 TCCR1B = TCCR1B & B11111000 | B00000001; // Set PWM frequency for D9 & D10:
 pinMode(pwmPin9, OUTPUT); // Sets the pin as output
 pinMode(pwmPin10, OUTPUT); // Sets the pin as output
 TCCR2B = TCCR2B & B11111000 | B00000001; // Set PWM for D3 & D11
 pinMode(pwmPin3, OUTPUT); // Sets the pin as output
 pinMode(pwmPin11, OUTPUT); // Sets the pin as output
}

I found this setup somewhere, but I don't know how I can set these four PWM pins to around 25 kHz instead. How is that possible?

Answer*

Draft saved
Draft discarded
Cancel
10
  • 1
    It might be useful to explain that this method only works for timer1, as the other timers don't have a ICRx register. At most, you can only have one PWM pin per timer, for timers 0 and 2. Commented Jun 24, 2016 at 13:04
  • 1
    @Gerben: Don't all the 16-bit timers have that register? At least on the Mega they do. Commented Jun 24, 2016 at 13:09
  • 1
    Yes, but only timer1 is 16-bit on the ATMega328. The rest are 8-bit. And the OP wants 4 PWM output, and your solution only provides 2. Or am I mistaken? Commented Jun 24, 2016 at 15:12
  • 1
    @Gerben: No, you are right. I just say that requiring ICRx seems to be redundant with requiring the timer to be 16-bits. For the Uno and Mega at least, not sure about other AVR-based Arduinos. The OP understand this only provides 2 PWM channels: see my comment on his question and his answer. Commented Jun 24, 2016 at 15:48
  • 2
    @techniche: 1. Works for me. Maybe you forgot to set COM4C1 in TCCR4A? 2. If that's not the problem, then read How do I ask a good question?, then update your question by including your full source code and clearly stating what you expect the program to do and what it does instead ("I dont see any success" is not considered a valid problem statement). Commented Sep 15, 2017 at 8:43

AltStyle によって変換されたページ (->オリジナル) /