/ CircuitPython_on_Linux_and_Raspberry_Pi / PWM_motor_servo_control.py
PWM_motor_servo_control.py
1 # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import pwmio 8 from adafruit_motor import servo 9 10 # create a PWMOut object on Pin D5. 11 pwm = pwmio.PWMOut(board.D5, duty_cycle=2 ** 15, frequency=50) 12 13 # Create a servo object. 14 servo = servo.Servo(pwm) 15 16 while True: 17 for angle in range(0, 180, 5): # 0 - 180 degrees, 5 degrees at a time. 18 servo.angle = angle 19 time.sleep(0.05) 20 for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time. 21 servo.angle = angle 22 time.sleep(0.05)