Servo Motor

Circuit Python Library

https://github.com/adafruit/Adafruit_CircuitPython_Motor

Code

"""
Servo Demo Code
Breadstick Innovations
April 28, 2024
https://learn.breadstick.ca/breadstick/breadsticks/raspberry-breadstick/code-examples/servo-motor

Servos are controlled via a particularly defined PWM signal.
The freqency of the signal is 50Hz and duty cycle varies between 5% to 10% to swing through 180°.
This can be said another way.
The period of the signal is 20ms, and the positive pulse is varied between 1ms and 2ms.
These timings are guidelines and depend on multiple factors.
Adafruit's motor library has a Servo object that can handle converting angles to pulse widths.
Before creating a Servo object, we must first creat a PWMOut object and set the 50Hz frequency.
When creating the servo object, you can adjust the min_pulse and max_pulse to achieve 180° range.
"""

from board import *
from time import sleep
from pwmio import PWMOut
from adafruit_motor import servo

pwm_16 = PWMOut(D16, frequency=50, duty_cycle=0)
servo_16 = servo.Servo(pwm_16, min_pulse=700, max_pulse=2450)

delay = 2
while True:
    servo_16.angle = 0
    sleep(delay)

    servo_16.angle = 90
    sleep(delay)

    servo_16.angle = 180
    sleep(delay)

    servo_16.angle = 90
    sleep(delay)

Last updated