2

I have read several answers and approaches to controlling an SG-90 servo with a raspberry pi and I have tried the following approaches, with varying but never satisfactory results.

Wiring Setup:

  • Servo + to 5V external power
  • Servo - to external ground
  • External ground connected to R-PI ground
  • R-PI PIN 13 (BCM 27) to servo signal

First approach was to use RPi.GPIO with PIN set to output and GPIO.PWM(13, 50). None of the values used had any effect whatsoever on the servo, no movement at all.

Second approach was to use pi-blaster. I've used the default rates and did some echo "27=0.075" > /etc/pi-blaster and could get the motor to move. However lets say moving from -90 to +90 degrees would not be a continous movement, it was more like 3 intervals where it moved fast, halted a while, moved again until it reached the position. Otherwise it was fine, i.e. reliable movements, no jerking except the very large intervals.

Third approach was with some additional helper. I have used one of my Arduino motor shields that I have used on the arduino to successfully control that same servo and connected the external power to the shield, used pin 9 and ground of the shield to the R-PI and connected the servo to the second servo slot. I tried both pi-blaster and RPi.GPIO but neither of them had any effect on the motor.

What am I missing here? I saw many articles where people were pretty much hooking up an sg-90 to an external PSU (and connect grounds) and directly to the R-PI and then using pythons RPi.GPIO without any issues, but this wont even get the motor to move in my case.

  • You do not need external power. You should be able to supply the modest SG-90 requirements from the Pi. Seehttps://gpiozero.readthedocs.io/en/stable/api_output.html?highlight=servo#servo for a simple example. – Milliways Oct 18 '20 at 23:00
  • You might find my answer to the following servo Q&A helpful. *There is a fully debugged, copy and run Rpi4B demo test python program for toy servos (with an intro to PWM, and how to use Rpi/3/4 PWM pins)*: Run the program in the laptop and use the raspberry gpios, PWM to control servos - rpi se 2019jun06 https://raspberrypi.stackexchange.com/questions/99315/run-the-program-in-the-laptop-and-use-the-raspberry-gpios-pwm-to-control-servos.Happy servoing. Cheers. – tlfong01 Oct 19 '20 at 01:40

1 Answers1

2

Assuming you have correctly connected the servo to power and you have connected the control line to GPIO 27 (pin 13).

https://pinout.xyz/

Also assuming you have a recent Raspbian.

Test the servo with the following commands.

sudo pigpiod # start the pigpio daemon

pigs s 27 1500 # centre servo

pigs s 27 1000 # counterclockwise servo

pigs s 27 2000 # clockwise servo

If the servo does not move you have not made the proper connections and you will need to add clear photos of your connections to your question.

joan
  • 71,024
  • 5
  • 73
  • 106
  • This is actually quite similar to pi-blaster. The movements are really accurate in terms of where they end up but the movement to them is sometimes a bit odd. Sometimes going from 550 to 2250 and back works great and smooth, sometimes it stops at a random point for a fraction of second and then does the rest. Sometimes it does that 2 or 3 times. It feels a bit like a timing issue. – Yanick Salzmann Oct 19 '20 at 03:29
  • You are testing the servo range. You may be stripping the end stops. – joan Oct 19 '20 at 06:31
  • Yea, I tested the servo range to be 550 to 2250, thats the range it can move without hitting the end stops. The same thing is also true when using 1000 and 1900, sometimes it goes smoothly, sometimes there are short jerks at random points during the movement. Sometimes it also reaches the designated position and after very short brake does one single tiny jerk – Yanick Salzmann Oct 19 '20 at 06:34
  • Are you sure you haven't stripped the end-stops? That occasionally jerky movement is characteristic. – joan Oct 19 '20 at 07:09
  • What exactly does "stripped" mean in this context? I assumed hitting them because I try to go past the maximum range, but I think its not that. – Yanick Salzmann Oct 19 '20 at 07:12
  • If the servo has plastic cogs they can break and you can go beyond the end-stops. The servo then becomes erratic. – joan Oct 19 '20 at 07:55
  • ah no, definitely not, if I move it by hand when not connected to power it stops at exactly those positions, so they are not broken. Its not that important, my use case does not ask for large movements, they are slow and small, and that works without issue, so I guess thats fine. – Yanick Salzmann Oct 19 '20 at 07:59
  • what seems a bit strange is the fact that with pi-blaster its similar, but worse, so during almost all movements that jerking happens whereas with your library it happens rather infrequently (less than 50%), that made me think its probably not directly caused by the servo. – Yanick Salzmann Oct 19 '20 at 08:06
  • pi-blaster works in a similar fashion to pigpio (which pigs is using). They both use DMA to generate the timing pulses. You may get problems if you use audio at the same time. – joan Oct 19 '20 at 08:46