0

I found a code script in this website generated by a user called 'Hackaholic' but when run it is not working.

It gives me the chance to enter an angle but the servomotor is not moving.

Someone suggested to write this line of code (dutycycle = ((angle/180.0) + 1.0) * 5.0) but I do not know where to replace it in the code below.

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
  • Please edit your question and include photos of your connections. Also, how do you know that the servo works and the code is the problem. To rule out the obvious have you entered an angle when prompted? – Steve Robillard Nov 17 '16 at 11:58
  • Make sure you're running this in python2 or it will not work. – sir_ian Nov 17 '16 at 12:55
2

A couple of points.

  1. Servos mostly respond to 50Hz signals, not the 100Hz you are using. I would change pwm = gpio.PWM(7,100) to pwm = gpio.PWM(7, 50).
  2. You are a factor of 100 out in the values sent to the dutycycle function, it expects a percentage in the range 0-100. Try changing change(k) to change(100.0*k).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.