SG90-Servo: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 41: | Line 41: | ||
GPIO.cleanup() | GPIO.cleanup() | ||
</source> | |||
== Angle control == | |||
<source lang='python'> | |||
# set servo position by angle (degree) | |||
# calculate duty cycle from angle | |||
def setservo(angle): | |||
if angle < 0: | |||
angle = 0 | |||
if angle > 180: | |||
angle = 180 | |||
pwm = angle/18 + 2.5 | |||
servo.ChangeDutyCycle(pwm) | |||
</source> | </source> | ||
[[Category:Raspberry]] | [[Category:Raspberry]] | ||
Latest revision as of 18:05, 12 June 2019
see also https://tutorials-raspberrypi.de/raspberry-pi-servo-motor-steuerung/
<HTML5video width="960" height="600" controls autoplay="false" loop="false">20190612_084825</HTML5video>
Python Source Code
# Servo control
# see https://tutorials-raspberrypi.de/raspberry-pi-servo-motor-steuerung/
import RPi.GPIO as GPIO
import time
# set the GPIO pin to be used
servoPIN = 23
# set the mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50) # set pulse width modulation (PWM to 50Hz)
# start the pulswidth with a 2.5 cycle
p.start(2.5) # Initialisierung
try:
while True:
p.ChangeDutyCycle(5)
time.sleep(0.5)
p.ChangeDutyCycle(7.5)
time.sleep(0.5)
p.ChangeDutyCycle(10)
time.sleep(0.5)
p.ChangeDutyCycle(12.5)
time.sleep(0.5)
p.ChangeDutyCycle(10)
time.sleep(0.5)
p.ChangeDutyCycle(7.5)
time.sleep(0.5)
p.ChangeDutyCycle(5)
time.sleep(0.5)
p.ChangeDutyCycle(2.5)
time.sleep(0.5)
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
Angle control
# set servo position by angle (degree)
# calculate duty cycle from angle
def setservo(angle):
if angle < 0:
angle = 0
if angle > 180:
angle = 180
pwm = angle/18 + 2.5
servo.ChangeDutyCycle(pwm)