My Investigation into Servo Twitches & Glitches

Flenser

Senior Member
In the thread Unique Servo Control Requirement rob43 reported that on an 08M2 at the default 4MHz with the servo output on pin C.4 when servos were held at a stationary position they were "twitching" every 1-2s.

This prompted me to investigate servo twitching and this thread is to describe what I found and give the results of the testing I did.

I am only documenting the twitching that occurs continuously while the servo is in a fixed position. I have ignored the twitches that can occur when a new program is downloaded.

Here is a video of the showing the most comment type of twitching I've observed. This example occurs with a period of about 1s but the twitches can occur once per 1s, once per 2s, once per 4s, once per 8s and about 5 times per 1s.
⦁ Note: This means that if you are moving the servo by changing its position more frequenty than the glitch occurs then you may not get any twitching. I did not try to test this proposition.

Twitching Example.gif

These are screenshots from the servo signal captured using DPScope:
This screenshot above shows the normal servo pulse
GlitchCapture18M2_B.6_A 3s,D 6s@4MHz#3_#0.PNG

This screenshot shows one of the glitches I captured
GlitchCapture18M2_B.6_A 3s,D 6s@4MHz#3_#1@08sDoubleGlitch#1.PNG

This screenshot shows a second type of glitch that that was captured.
GlitchCapture18M2_B.6_A 3s,D 6s@4MHz#3_#1@23sDoubleGlitch#2.PNG

These may not be all the possible glitches because we are not capturing all the servo pulses with DPScope.
⦁ The servo pulses are happening at 50 times per second.
⦁ DPScope is refreshing the screen at 40 times per second, so is sampling the servo pulses.
⦁ The Windows video capture is at 30 frames per second, so is sampling the DPScope screen.

Summary
⦁ With the sole exception of the 28X2, I have observed the servo twitching on all the other X2 & M2 chips I own, 08M2, 14M2, 18M2 and 20X2. All my M2 & X2 chips have the current firmware version listed on picaxe.com.
⦁ On the chips where twitching does occur, the combination of some pins, some SERVO settings & some CPU frequencies twitch while others do not.
⦁ On the 28X2 I did some more detailed testing and still did not see any twitching
⦁ Servos have different sensitivities to glitches in the signal. I test with a very old Futaba FP-S148 standard sized analog servo and a current Emax ES 9051 micro digital servo and on some tests where my analog servo was twitching I could not detect my digital servo twitching. This occurred in rob43's original thread where his servo had a distinct twitch at 4MHz that I was not able to reproduce using the same code, pin & CPU frequency but I was able to reproduce it at 16MHz and I attribute this to my servos having a different sensitivity to rob43's.
My results only apply to the servos I have tested. You cannot assume my results will apply to your servos. You will have to test them to find out for yourself.
My results also only apply to the very simple test programs I have used. When I repeated my tests with different programs I sometimes got different results, so you should not assume my results will apply to your program, even if you are using one of the servos that I tested with.
I did not test on a 20M2 or 40X2 chip. If you are using one of these chips then you will have to test them to find out for yourself. However, I found this post on another forum discussing servo twitches using the 40X2 chip https://community.robotshop.com/forum/t/picaxe-40x2-servo-control/9173/2
I've only tested at limited set of servo positions, 100, 110, 120, etc, so I don't know whether there will be glitches between these tests at 111, 112, 113, etc

I plan to do some more posts in this thread:
⦁ This first post is the overview with my summary.
The second post will give the results of my testing on all the chips I own.
The third post will give the results of my testing showing that changing the main program can change the glitching behaviour when using both SERVO and SERVOPOS
The fourth post will discuss possible workarounds when using SERVO+SERVOPOS and provide links to the alternative techniques for driving servos using the PULSOUT+PAUSEUS, PULSOUT+SANDWICH TIMER or PWMOUT+PWMDUTY commands.
The fifth post describes a fix for the servo kick that happens when power is first applied to the servo.


EDITED: Added the PULSOUT+SANDWICH TIMER technique
EDITED: Updated the description for my fifth post.
 
Last edited:
Testing on my M2 chips

I test by seeing if I could see or feel the servo twitching and "No" in my results simply means either that I did not detect a twitch or if I did think I detected a twitch that I was not able to reproduce it.

My first test program continually updates the servo to the position 100 in the main loop using the SERVO command and I test every SERVO pin.
Code:
SETFREQ M4
main:
    SERVO C.4, 100
GOTO main

