Ben's Blog

electronics, I.T. ben December 14, 2019

Raspberry Pi Servo Jitter

Here’s the final solution I came up with to finally get a servo motor to behave on a Pi. It may not seem like much but it took a lot of doing to gather all the right bits. This was tested with an SG90, SG92R, and an MG90S on a Pi Zero.

Scroll straight to the end for the solution.

The most common way for controlling a servo motor on a Pi with is through RPi.GPIO as such:

#!/usr/bin/python3
import RPi.GPIO as GPIO
import time

servo = 23

GPIO.setmode( GPIO.BCM )
GPIO.setup( servo, GPIO.OUT )

# info on frequency and PWM formula at https://rpi.science.uoit.ca/lab/servo/
pwm = GPIO.PWM( servo, 50 )
pwm.start( 2.5 )

print( "0 deg" )
pwm.ChangeDutyCycle( 2.5 )  # turn towards 0 degree
time.sleep( 3 )

print( "90 deg" )
pwm.ChangeDutyCycle( 7.5 )  # turn towards 90 degree
time.sleep( 3 )

print( "180 deg" )
pwm.ChangeDutyCycle( 12.5 ) # turn towards 180 degree
time.sleep( 3 )

pwm.stop()
GPIO.cleanup()

Stuff you might need for this to run:

sudo apt-get update && sudo apt-get install python3-rpi.gpio

It results in super jitter which is unacceptable for the holy mission of pen plotting.

As far as I understand, the jitter comes from the wave form RPi.GPIO produces for Pulse Width Modulation, which is made in software and so it’s not super stable (no dedicated resources to build it). From what I gather, pigpio is programmed to tap into the one hardware PWM that Pis have.

The solution thus is as such:

#!/usr/bin/python3
import RPi.GPIO as GPIO
import pigpio
import time

servo = 23

# more info at http://abyz.me.uk/rpi/pigpio/python.html#set_servo_pulsewidth

pwm = pigpio.pi()
pwm.set_mode(servo, pigpio.OUTPUT)

pwm.set_PWM_frequency( servo, 50 )

print( "0 deg" )
pwm.set_servo_pulsewidth( servo, 500 ) ;
time.sleep( 3 )

print( "90 deg" )
pwm.set_servo_pulsewidth( servo, 1500 ) ;
time.sleep( 3 )

print( "180 deg" )
pwm.set_servo_pulsewidth( servo, 2500 ) ;
time.sleep( 3 )

# turning off servo
pwm.set_PWM_dutycycle( servo, 0 )
pwm.set_PWM_frequency( servo, 0 )

Stuff you might need for this to run:

sudo apt-get update && sudo apt-get install python3-pigpio
sudo pigpiod

And the resulting super smooth motion and holds:

Recent Comments

  1. Cassio says:
    July 6, 2020 at 7:17 am

    it works!!
    thank you very much!

    Reply
    • ben says:
      July 7, 2020 at 1:41 am

      Thank you for dropping a note 🙂 it’s always nice to know these posts help others. Take care.

      Reply
  2. y says:
    September 24, 2020 at 11:25 pm

    mad my life so much easier.
    thanks!

    Reply
  3. Jhonny B says:
    October 24, 2020 at 10:17 pm

    Thanks a lot ^^

    Reply
  4. Jim A says:
    December 15, 2020 at 3:15 pm

    Thanks. I tested on my RPi3B+ and SG90 servos. PIGPIO is a much better solution than just turning the servo off after each positioning.

    Reply
  5. cipri says:
    February 21, 2021 at 8:48 pm

    much apreciated.thats the solution

    Reply
  6. Arif Kamal says:
    June 20, 2021 at 6:42 pm

    hi, i tried using PIGPIO on MG995 servo but it doesn’t go the angle like in the coding. it moves more than 360 degree angle even if i decrease the time in sleep. Can you help me?

    Reply
    • ben says:
      June 21, 2021 at 1:03 am

      It’s not the time you want to change, it’s the frequency set on this line: “pwm = GPIO.PWM( servo, 50 )” and the duty cycle set on this line: “pwm = GPIO.PWM( servo, 50 )”

      They’ll be specific to your model, and I’m sure it’s documented in the manual or specs.

      Reply
  7. pseudokool says:
    July 24, 2021 at 6:55 pm

    protip!

    Reply
  8. Sajeewa Pemasinghe says:
    November 1, 2021 at 8:12 pm

    This is just brilliant!! Thank you so much for sharing!! I spent so many hours thinking of all sorts of reasons, that could have resulted in this super jitter. Now everything is so smooth thanks to you!!

    Reply
  9. Matt says:
    December 8, 2021 at 9:37 pm

    brillant!

    Reply
  10. gildo says:
    July 31, 2022 at 5:12 pm

    freaking awesome!!

    Reply
  11. lab rpi says:
    September 22, 2022 at 12:18 pm

    ah briliant! thank you!

    Reply
  12. Jorge O Hernandez says:
    November 16, 2022 at 6:36 pm

    This is awesome, that servo jitter was driving me crazy. Thank you so much for sharing!

    Reply
  13. Totos says:
    July 20, 2023 at 6:39 am

    Thank you a lot. Little note: you can remove ‘import RPi.GPIO as GPIO’ in the pigpio version.

    Reply
  14. whc2818 says:
    November 18, 2023 at 10:28 am

    Thanks a lot. I never though gpio library is software based even raspberry pi supports hardware pwm. I have wrongly blamed the hardware for the shaking problem

    Reply
  15. Sam Malone says:
    December 27, 2023 at 7:17 pm

    The only thing that worked after 3 days of searching and experimenting, appreciate it!

    Reply
  16. Abhi says:
    January 4, 2024 at 6:14 pm

    Thank you for this post! I was just wondering if there are any ways to make these servos rotate in a 360 degree motion? I am trying to unwind a string in a door opening mechanism for a mousetrap, and I was trying to attach the string to the blade of the servo. If this is not possible to make it do a 360 degree motion, would you recommend that I use two servos to increase the amount of string I can unwind?

    Reply
    • ben says:
      January 4, 2024 at 6:34 pm

      I believe not all servos are rated for 360 motion, so you want to make sure you get one of those first. Then it’ll likely be that you need to make sure you hit intermediary steps in your code to describe a full rotation to ensure that the servo rotates and doesn’t just take the shortest path to a destination. I’m not sure what doubling servos would achieve, there are many ways to skin that cat :).

      Reply
    • theborg1of4 says:
      June 21, 2024 at 10:09 pm

      Most servos have mechanical stops to limit their rotation (for example, the HS-322HD I am currently using has around 190 degrees of rotation. You can either buy continuous rotation servos or in some cases hack an existing one; I have cut off the mechanical stop on various Hitec servos, then you can basically have them spin in either direction. Try googling for it (for example https://www.google.com/search?q=continuous+rotation+servo+hack&oq=continuous+rotation+servo+hack&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIKCAEQABiABBiiBDIKCAIQABiABBiiBDIKCAMQABiABBiiBNIBCDU4NjNqMGoxqAIAsAIB&sourceid=chrome&ie=UTF-8), there are tons of examples

      Reply
  17. theborg1of4 says:
    June 21, 2024 at 9:56 pm

    I know it’s been over four years since you posted this, but I wanted to reach out anyway: this is a nice, simple example that answers a lot of questions I had regarding servos and jitter on a Raspi Zero W. And it’s one of the first bits of pigpio code that actually works. Thank you!

    Reply
    • ben says:
      June 22, 2024 at 1:30 am

      Thank you 🙂 I’m glad it still helps.

      Reply
  18. Alex says:
    October 30, 2024 at 2:59 pm

    This saved the day on my halloween project at the last possible second!

    Reply

Leave a Comment

Cancel reply

This blog is solar powered

Interactive

Handwriting Capture
Mandalagaba
IPv6 link-local to MAC converter
IPv6 MAC to link-local converter
Markov Text Generation
Markov Word Generation
Markov Music Generation
Duplogrifier
Flood Fill Algorithms
Homestead Metrics
RGB Playground
Web Games

Categories

  • aesthetics111
    • plots54
    • specular holography6
  • Books3
  • I.T.202
    • 3D modeling / printing21
    • AI6
    • all out geekery36
    • electronics27
    • homestead automation6
    • maniacal paranoia25
    • plotters49
    • unix / linux29
    • video games4
    • web development29
    • web games3
  • Lego / Duplo67
  • life in the U.S.42
  • miscellaneous202
  • nature encounters114
  • old vinyls3
  • organs2
  • self sustainability560
    • agriculture105
    • apiculture38
    • apple20
    • building131
    • canning3
    • crochet6
    • foraging6
    • hunting10
    • maple syrup47
    • poultry39
    • preserving2
    • solar power28
    • water23
    • wood84
  • trip to a new life6
Theme by Bloompixel. Proudly Powered by WordPress