1# Servo1.py
2
3import RPi.GPIO as GPIO
4import time
5
6P_SERVO = 22 # adapt to your wiring
7fPWM = 50 # Hz (not higher with software PWM)
8a = 10
9b = 2
10
11def setup():
12 global pwm
13 GPIO.setmode(GPIO.BOARD)
14 GPIO.setup(P_SERVO, GPIO.OUT)
15 pwm = GPIO.PWM(P_SERVO, fPWM)
16 pwm.start(0)
17
18def setDirection(direction):
19 duty = a / 180 * direction + b
20 pwm.ChangeDutyCycle(duty)
21 print("direction =", direction, "-> duty =", duty)
22 time.sleep(1) # allow to settle
23
24print("starting")
25setup()
26for direction in range(0, 181, 10):
27 setDirection(direction)
28direction = 0
29setDirection(0)
30GPIO.cleanup()
31print("done")
32