08M2 v4.A C.1 C.2 C.4
M4 Analog Servo YES No YES
M4 Digital Servo YES No YES
M16 Analolg Servo No No No
M16 Digital Servo No No YES

14M2 v6.A B.1 B.2 B.3 B.4 B.5
M4 Analog Servo No YES No YES No
M4 Digital Servo No YES No No No
M16 Analog Servo No No No No No
M16 Digital Servo No No No No No

18M2+ vD.A B.0 B.1 B.2 B.3 B.4 B.5 B.6 B.7
M4 Analog Servo YES YES YES No YES No No No
M4 Digital Servo YES YES YES No YES No No No
M16 Analog Servo YES No YES No No No No No
M16 Digital Servo YES No YES No No No No No

To get a better idea of the scope of the twtiching I did more testing with different test programs and for these tests:
⦁ I tests the SERVO settings 100, 110, 120, etc, through to 200
⦁ I only test on pin C.4 of the 08M2 chip.
⦁ I only use the old standard size analog servo.
⦁ I test at both CPU frequencies M4 & M16

This test uses the same program as above:
Code:
SETFREQ M4
main:
    SERVO C.4, 100
GOTO main

08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo YES YES No No YES No No, YES No No No
M16 Analog Servo No, YES No YES YES YES YES No No No YES

This test is intended to set the servo postion once and then see what the effect of a minimum main loop that just executes a goto main
Code:
SETFREQ M4
SERVO C.4, 100
main:
GOTO main

08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No No No No No No No No, No No No
M16 Analog Servo YES YES YES No No No No YES No No YES

This test is using RevEd's recommendation of intialiing the firmware servo operation once using the SERVO command then then using the SERVOPOS command to alter the servo position. In this case I am deliberatly altering the servo to the same setting in the main loop.
Code:
SETFREQ M4
SERVO C.4, 100
main:
    SERVOPOS C.4, 100
GOTO main

08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No, YES No No No YES No, No No, No, No
M16 Analog Servo No No, YES YES YES YES YES No YES No, No





Testing on my X2 chips
This first test program, that I used for the M2 chips, did not work on the 20X2 chip. The servo did not move to the new position and the servo did not hold it's position. i.e. I could move the servo with the only resistance being the friction in the servo's geartrain.
There was no servo pulse signal output on pin B.0. <test this>
Code:
SETFREQ M8
main:
    SERVO B.0, 100
GOTO main

So for the X2 chips I switched to using RevEd's recommendation of intialiing the firmware servo operation once using the SERVO command then then using the SERVOPOS command to alter the servo position.
Code:
SETFREQ M8
SERVO B.0, 100
main:
    SERVOPOS B.0, 100
GOTO main

20X2 vC.3 B.0 B.1 B.2 B.3 B.4 B.5 B.6 B.7
M8 Analog Servo No No No No YES YES No No
M8 Digital Servo No No No No YES YES No No
M32 Analog Servo No YES No No No No No No
M32 Digital Servo No No No No No No No No

28X2 vB.3 B.0 B.1 B.2 B.3 B.4 B.5 B.6 B.7
M8 Analog Servo No No No No No No No No
M8 Digital Servo No No No No No No No No


For the 28X2 I did the following more detailed tests using the same program which also discovered no twitching at all:
⦁ I tests the SERVO settings 100, 110, 120, etc, through to 200
⦁ I only test on all pins.
⦁ I only use the old standard size analog servo.

Code:
SETFREQ M8
SERVO B.0, 100
main:
    SERVOPOS B.0, 100
GOTO main

28X2 vB.3 B.0 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No

28X2 vB.3 B.1 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No

28X2 vB.3 B.2 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No

28X2 vB.3 B.3 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No

28X2 vB.3 B.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No

28X2 vB.3 B.5 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No

28X2 vB.3 B.7 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No
 
Last edited:
The results of my testing showing that changing the main program can change the glitching behaviour when using both SERVO and SERVOPOS.

On the 08M2 chip


Adding a PAUSE 1 immediately after the SERVO command in this test program:
Code:
SETFREQ M4
main:
    SERVO C.4, 100
    PAUSE 1
GOTO main

Gives these results, which are different from the same test above:
08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No No No No No No No YES No, No, No
M16 Analog Servo No No No No No No No No YES No No

Adding a PAUSE 1 immediately after the SERVOPOS command in this test program:
Code:
SETFREQ M4
SERVO C.4, 100
main:
    SERVOPOS C.4, 100
    PAUSE 1
GOTO main

Gives these results, which are different from the same test above:
08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo YES YES YES No No YES YES YES YES YES No
M16 Analog Servo No No No No No No YES No YES No, No

