🍞
Breadstick
  • Breadstick Innovations Website
  • Breadsticks
    • đŸĨ–Raspberry Breadstick
      • Code Examples
        • Demo Code
        • POV Wand
        • Pride Flags POV Wand
        • 6-Axis IMU
        • RGB Blink
        • AsyncIO RGB Blink
        • Digital Input
        • Digital Output
        • PWM
        • ADC
        • Servo Motor
    • đŸĨ–Raspberry Breadstick Lite
    • 🍞Support Boards
      • I2C Devices
        • 🚌I2C Bus Rail Adapter
        • đŸŒĻī¸Weather Crouton
        • 😎Brightness Crouton
        • 📏Distance Crouton
        • đŸĢ¨Motion Crouton
      • I2S Devices
        • đŸ“ĸBoombox
      • SPI Devices
      • đŸ•šī¸Buttons/Switches
    • Learning Resources
    • Troubleshooting
  • Nougat
    • Nougat C3
    • Nougat Quad
    • Installing WLED
  • Pico Slices
    • 🔴Slice 1 - LED Mixer
      • Assembly Guide
      • CircuitPython Code
        • 1 - Blink
        • 2 - Analog Read to Plotter
        • 3 - PWM Fade
        • 4 - Pot Controlled PWM
        • 5 - Gamma Correction
    • âąī¸Slice 2 - Stopwatch
      • Assembly Guide
      • Coding Lessons
        • 1 - 7-Segment Display Intro
        • 2 - Cycling Through All Segments
    • âŦœSlice 3 - 8x8 Dot Matrix
      • Assembly Guide
      • MicroPython Code
        • 1 - Moveable Pixel
        • 2 - Snake
    • Circuit Python Setup
    • Reset Bricked Pico
  • Christmas
    • Christmas Tree DIY kit
  • Protoboards
    • â¤ī¸Proto-Heart
    • đŸĨĒProto-Toast
  • SHOP
Powered by GitBook
On this page
  • Circuit Python Library
  • Code

Was this helpful?

  1. Breadsticks
  2. Raspberry Breadstick
  3. Code Examples

Servo Motor

PreviousADCNextRaspberry Breadstick Lite

Last updated 1 year ago

Was this helpful?

Circuit Python Library

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)
đŸĨ–
https://github.com/adafruit/Adafruit_CircuitPython_Motor