# UNO R4 WIFI - Unexpected servo movements

**URL:** <https://forum.arduino.cc/t/uno-r4-wifi-unexpected-servo-movements/1163179>\
**Category:** UNO R4 WiFi\
**Created:** [August 29, 2023, 12:22pm UTC](https://forum.arduino.cc/t/uno-r4-wifi-unexpected-servo-movements/1163179 "2023-08-29T12:22:18Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![KurtE](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kurte/32/1246572_2.png) [@KurtE](https://forum.arduino.cc/u/KurtE)\
**Post date:** [August 29, 2023, 2:04pm UTC](https://forum.arduino.cc/t/uno-r4-wifi-unexpected-servo-movements/1163179/3 "2023-08-29T14:04:46Z")

</div>

@SN68 - I believe there are several things that are probably impacting this.  
As I mentioned in your other thread about this setup:  
[Arduino R4 WIFI Servo Jitters - UNO R4 / UNO R4 WiFi - Arduino Forum](https://forum.arduino.cc/t/arduino-r4-wifi-servo-jitters/1148768/9)

The Servo library has always been susceptible to jitters when there are other things going on in the  
sketch that produces interrupts. As if one pulse goes through without any interrupts delaying it and the next one is produced and another interrupt is being handled at either the start and/or end of the pulse, it can delay the setting of the IO pin, which can cause the pulse to be longer or shorter than the previous one...

That is why when I used to use RC servos, I would typically use a servo controller. Back then I used to use the Lynxmotion SSC-32. Later to control a small number of servos, I might use the PWMServo library I mentioned in the other thread.

Note: some jitter may also be caused by the servos themself. I know back then some of the cheaper servos could have issues.

> [@ptillisch](#):
>
> It sounds similar to this bug:

Maybe, but I also suspect a couple of other things:

**Timing** of the code: The current released Hardware serial code does not use the Software FIFO queue, as such the code will hang there until the last character to be output, has started to be output.

This will probably impacts both the USB Serial (Serial.print statements), the impact of this might be reduced by using a higher baud rate. You currently have Serial.begin(9600);  
Maybe try Serial.begin(1000000);

Likewise, the Wifi code, uses another Hardware serial port (Serial2) for the processor to communicate with the ESP32. I don't remember what baud rate they communicate with. Nor how much if any gain or loss, they get due to the UART used has a hardware FIFO. The FIFO might help in potentially reducing the number of interrupts they need to process for RX data coming from the ESP32. But that could also add latency issues, to when the main processor sees the data.

Nature of **Servo library** : unless it has changed over recent years and you probably already know the stuff below.

When you tell a servo to go to a position: like Servo2.write(value)  
The servo will try to instantly move the servo to that position, as fast as the motor on the servo will allow it to. Which is also impacted by load and the like on the servo.

The Servo library has no way to know where the servo actually is. That is Servo1.read(), does not return the actual position of the servo, only the last position you asked the servo to go to.

So for example, if you tell the servo that is currently at lets say 90 degrees, to now go to 85 then 80 degrees, the servo is going to jump those 5 degrees to 85 and then jerk again to 80 degrees.

So again, long ago when I used the SSC-32 or when I experimented with Servo library, I had my own version of ServoEx which was setup to act like the SSC-32.

What both of these would do, was instead of saying: Servo1.write(85), you would say  
Servo1.write(85, 200)  
Which would say move from current position to 85 degrees in 200ms (1/5 of a second).  
Instead lets say that is from 1500us to 1400us,

The code knew that the servo library updates the servos 50 times per second. So the move above  
So the 200ms would so the move would take 10 servo cycles, and would adjust the position on each of these cycles: 1500 1490 1480 ... 1400 to give a smoother action.

You can emulate this external from the Servo library, although I found it cleaner to do it within it.

Hope that makes sense.

Good luck  
Kurt

---

_[View the full topic](https://forum.arduino.cc/t/uno-r4-wifi-unexpected-servo-movements/1163179)._
