Servo Motor with Raspberry Pi and PWM

10 Feb

After my dad and I got an LED to dim using Pulse Width Modulation on my Raspberry Pi, we decided to try to spin a servo motor.  We had a bunch of old servo motors on my robotics team at school so I used one of those, but I didn’t have any information on the pulses that they use.  It was a HiTec HS-485SB.  So, we started off following this YouTube video to modify the code from the LED to operate the servo.  Of course . . . it didn’t work.

There were a couple of reasons why it didn’t work.  First, the video didn’t really talk about how to hook up the servo to the RPi.  Second, every servo is a little different.  The colors of my wires were even different than the colors of his wires.  Third, I didn’t know what pulse widths to use with my servo.  The only thing that I could find is that it used from 0.9 to 2.1 ms so I assumed that means that 0.9 is CCW, 1.5 is center, and 2.1 is CW similar to the video, but not exactly the same.

So, we hooked up the red wire to the 5 volt pin, the black wire to ground, and the yellow wire to pin 21.  I typed in the code from the video and changed all of the pin 7 to pin 21.  I changed all of the pulse widths to match my servo.  I ran the program and bzzz . . . I got a tiny little buzz out of the servo and the whole raspberry pi froze up.  My dad said that he read that you can destroy a RPi by hooking up a servo the wrong way, so we decided to hook the red and black wires up to four AA batteries instead.  So, with much anticipation (and cutting, stripping wires, and soldering batteries), I ran the program again . . . bzzzz, a little buzz and the program stopped.

I figured that it must be the pulse widths, so I played around with the numbers and couldn’t get anything else to work after many, many tries.  Same thing over and over.  It would rotate about 45 degrees counter-clockwise and stop (at least it doesn’t freeze up anymore).

So, I started doing research.  I came across this blog where a noob, like myself, was trying to learn how to spin a servo.  People suggested using something called servoblaster (too complicated for me) and lots of other things that I’d already tried.  But then something jumped out at me that I hadn’t tried yet.  It said that you have to connect the negative side of the batteries to the ground on the Raspberry Pi.  I connected that one wire and bzzz, bzzz, bzzzzzzzzzzz, bzzz, bzzz, bzzzzzzzzz, it WORKED!

Here’s the exact code that came from the video link above:

________________________________________________________________

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(21,GPIO.OUT)

p = GPIO.PWM(21,50)        #sets pin 21 to PWM and sends 50 signals per second

p.start(7.5)          #starts by sending a pulse at 7.5% to center the servo

try:                      # I still don’t know what this does

    while True:       #starts an infinite loop

        p.ChangeDutyCycle(4.5)    #sends a 4.5% pulse to turn the servo CCW

        time.sleep(0.5)                   #continues for a half a second

        p.ChangeDutyCycle(10.5)    #sends a 10.5% pulse to turn the servo CW

        time.sleep(0.5)                   #continues for a half a second

        p.ChangeDutyCycle(7.5)    #sends a 7.5% pulse to center the servo again

        time.sleep(0.5)                   #continues for a half a second

except KeyboardInterrupt:

    p.stop()

    GPIO.cleanup()                 #supposed to stop when a key is pressed, doesn’t work

____________________________________________________________

The percents work because at the beginning, we set it to send 50 pulses per second.  So, 7.5% of 1/50 of a second is .0015 seconds or 1.5 milliseconds.  That’s the pulse that it needs to center the servo as I explained in the beginning.

See the video below of my servo working!  It doesn’t seem to be spinning the full 180 degrees, so I need to play with the numbers some more.  Maybe I’m not giving it enough time to move all the way.

My dad zoomed in real close on the servo because he didn’t want you to see how bad the soldering was on the batteries.  He’s terrible at soldering!  But don’t tell him I said that.

Advertisements

5 Responses to “Servo Motor with Raspberry Pi and PWM”

  1. Reinoud de Lange February 10, 2014 at 10:31 am #

    Well done! I tried it myself too while reviewing the PiRack:
    http://www.element14.com/community/roadTestReviews/1606
    It also shows the code I used.

    • Mariusz Kubiński June 5, 2015 at 10:03 pm #

      Thank you! Spent hours looking for clear explanation of what functions mean exactly and everyone seemed to treat it as some obvious knowledge i should alreayd have.

      • krystal92586 October 16, 2015 at 3:50 pm #

        I owe so much to the Raspberry Pi community that part of the idea behind this blog is to give back by helping others learn how to use the RPi. I’m glad that this was helpful.

  2. Garvit July 19, 2015 at 11:59 am #

    except KeyboardInterrupt: will occur when CTRL+C is pressed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: