PWM_servo_control.py
 1  # SPDX-FileCopyrightText: 2020 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import board
 7  import pwmio
 8  
 9  # Initialize PWM output for the servo (on pin D5):
10  servo = pwmio.PWMOut(board.D5, frequency=50)
11  
12  
13  # Create a function to simplify setting PWM duty cycle for the servo:
14  def servo_duty_cycle(pulse_ms, frequency=50):
15      period_ms = 1.0 / frequency * 1000.0
16      duty_cycle = int(pulse_ms / (period_ms / 65535.0))
17      return duty_cycle
18  
19  
20  # Main loop will run forever moving between 1.0 and 2.0 mS long pulses:
21  while True:
22      servo.duty_cycle = servo_duty_cycle(1.0)
23      time.sleep(1.0)
24      servo.duty_cycle = servo_duty_cycle(2.0)
25      time.sleep(1.0)