Adding a TOGGLE C.1 immediately after the SERVO command in this test program:
Code:
SETFREQ M4
main:
    SERVO C.4, 100
    TOGGLE C.1
GOTO main

Gives these results, which are different from the same test above:
08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo YES YES YES YES YES YES YES YES YES No No
M16 Analog Servo YES YES No YES No YES YES YES YES No YES

Adding a TOGGLE C.1 immediately after the SERVOPOS command in this test program:
Code:
SETFREQ M4
SERVO C.4, 100
main:
    SERVOPOS C.4, 100
    TOGGLE C.1
GOTO main

Gives these results, which are different from the same test above:
08M2 v4.A C.4 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No No YES YES YES No No No No YES YES
M16 Analog Servo No No No No No No No No No No No


On the 28X2 chip

Adding a TOGGLE C.0 immediately after the SERVOPOS command in this test program:
Code:
SETFREQ M8
SERVO B.1, 100
main:
    SERVOPOS B.1, 100
    TOGGLE C.0
GOTO main

Has no effect, which are identical to the results for the same test above:
28X2 vB.3 B.0 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M8 Analog Servo No, No, No, No, No, No, No, No, No, No, No
M8 Digital Servo No, No, No, No, No, No, No, No, No, No, No
 
Last edited:
The workarounds I can think to try to see if they work:
If the servo in your project is twitching when in a stationary position there are several things that you can try before abandoning the SERVO+SERVOPOS commands:
⦁ Try different combinations of pin + CPU frequency on the chip.
⦁ Try different settings for the servo positions that avoid the range where the twtiching occurs. e.g. instead of 100 & 150 try 114 & 164, etc.
⦁ Try adding a DISCONNECT or DISABLEBOD command.
⦁ If your servo only needs to hold a light load when stationary then try SERVOPOS OFF once you get to that position to see if the friction in the servo's geartrain is enough to hold it's position.
⦁ Try a different servo. If you are using analog servo then you could try a digital servo as these seem to be less sensitive to glitches in the servo signal.
⦁ Try a different PICAXE chip.

The workarounds "combinations of pin + CPU freq", "different settings for the servo positions" and "adding DISCONNECT or DISABLEBOD" are all subject to the disclaimers for my test results.
⦁ e.g. if a workaround works and you later alter your program then you could find you have to redo the work to find a workaround.

These disclaimers work both ways.
My test results below show that adding a DISCONNECT or DISABLEBOD command did not work however this only demonstrates that they are not guaranteed to be a fix.
⦁ Adding these commands alters your program so it is equally possible that they could solve the twitching in some cases.


Alternative Techniques For Generating Servo Pulses
If you unable to find settings for SERVO+SERVOPOS that don't twitch then you will need to try an alternative technique to generate the servo pulses, like PULSOUT+PAUSEUS, PULSOUT+SANDWICH TIMER, using PWMOUT & PWMDUTY to generate the pulses using the internal pwm module, or using an I2C Servo driver board.


Links for using PULSOUT+PAUSEUS
⦁ There is example code in both the online and pdf documentation for the PULSOUT command with the online documentation giving a second example for driving multiple servos.
⦁ Using PULSOUT & PAUSEUS described by Hippy in this post

and this is the general approach
Code:
main:
   ... your other program code ...
   pulsout B.1, 150         ; send pulse out of pin B.1
   pause NNN                ; Adjust the main loop to ~20ms
goto main

Hippy's example code adjusts the NNN value so that the pulse frequency is close to 20Hz however this Princeton University page makes the following comments about driving R/C hobby servos :
⦁ If you repeat the control signals too quickly (i.e. 10ms delay) the servo will buzz and jitter. If you repeat the control signals too slow (i.e. 70ms delay) the servo will shut off between signals and its position will not remain constant


Links for using PULSEOUT + SANDWICH TIMER
See Post #11


Links for using PWMOUT & PWMDUTY
Using PWMOUT & PWMDUTY is described by mikeBoo in this post.
In this post Hippy's last comment warns about the need to be careful about commands that cause the firmware to briefly change its operating frequency, which can disrupt the PWM signal.


Workarounds that I tried which do not work.
A search of the forum posts found DISABLETIME, DISABLEBOD or DISCONNECT as possible workarounds.

I did not test DISABLETIME.
This disables the internal timer used by other commands, including SERVO, and so while it will stop the servo pulses I recommend using SERVOPOS STOP as a better way because:
⦁ It does not effect any other command.
⦁ As a more explicit way to stop the servo pulses it is self-documenting as to what you intend in your program.

