3

I am unsure of the relationship between duty cycle and angle. I see lots of tutorials online with many different equations to calculate duty cycle for a given angle. I have a 50Hz servo motor that ranges between a 2%-12% duty cycle for 0-180 degrees.

The equation I've seen is (angle/18 + 2). This works but I do not understand why and virtually all resources just give the equation with no explanation where the numbers come from.

Can someone please explain why I am dividing by 18 and add 2 to my duty cycle?

user114449
  • 39
  • 1
  • 2
  • The following posts might help: (1) https://raspberrypi.stackexchange.com/questions/104408/how-to-use-rpi-python-to-read-write-convert-to-from-arduino-ppm-and-pwm-servo-c (2) https://raspberrypi.stackexchange.com/questions/98467/how-can-rpi-move-a-servo-motor-using-a-gpio-pin-in-pwm-mode (3) https://raspberrypi.stackexchange.com/questions/99315/run-the-program-in-the-laptop-and-use-the-raspberry-gpios-pwm-to-control-servos. – tlfong01 Feb 07 '20 at 01:43
  • PS - In case you find the above posts too blurred to read (because of too many down votes :( ), you may try read the following clear version: (4) https://penzu.com/p/772fa292. Happy python programming. Cheers. – tlfong01 Feb 07 '20 at 01:44
  • 1
    I'm sorry, but i have to downvote your post because it is a simple arithmetic question and has abslutely nothing to do wih the RPi .... you are trying to translate a range of 0 to 180 into a range of 2 to 12 ... also can be thought of as a range of 0 to 10 with an offset of 2 ....... the equation that you are having trouble grasping is `(angle/180 × 10) + 2` .... `(0/180×10)+2 = 2` .... `(180/180×10)+2 = 12` .... `(90/180×10)+2 = 7` – jsotola Feb 07 '20 at 02:36
  • Ah let me see. Me IQ 97 never can do division. In primary school, I used a Chinese abacus and used my fingers (no pen!) to do division. In middle school, I had a 4 figure log table and used subtraction to do division. In college, I used a slide rule to do division. Fast forward a couple of decades, I still don't know what is the meaning of division, because Excel is my friend. I just ask Excel "WHAT IF", eg: What if 0 degrees = 0.4mS, 180 degrees = 2.4mS, frequency 50Hz, blah, blah, blah, and Excel, I mean Rpi4B LibreOffice Calc does all the dirty work: https://imgur.com/gallery/12YxJss. – tlfong01 Feb 08 '20 at 04:38
  • Actually Libre Calc is as powerful as the evil MS Excel. It can give you a chart, yes, no scary numbers, not to mention division, of say, "relationship" between degree, pulse width, duty cycle: (1) https://penzu.com/p/772fa292. And much more than that, say for open loop fake PID servo control. Say if you want the servo to move from 20 degrees to 150 degrees, in a trapezoidal pattern, starting very slow, gradually increases to very fast, then steady, then towards the end point, smoothly stops. That is, no abrupt, jerky movements. Yes,poor Arduino guys without Calc are feeling jealous!. – tlfong01 Feb 08 '20 at 05:16

1 Answers1

4

Servos are controlled by pulse width, the pulse width determines the horn angle.

A typical servo responds to pulse widths in the range 1000 to 2000 µs.

A pulse width of 1500 µs moves the servo to angle 0. Each 10 µs increase in pulse width typically moves the servo 1 degree more clockwise. Each 10 µs decrease in pulse width typically moves the servo 1 degree more anticlockwise.

Small 9g servos typically have an extended range and may respond to pulse widths in the range 500 to 2500 µs.

Why do people think servos are controlled by duty cycle? Because servos are typically given 50 pulses per second (50 Hz). So each pulse is potentially a maximum of 20000 µs (1 million divided by 50). A duty cycle is the percentage on time. 100% will be a 20000 µs pulse, way outside the range accepted by a servo.

Do some calculations at 50 Hz for sample pulse widths.

 500 / 20000 = 0.025 or  2.5 % dutycycle
1000 / 20000 = 0.05  or  5.0 % dutycycle
1500 / 20000 = 0.075 or  7.5 % dutycycle
2000 / 20000 = 0.1   or 10.0 % dutycycle
2500 / 20000 = 0.125 or 12.5 % dutycycle

Don't use dutycycles, if possible use pulse widths, and think in pulse widths. If you send pulses at 60 Hz by duty cycle the servo will go to the wrong position.

joan
  • 67,803
  • 5
  • 67
  • 102