# Raspberry pi servo motor angle calculation [closed]

I know servo :

``````1 ms is 0 degree
1.5 ms is 90 degree
2 ms is 180 degree
``````

I also know servo motor has period of 20 ms and PWM from 1-2 ms, but i totally confused how to calculate angle like 30 or 60 or any angle between 0-180 . So I want to enter angle and the angle will be converted to dutycycle. than can be used in `p.ChangeDutyCycle`

``````import time
import RPi.GPIO as gpio
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(7,gpio.OUT)
pwm = gpio.PWM(7,100)
pwm.start(2.5)
def change(k):
pwm.ChangeDutyCycle(k)
time.sleep(1)

try:
while True:
k = float(raw_input("enter angle: "))
k = (1+(float(k)/180))/20
change(k)
except KeyboardInterrupt:
pwm.stop()
gpio.cleanup()
exit
``````

## closed as off-topic by lenik, Steve RobillardDec 2 '14 at 1:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – lenik, Steve Robillard
If this question can be reworded to fit the rules in the help center, please edit the question.

• As originally written the question had nothing to do with the Raspberry Pi. However subsequent edits showed the use of the Python module RPi.GPIO which only runs on the Raspberry Pi. – joan Dec 2 '14 at 10:48

For the Raspberry Pi Python RPi.GPIO module (which seems to use dutycycle values between 0 and 100).

dutycycle = ((angle/180.0) + 1.0) * 5.0

so angle=0 gives 1.0 * 5 (5%), angle=90 gives 1.5 * 5 (7.5%), angle=180 gives 2.0 * 5 (10%), angle=45 gives 1.25 * 5 (6.25%) etc.

The RPi.GPIO ChangeDutyCycle method is not a reliable way to control servos. It uses software timing which will cause servo glitches.

• its pwm not ducty cylce, need to convert it to duty cycle??, – Hackaholic Dec 1 '14 at 13:06
• pwm = duty cycle – Werner Kvalem Vesterås Dec 1 '14 at 13:08
• @Hackaholic I edited the calculation to give dutycycle values as I think are used by RPi.GPIO. – joan Dec 1 '14 at 13:10
• @joan yea i am using RPi.GPIO, it totally confused me – Hackaholic Dec 1 '14 at 13:11
• ahhh i was so stupid i was using this formula but i was multpling it wiht 100, ohhh, we dont not to multiply to 100 – Hackaholic Dec 1 '14 at 13:13