free hit counter

Raspberry Pi, Python Servo Control

The following python code, along with Adafruit PWM and Servo Kernel allows you to control a servo with the following syntax:

python [degree] [degree] [degree]...

Example rotation to 180 and back to 0 twice:

python 0 180 0 180 0

# Servo Control
import time  
import sys  
def set(property, value):  
                f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
                print("Error writing to: " + property + " value: " + value)

def setServo(angle):  
        set("servo", str(angle))

set("delayed", "0")  
set("mode", "servo")  
set("servo_max", "180")

point = 1  
while point < len(sys.argv):  
    time.sleep(0.5) #may need to change depending on speed of servo to allow full range of movement
    point = point + 1

If you want to build in a non-standard time delay we simply change:
time.sleep(0.5) to time.sleep(float(sys.argv[1])) and now we can use the syntax like such:

python [delay] [degree] [degree] [degree]...

On top of this if you want to program your own delays as arguments you could with sys.argv.