Chris
Chris

Reputation: 311

Raspberry Pi Python Code Won't Move Servo

So I am doing a little project with a Raspberry Pi that involves moving a servo motor. In the following code in Python 3, 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.

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 > 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()

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.

Any help is appreciated!

Upvotes: 1

Views: 1906

Answers (2)

Chris
Chris

Reputation: 311

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.

Thank you for the coding help!

Upvotes: 3

Paul Cornelius
Paul Cornelius

Reputation: 10969

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

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)

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

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 :-).

Upvotes: 0

Related Questions