1

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!

Chris
  • 191
  • 2
  • 12

2 Answers2

3

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!

Chris
  • 191
  • 2
  • 12
  • 2
    I am greatly disappointed in the lack of upvotes for this answer. Every bit of "debugging" I could do for hours with a multimeter and I was convinced my Raspberry Pi was working correctly. Turns out my separate battery pack for the servo was the issue. Connecting ground for the Pi, servo, and battery all together is what got it working for me. – Douglas Parker Jul 27 '17 at 02:17
0

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

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
  • I fixed the scope issue and still no luck. I even switched the servo pin to a different GPIO pin. – Chris Sep 23 '16 at 02:24
  • I don't know how your motor works. In the program, your duty cycle values (variable named `dc`) will range from 2 to 12; the full range is 0-100 (percent). Are you sure the range 2%-12% is what your motor needs? Can you look at the pin with an AC voltmeter? You should see a duty-cycle dependent voltage. – Paul Cornelius Sep 23 '16 at 07:14
  • I am currently using this servo: https://www.amazon.com/gp/product/B0015H2V72/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1 However, I do not have a voltmeter unfortunately. – Chris Sep 23 '16 at 14:42