The DISABLEBOD workaround did not work.

This test program:
Code:
SETFREQ M4
DISABLEBOD
SERVO C.1, 100
main:
    SERVO C.1, 100
GOTO main

Gave these results:
08M2 v4.A C.1 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo YES YES No YES YES YES YES No, No, No, YES
M4 Digital Servo YES No No YES YES YES YES No, No, No, YES
M16 Analog Servo No No No YES YES No No, No, YES YES YES
M16 Digital Servo No No No YES YES No No, No, YES YES YES

This test program:
Code:
SETFREQ M4
DISABLEBOD
SERVO C.1, 100
main:
    SERVOPOS C.1, 100
GOTO main

Gave these results:
08M2 v4.A C.1 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No, No No No, No, YES No No, No, YES No
M4 Digital Servo No, No No No, No YES No No No, YES No
M16 Analog Servo YES No No YES No No, No No YES No, YES
M16 Digital Servo No, No No No No No, No No No, No, No


The DISCONNECT workaround did not work.

This test program:
Code:
SETFREQ M4
DISCONNECT
SERVO C.1, 100
main:
GOTO main

Gave these results:
08M2 v4.A C.1 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No, No No No No YES YES YES No YES No
M4 Digital Servo No, No No No No YES, YES YES No YES No
M16 Analog Servo YES YES No No No No No, No, No YES YES
M16 Digital Servo YES YES No No No No No, No, No YES No

This test program:
Code:
SETFREQ M4
DISCONNECT
SERVO C.1, 100
main:
    SERVO C.1, 100
GOTO main

Gave these results:
08M2 v4.A C.1 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo No, No, No, No No, No No No No No No
M4 Digital Servo No, No, No, No No, No No No No No No
M16 Analog Servo YES YES YES No YES No YES YES YES YES No
M16 Digital Servo YES YES YES No No, No YES YES YES No No

This test program:
Code:
SETFREQ M4
DISCONNECT
SERVO C.1, 100
main:
    SERVOPOS C.1, 100
GOTO main

Gave these results:
08M2 v4.A C.1 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200
M4 Analog Servo YES No, No YES YES YES YES No No YES YES
M4 Digital Servo YES No, No YES YES YES YES No No YES YES
M16 Analog Servo No YES YES No YES YES No No No No No
M16 Digital Servo No No YES No No, YES No No No, No No


EDITED: Added the PULSOUT+SANDWICH TIMER technique
 
Last edited:
A FIX for the servo kick that happens when power is first applied to the servo.

I found the following description of a fix for the kick when power is first applied to a servo in Chapter 6 - Point motors and servos of the Model Electronic Railway Group's ebook "Electronics for Model Railways"

Kicking
It is well-known that most servos produce a 'kick' (an unwanted movement) when they are first powered up. This is a feature of their internal electronics and is cleared in almost all cases by soldering a 10k resistor between the servo's signal wire and the +5V supply line.​
In some cases, it only effective if wired across the signal line and the 0V line.​

This fix worked for me
Putting a 10k resistor between the servo's signal and power pins completely fixed the kick, that I show in the video below, when power is first applied to my Futaba FP-S148 standard sized analog servo.
Putting a 10k resistor between the signal and ground pins had no effect. The servo still kicked.

I've left the rest of this post below in place for the demonstration of this kick when power is applied without the fix.
After AllyCat posted the code that fixed rob43's first problem (that when servos were held at a stationary position they were "twitching" every 1-2s) rob43 reported the second problem "When I turn on the power (battery pack OR BT-PSU) it kicks a little to the left (CCW)." and "Now it has that CCW kick when I cycle the power, and it kicks to the left each time I cycle the power."

I was able to reproduce exactly the same behaviour from a very old Futaba FP-S148 standard sized analog servo but no twitching at all from an Emax ES 9051 micro digital servo.

Attached is a video showing my Futaba FP-S148 analog servo twitching each time I cycle the power off and back on. The servo's signal line connected to ground and there is no PICAXE chip.
- I have to leave the servo for 5-10 seconds after disconnecting power before I can repeat the twitch. which I assume is due to the time it takes the internal capacitance to dissipate, so in the video I short the servo power to the servo ground to drain the capacitors between each time I re-apply power to show the servo twitch.
Servo-power-twitch.gif

