How can I increase the PWM frequency i.e. at PIN 6?

Hallo,
I could use some help!
I wrote program code for Uno R4 Wifi and it actually works perfect. But ...I just need a PWM frequency of about 960 Hz. However, all PWM ports only show up with 460 Hz. How can I serve this Problem?
p.s..
This Problem does not appeare while using an Arduino Uno R2 WiFi. On R2 it runs perfect with 975Hz.

Perhaps 960 Hz rather than KHz?

sorry. You‘re right! 960 Hz. :sweat_smile:

I honestly don't think that this is possible, or maybe it is but not easily

Can you maybe explain us why you need it? So we can help you with it

I was wondering if it could go that high as well,

But a little while ago, I was wondering if there was any official way to be able to control the frequency.

That is on the Teensy boards, we have the API:
void analogWriteFrequency(uint8_t pin, float frequency)

But I don't see that here. What I did find is there is the class: PwmOut
which the analogWrite code makes use of, which appears to have that capability, but I don't see any glue that allows you a clean way to get there. You could probably easily bypass the analogWrite code and use it directly.

Example case I was looking to play with. There is a library PWMServo, which can control RC servos using PWM. For this I wanted to maybe setup for 50 hz.

But then that diversion got diverted to other diversions. :smiley:

For a better understanding of the situation:
The Problem does not appeare while using an Arduino Uno R2 WiFi.
On R2 it runs perfect with 975Hz. Just while using the new Uno R4 WiFi the Pins 5 and 6 only deliver 460Hz.

A Google search says that’s to be expected.

Found this page.
Leo..

1 Like

As a matter of facts, i gotta move my project back to Uno R2 WiFi. That‘s a pity. :worried:
Anyway, thanks a lot to all!

@Wawa - Thank you for the link. What I am curious about, is what does Arduino think about using the PwmOut object? Is this considered an internal private interface, or something that will carry forward?

You can also have fun with side effects. That is if you look at the cheat sheet.
Arduino UNO R4 WiFi Cheat Sheet | Arduino Documentation

You will see that all of the timers used (minus A, B suffix) are unique.
For example pin 5: D5 P107 GTIOC0A

But if you then look at the second table of other possible PWM pins, you see:
Pin 4: D4 P106 GTIOC0B

Looks like same timer, with different pin controlled. From other boards (Teensy 4.x), I was guessing that they would share some common settings, which I confirmed. With at quick and dirty test program:

#include <pwm.h>
PwmOut pwm5(5);
void setup() {
  analogWriteResolution(10);
  pinMode(4, OUTPUT);
  analogWrite(4, 512);
  delay(10);
  pwm5.begin(1026, 0);
  pwm5.pulse_perc(50.0);
  pinMode(LED_BUILTIN, OUTPUT);

  delay(10);

  analogWrite(4, 512);
  delay(10);
  analogWrite(4, 256);
  delay(10);
  analogWrite(4, 768);
  delay(10);
  analogWrite(4, 512);

}

uint8_t loop_count = 0;
void loop() {
  digitalWrite(LED_BUILTIN, (loop_count++ & 1) ? HIGH : LOW);
  delay(500);
}

@hwolf - It is possible to run at around 975 if you want with
The above program logic analyzer output, shows the two pins:

The first part of the trace, was from setting up pin 4 using analogWrite, which then got clobbered, when I started up the PWM on pin 5 using the PwmOut object. But it then worked again using the frequency stuff I setup pin 5 to use.

image

You should be to setup the PwmOut on pin 4, setup the frequency you want, and then do your normal stuff on Pin 5. I would guess that once setup you could use pin 4 for other stuff.

However this code will probably not work directly on the MINIMA as the do boards do not share a common pin mapping to the underlying pins.

The Cheat sheet for the MINIMA does not specify any alternative pins.

From my excel document I see pin 5 is:
5 13 P102 46 KR02 AGTO0 GTOWLO GTIOC2B

And I don't see any pins that have GTIOC2A

So there does not appear to be an alternative to that pin.

Update: Try creating PwmOut on pin 5 Set the settings you want and then use analogWrite directly on that pin...
:
It appears to work fine:

#include <pwm.h>
PwmOut pwm5(5);
void setup() {
  analogWriteResolution(10);
  pwm5.begin(1026, 0);

  pinMode(LED_BUILTIN, OUTPUT);

  analogWrite(5, 512);
  delay(10);
  analogWrite(5, 256);
  delay(10);
  analogWrite(5, 768);
  delay(10);
  analogWrite(5, 512);
}

uint8_t loop_count = 0;
void loop() {
  digitalWrite(LED_BUILTIN, (loop_count++ & 1) ? HIGH : LOW);
  delay(500);
}

image

Update 2: See what other PWM pins there are on MINIMA
Quick update from excel... I have not verified if the pins that don't show the ~ are in the Variant pin definitions.
Edit: looks like they are defined in the pinmux.inc file.

3 ~ P104 GTIOC1B 11
5 ~ P102 GTIOC2B 4
6 ~ P106 GTIOC0B 7
9 ~ P303 GTIOC7B 8
10 ~ P112 GTIOC3B 13
11 ~ P109 GTIOC1A 3
0 P301 GTIOC4B 1
1 P302 GTIOC4A 0
2 P105 GTIOC1A D 11
4 P103 GTIOC2A 5
7 P107 GTIOC0A 6
8 P304 GTIOC7A 9
12 P110 GTIOC1B D 3
13 P111 GTIOC3A 10
18 P101 GTIOC5A 19
19 P100 GTIOC5B 18
20 P500 GTIOC2A D 4

Edit: note the last column tries to give me hints if a timer is used for more than one pin. I am assuming each timer will only run at one frequency.

Hi eKurt,
thanks a lot for your support.