樹莓派控制S90舵機
阿新 • • 發佈:2019-01-29
- #!/usr/bin/env python
- # coding: utf-8
- import RPi.GPIO as GPIO
- import time
- import signal
- import atexit
- atexit.register(GPIO.cleanup)
- servopin = 21
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(servopin, GPIO.OUT, initial=False)
- p = GPIO.PWM(servopin,50) #50HZ
- p.start(0)
- time.sleep(2)
- while(True):
- for i in range(0,181,10):
- p.ChangeDutyCycle(2.5 + 10 * i / 180) #設定轉動角度
- time.sleep(0.02) #等該20ms週期結束
- p.ChangeDutyCycle(0) #歸零訊號
- time.sleep(0.2)
- for i in range(181,0,-10):
- p.ChangeDutyCycle(2.5 + 10 * i / 180)
- time.sleep(0.02)
- p.ChangeDutyCycle(0)
- time.sleep(0.2)