角度伺服馬達(SG90)
-
- python 程式碼
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) pwm=GPIO.PWM(11, 50) pwm.start(0) left = 5 pwm.ChangeDutyCycle(left) # left -90 deg position sleep(2) middle = 7.5 pwm.ChangeDutyCycle(middle) # middle 0 deg position sleep(2) right = 10 pwm.ChangeDutyCycle(right) # right -90 deg position sleep(2) pwm.stop() GPIO.cleanup()
- python 程式碼
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) pwm=GPIO.PWM(11, 50) pwm.start(0) def setAngle(angle): duty = angle / 18 + 3 GPIO.output(11, True) pwm.ChangeDutyCycle(duty) sleep(1) GPIO.output(11, False) pwm.ChangeDutyCycle(duty) setAngle(0) sleep(2) setAngle(45) sleep(2) setAngle(90) sleep(2) setAngle(135) sleep(2) setAngle(180) sleep(2) pwm.stop() GPIO.cleanup()
參考資料
https://www.learnrobotics.org/blog/raspberry-pi-servo-motor/