I did a web search for "servo twitches when power is first turned on" and discovered:
- It seems to be a pretty common problem. There are a lot of hits.
- There are many hits in both the Arduino and Model Radio Control forums, so it is an issue people strike trying to drive servos in general and not something specific to the PICAXE.
- it sounds like there is an element of luck involved in whether or not someone strikes this problem.

neiltechspec posted his experience:
- "Having used hobby servos in R/C models for decades I'm of the opinion that there is no way to totally eliminate servo 'twitching' at intial power-up, it's just the nature of basic servo design. Some will twitch more then others but it's totally normal to see them move a little (or a lot depending)."
- "The most common thing with servos is that they twitch when power is applied. Some are much worse than others. The GWS servos I have are especially bad."
- "I tried a couple of my Emax ES08MD, they don’t twitch on power cycles either."

Based on this rob43 purchased two digital servos to test.
- The first one he tested was :the GH-S37D digital servos. that he got from an eBay vendor. This did not solve this second problem.
- The second one he tested was:the Turnigy TGY-0025 and this fixed his problem.

From this it looks like if a power on twitch is a problem for your project then:
- You will have to be prepared to test your servo.
- Buying a servo described as "digital" is no guarantee that it wont's suffer from this issue.

UPDATED: Added the fix from the Model Electronic Railway Group website.
 
Last edited:
Have you tried the DISCONNECT command ?
Ooops, I'd forgotten about including the results for the workarounds I tested that did not work, so thanks for bringing this up.

EDITED: Yes I did test DISCONNECT and it isn't guaranteed to work. I'll update my workaround post to include the test results for this.
 
Last edited:
It's been a few years, but I had 6 servos working glitch-free in this arm project. Nothing special hardware or software-wise.

 
It's been a few years, but I had 6 servos working glitch-free in this arm project. Nothing special hardware or software-wise.
Erco,

I expected to do some tests, identify a pattern and write a post along the lines of "When do servos twitch using the SERVO command" but it did not turn out this simple. The more testing I did the more detailed the testing seemed to need to be in order for the results to be definitive.

So I would now describe my post as answering the question "Is it possible that servos could twitch in your program on the chip you are using" with the answer being:
⦁ For the 08M2, 14M2, 18M2 and 20X2 chips: Yes, it is possible.
⦁ For the 20X2 chip: I did not find twitching in any of the tests I performed. So the answer might be No but a No is subject to the disclaimers I list below.
⦁ For the 20M2 and 40X2 chips: Unknown as these were not tested.

and my post comes with these disclaimers:
⦁ My results only apply to the servos I have tested.
⦁ My results also only apply to the very simple test programs I have used.
⦁ I did not test on a 20M2 or 40X2 chip
⦁ I've only tested at limited set of servo positions, 100, 110, 120, etc through to 200

This all means that when people use different servos OR a different program from the simple ones I used OR on the 20M2 or 40X2 chips then, as at least one of those first three conditions are already different from what I tested with, then it is possible that at any servo position they could get different results than I list in my post.

The difference between your experience, rob43's experience and my experience trying to reproduce rob43's twitching seem to provide a good example of how difficult it would be to ever state generically that "servos won't twitch" or "servos will twitch".
⦁ rob43 had a relatively simple program driving one servo that did twitch.
⦁ Using the rob43's program on pin C.4 of an 08M2 chip with my servos I could not reproduce the twitching rob43 saw at 4MHz but when running the same program at 16MHz I did see twitching that matched rob43's description.
⦁ You had a more complicated program driving 6 servos that did not twitch.

Just three examples give three distinctly different "did the servo twitch" results.
 
Links for using PULSEOUT + SANDWICH TIMER
The PULSEOUT + SANDWICH TIMER technique is a similar approach to the PULSOUT+PAUSEUSE technique, just with the PAUSEUS(s) replaced with the SANDWICH TIMER. Using the sandwich timer means that you do not have to hand tune one or more PAUSEUScommands in your main program so that the servo pulses will be sent about every 20ms, The operation of the sandwich timer taskes care of this for you.

Example code usint PULSEOUT+SANDWICH TIMER to drive a servo is given by Hippy in this post.

Sandwich timers are something that was developed at University of Leicester to get accurate task execution times for compiled C programs an "8-bit or 16/32-bit microcontroller with very limited memory and CPU performance".
With help from Hippy, I ported that sandwich timer pattern to the PICAXE chips.
For more details about how they work see these two posts:
Sandwich Delays. A technique to improve the scheduling of timed tasks in super loops on X2 chips.
Announce: Using Sandwich Delays to schedule timed tasks in super loops on X2 chips
 
Last edited:
Back
Top