Trouble with Servos on R4 Wifi

Hi! Just got my r4 wifi today, and I'm having trouble getting it to work with servos. As best I can tell, the PWM output on the pin is updated much less frequently than it's supposed to be, resulting in a ticking like behavior on the servo.

To eliminate variables right out the door, I am properly powering the servo externally, and have a shared reference ground with the servo power supply and the MCU. As well, even the basic example "Servo Sweep" for me on my R4 wifi exhibits this behavior. Using the exact same code, servo, power, circuit, etc. and only swapping out between an R4 wifi and an uno R3 for the PWM generation, I see the correct movement on the servo with an R3 and incorrect with an R4.

I used pulseIn on another MCU to check the actual PWM output coming from the R4 wifi with the example sweep sketch, and confirmed it's not ouputting the expected pwm values. (I'll attach a video if I can showing this). Any ideas?

(I'm also having intermittent trouble even uploading to the r4 wifi from the latest Arduino IDE 2.1.1, but that feels unrelated, but putting all data here I can)

Looks like I can't upload videos, so here's self hosted: https://s3.us-west-1.amazonaws.com/bottango.com/TestBuilds/offset.mov
On the left, I'm reading the pwm input directly from the r4 on the r3, using pulseIn() with a 10ms delay so it's a little more readable between writes.

On the right, that's the r4 doing a sweep (I switched to writeMicroseconds in case that changed anything... it didn't) and printing it's target

You can see on the right, it's setting the pwm as expected on the r4, at least as far as outputting the log to serial is concerned. On the left, the r3 is really only reading increments of 100 change in the pwm value.

Here's what that looks like on the servo itself: https://s3.us-west-1.amazonaws.com/bottango.com/TestBuilds/IMG_5363.mov

Hi @evanbottango. The Arduino IDE developers are tracking this problem here:

If you have a GitHub account, you can subscribe to that issue to get notifications of any new developments related to this subject.

1 Like

Thanks for the update, glad to know it’s not user error. I’ll keep an eye out for a solve, much appreciated! Excited to see what I can do once that’s fixed :slight_smile:

Edit: @KurtE 's PR (#116) does work! Is there any reason why it's not yet merged?

Hi, I am still having the issue described above.

Servo library is version 1.2.1:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 1000;    // variable to store the servo position

void setup() {
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}

void loop() {
  for (pos = 1000; pos<2000; pos++)
  {
    myservo.writeMicroseconds(pos);
    Serial.println(pos);
    delay(10);
  }
  for (pos = 2000; pos>1000; pos--)
  {
    myservo.writeMicroseconds(pos);
    Serial.println(pos);
    delay(10);
  }
}

I notice on an oscilloscope that the duty cycle only changes every 100 µs or so when running on an UNO R4 Wifi.
The same code delivers a very smooth sweep on an UNO R3.

My understanding is the lib update should solve this? What am I doing wrong?

Thanks!

Sorry been awhile. It looks like the reviewer still has some edits he wants...

Note: my UNO R4 boards are currently in box.... Servos back in storage...

Tacking on to this thread - I tried using a servo with the R4 minima today and immediately encountered extreme servo jitter that I wasn't experiencing with the same circuit and code on an R3. Googled it and immediately found this thread and a bunch of others, so I didn't bother breaking out the oscilloscope. I notice that the Github thread linked by @ptillisch above was marked as resolved by the arduino-libraries user on Feb 25th. I was still having the issue with version 1.2.1 of the servo library. I followed steps from another forum thread (which I have since lost because I rebooted and lost browser tabs) to manually install the servo library from Github using a .zip file - it still shows as version 1.2.1, but the jitter is gone.

So...looks like there's a fix but it's not part of the latest IDE/automatic updates yet? Can anyone clarify?

Edit - see reply from Eliot in this thread 6 days ago, that's what I did: Arduino Uno R4 Servo PWM changing in 100µs increments - #9 by Reef_Engineer - Thrusters and Motors - Blue Robotics Community Forums

That is correct. Arduino IDE only provides release versions of the libraries. The bug has been fixed (thanks @KurtE!), but there hasn't been a new release of the library since the time of the fix, so the fix is only available when using the development version of the library, which must be installed manually via the procedure described at the thread you linked.

Thanks @ptillisch ! Is there any way to get an estimate of when the fix will be officially released - even a very rough timeline, e.g. "sometime in the next week" vs "later this year"? I work for a STEM education nonprofit and we really try to minimize barriers to entry for our teachers/students (e.g. just keep it to installing the IDE and not manually installing libraries). A lot of our projects use servos so we are debating holding off on switching to the R4 until this gets resolved.

Unfortunately I don't have that information. I'm not involved in managing the releases of this library.

You can request a new release by submitting an issue to the GitHub repository:

That will allow the people who manage the project to effectively track the task of creating a new release.

Update for other people finding this - as of May 15th, 2024 it looks like the library is still on version 1.2.1. I submitted a GitHub issue here, will also update this thread if I get a reply on GitHub: Release update that fixes servo jitter issue on R4 · Issue #128 · arduino-libraries/Servo · GitHub

2 Likes

Thank you for following up on this @bfinio
That Arduino specifically called out Servo in the UNO R4 WiFi Suggested Libraries without ensuring it actually worked correctly has been frustrating from the start

I totally agree with you that there are other, probably better solutions. For example, with Teensy boards, we also have another library PWMServo, that does more or less what you are mentioning.

