Raspberry Pi Python Code Won't Move Servo
<p>So I am doing a little project with a Raspberry Pi that involves moving a servo motor. In the following code in <strong>Python 3</strong>, I begin by starting the servo at approximately 45 degrees. Later in the code, a different angle is determined based on the previous angle, and the the Duty Cycle is changed.</p>

<pre><code>def main():   
    #Import functions
    import measure, move
    import time
    import RPi.GPIO as GPIO
    #Declare Variables
    Servo_pin = 35
    angle = 45
    freq = 50
    #Setup board
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(Servo_pin, GPIO.OUT)
    servo = GPIO.PWM(Servo_pin,freq)
    #Determine Duty Cycle
    dc = 1/18 * (angle) + 2
    print("Starting Duty Cycle: ",dc)
    #Start servo
    servo.start(dc)

    i = 1
    #Determine angle based on previous angle
    while True:
        if (i == 0):
            angle = 45
        elif (i == 1):
            angle = 90
        elif (i == 2):
            angle = 180
        elif (i &gt; 2):
            angle = 45
            i = 0
        i = i+1
        #Change servo's position
        #Convert angle to Duty Cycle
        dc = 1/18 * (angle) + 2
        print("Setting Duty Cycle: ",dc)
        #Change position
        servo.ChangeDutyCycle(dc)
        #Give servo time to finish moving
        time.sleep(0.3)    
main()
</code></pre>

<p>I have the servo connected to a battery pack (4 AA batteries), yet the servo won't move with this code. Now, I'll admit that I'm a beginner, and it's probably something really easy and I apologize in advance if it that is the case.</p>

<p>Any help is appreciated!</p>

<p>According to the docs on <a href="https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/" rel="nofollow">https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/</a><a href="https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/" rel="nofollow">sourceforge</a>, the PWM will stop when the the instance variable representing the PWM goes out of scope.  Both of your functions contain this line: <code>servo = GPIO.PWM(pin,freq)</code>, creating a local variable <code>servo</code> that will go out of scope as soon as the end of the function is reached.  One fix is to move these lines:</p>

<pre><code>import time
import RPi.GPIO as GPIO
# HERE you need to define which pin you are using
# I can't tell you how to do that
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
freq = 10.0   # or some other value to get started
servo = GPIO.PWM(pin,freq)
</code></pre>

<p>to the top of the script, and then remove those lines from the individual functions.  Now <code>servo</code> will be a global variable that will not go out of scope at any point during the program.</p>

<p>There may also be other problems.  Do you have a voltmeter or an oscilloscope to verify that the pin is doing what you intend?  I deal with this kind of application all the time, and it helps a lot to know if the small pieces of the puzzle are actually working before you connect all the hardware.  Nothing ever works right the first time :-).</p>

python python-3.x raspberry-pi servo