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>There needed to be a common ground. I was using two separate breadboards, and did not connect a common ground. As soon as I connected a common ground, the servo began to operate as I wanted. </p>

<p>Thank you for the coding help!</p>

python python-3.x raspberry-pi servo