But I also believe that the easier Arduino makes it for people to move their code, to a new board such as this, the more likely they will, conversely the more things one has to change in their code, the more likely they will move on to a different board...

Likewise, the harder it is to get contributed potential fixes to issues reviewed and released, the less likely the contributors will try to resolve other issues.

2 Likes

I read this as "provide a simple upgrade path".

Preventing people from re-inventing the wheel and focus on the glue logic.

For me a library ist a tool. I don't necessarily need to know how a tool is made or what its inner workings are.

Did you am actually implement the font rendering on your PC by yourself or did you stop at "I can read the text on my Monitor"? (For me its the second...)

Why not both? Add a path for the R4 thats uses the GPTs?

1 Like

Thanks @Rintin

What does not work? AFAIK the changes I made to the Servo library fixed the issues that at least some of the users were having. Is it the greatest thing NOPE. Is it a workable tool in a toolbox... Hopefully.

I really wish someone would port or implement something like the Teensy PWMServo library! Note: I am unlikely to do so as I have not used analog servos for a long time now. If I am doing anything with servos, I end up using one of the smarter ones such as Dynamixels.

Even if PWMServo was implemented, would it remove the usefulness of the simple Servo library? Probably not. Why? Maybe there are not enough PWM pins available for my project. I have seen Teensy projects for example where both libraries are used... Likewise, compatibility, the servo library is supported on several different boards...

Sorry, but for now my R4s are in a box stuffed into a cabinet. Maybe I will pull them out again at some point...

Yep - notice this thread started back in July of last year...
I had a fix for it last September,
There were things the Author wanted changed... Variable names, comments, etc...
Was then merged in January, but a new version of the library has not been released.

One main issue that Arduino has, is the name UNO R4, as R1, R2 and R3 probably could run the same code. So one might assume that it will run on the R4... (Been mentioned before ...)

Sorry I just have been frustrated with how many basic things, I would have expected to work, by the time the board was released. And when things don't work as expected, how many times I would read comments on how should not do that anyway... Even simple things like I do a Serial.print from an ISR handler and the board hangs.... I don't think it is helpful to say that is bad practice, when it was simple enough to make work... Simply raise the priority (Lower number)of that interrupt... Serial port writes not buffered, issues we had earlier with SPI and Wire... Don't know if all of them were resolved... Sorry enough wining.

I am in total agreement that someone should create a library that can do this for some of these pins. I am also my knowledge on these boards is a bit hazy. But if I remember correctly not all of the timers on these boards support PWM. Could be wrong. Like timers 6 and 7?

Also I don't remember how configurable these timers are. For example, if you setup the frequency for these timers to 50hz, and if your timings for the servos go from 500us to 2500us, how many timer counts there are between those two. I know with some setups you end up with less granularity. But it has been awhile.

And years (over decade) ago when I was playing with analog Hitec servos using Arduino, most of the time I used a modified version of Servo library which I called ServoEx...
Where on my Hexapod with 18 servos, with each basic sequence of the walking gait, you tell all 18 servos to move to a new location and make it there in lets say 150ms. The code then calculated how much each servo should move for each servo period, such that all of the servos get to their end points at about the same time.... But that is a different story.

Thanks, was looking at the wrong document... :blush: That was the GIGA document, with the differences.

1 Like

I pulled out a Minima this morning for a question about Serial objects, and thought I would play for a few minutes on the servo stuff...

I did not create a library for this, but, did a quick and dirty sketch which should sweep a servo on pin 5 from 500-2500us in about 2 seconds and then back in another 2 seconds:

#include <UNOR4_digitalWriteFast.h>

#include <pwm.h>
PwmOut pwm5(5);

void setup() {
  Serial.begin(115200);
  Serial.print("Quick and dirty PWM Servo Test");

  pwm5.begin(1000000 / 50, 1500, false);
  pinMode(6, OUTPUT);


  FspTimer *ptimer = pwm5.get_timer();
}

void loop() {
  // lets play with a logical sweep where it takes something like 2 seconds to go 500-2500 and likewise back
  for (uint32_t pw = 500; pw <= 2500; pw += 40) {
    pwm5.pulseWidth_us(pw);
    digitalToggleFast(6);
    delay(20);  // 2000 increments
  }
  delay(100);
  for (uint32_t pw = 2500; pw >= 500; pw -= 40) {
    pwm5.pulseWidth_us(pw);
    digitalToggleFast(6);
    delay(20);  // 2000 increments
  }
  delay(100);

}

I have it updating every 20ms as most servos are setup for 50hz PWM signals... Some can go faster. I toggle a pin each time I change the pulse width, using my own digitalToggleFast.
Would be simple to convert to digitalWrite... but ...

It looks like it is working some:


At one end of a sweep:

The other end:

Now to put the Minima back in it's box...

Edit: Note I did not try running this on a real servo, as I did not want to try to dig one up, plus setup some power for it...

1 Like

Thank you @Delta_G that code solved my problem of getting large jumps in my pwm output. I am using a UNO R4 WiFi with two actuonix L16-140-150-6-R linear servo's on pins 3 and 5 .

Here is a link to my thread

2 Likes

FYI it looks that v1.2.2 released last week contains merge from @KurtE .
Now It can be solved by updating from Arduino IDE.
Thanks. Release Release 1.2.2 · arduino-libraries/Servo · GitHub

3 Likes