PWMServo support (PWM at 50Hz)

As I mentioned on the other PWM thread: How can I increase the PWM frequency i.e. at PIN 6? - UNO R4 / UNO R4 WiFi - Arduino Forum

I was wondering if we could go the other way and run at a lower speed. In this case 50 hz. So I thought I would try it.

So I created a new branch in my fork of the library:
PaulStoffregen/PWMServo: Control RC Servo motors with interrupt-resilient PWM (github.com)
Which is currently up at: KurtE/PWMServo at UNOR4 (github.com)
Will later do a Pull request back to the master version.

The code is using the PwmOut objects (pwm.h). I am assuming that some form of this object will continue and is not a transitory implementation detail, that will be removed :crossed_fingers:

Why would one use the PWMServo over the normal Servo library? The PWM Servo library uses PWM, which does not rely on using interrupts. So less likely to have jitters. Downside, uses up more resources, may have less resolution, and more limited choice of pins

So far I have only tested this using the example sketch sweep on WIFI pin 5. Will play a little more on other pins. I am curious about how much difference it would make in running on 32 bit timers versus 16 bit timers.

From the other thread, I believe the full set of PWM pins on the MINIMA is:

Pin ~ RA4M1 Timer same timer
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

On the WiFI, I will update with complete list, but the list on the cheat sheet Arduino UNO R4 WiFi Cheat Sheet | Arduino Documentation is probably reasonably complete. Not sure if it may also have some duplicate pins not marked. By duplicate I mean that they have the same timer as another pin already in the table.
Updated:

WIFI ~ RA4M1 GPT Same Timer
3 ~ P105 GTIOC1A 2
5 ~ P107 GTIOC0A 4
6 ~ P111 GTIOC3A 7
9 ~ P303 GTIOC7B 8
10 ~ P103 GTIOC2A 13
11 ~ P411 GTIOC6A 12
0 P301 GTIOC4B 1
1 P302 GTIOC4A 0
2 P104 GTIOC1B 3
4 P106 GTIOC0B 5
7 P112 GTIOC3B 6
8 P304 GTIOC7A 9
12 P410 GTIOC6B 11
13 P102 GTIOC2B 10
18 P101 GTIOC5A 19
19 P100 GTIOC5B 18

If necessary, will update later.

Other notes:
The 32 bit timers are thos on Timers 0 and 1. For example pin 5 in the WIFI is 32 bits (D5 P107 GTIOC0A). Whereas times 2-7 are 16 bit timers.

Changing the frequency of one of the pins on a timer, updates it for all of the pins on the same timer. i.e. if you change it for pin 5 on the WIFI, it also changes it for Pin 4.

Wish the PwmOut object allowed you to set the pin number, outside of the constructor, such that it could be included into the servo object and not need to do a new of it, during the begin process.

Now to continue playing.

3 Likes

Hey

First of all - thanks for your work =)

I read your posts, coming from the one you mentioned here first.
My goal is to control 2 different groups (24V-LED-stripes & 12V-fans) (with different PWM-frequencies to work fine) with my Arduino R4 Minima. I designed layout with transistors etc, on base of the Cheat Sheet from Arduino. On all 5 pins (3, 5, 6, 9 & 10), they say different Timers - so all good, I thought.

But now you are saying, that these Timers are also used on other pins. I don't know how you got this information for your list, but I might get into problem if thats right.

Am I right, that if I use for example

PwmOut pwm(D10);
pwm.begin(21000.0f, 0); // 21kHz

I'm getting in trouble because you are saying its the same timer that's being used on Pin 13 - so the SPI SCK? I'm using SPI too..

Thanks in advance,
rnddude

There are several ways to get this data. Starting with the Pinouts that you can download from the Arduino site. Like:
ABX00087-full-pinout.pdf (arduino.cc)
If you look in the advanced section, you will see:


Note the last area is the Timer connected to the pin.
Notice the two I highlighted. They are on timer 5 on channels A and B...
So they use same timer.

That should not be an issue. The issue is if you are using both pins for timer operations, such as PWM.

Good luck

1 Like