Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

Positional Servos

Positional servos, unlike continuous rotational servos, are designed to move and hold an angular position within a limited range (typically 0 to 180 degrees). When you set a servo to a specific degree angle, it will attempt to hold this angle until forces applied to it are greater than its torque output

Video Tutorial


Text Tutorial

On our servo mounts, we might have a servo set to 0 degrees: 0deg

and a servo set to 180 degrees. 180


Wiring a Servo

A servo has 3 wires:

  1. Signal (white or yellow): Attach this to most Digital or Analog pins on your board.
    • If you receive a all timers already in use for this pin Serial output, try using a different pin.
  2. Power: Provide 5V, 6V, or 7.4V, depending on your power supply. Higher Voltage generally means stronger servo performance.
  3. Ground: Connect a common ground to both your power supply and your microcontroller.

servo_wiring

You’ll find this Servo-Shroud to x3 Male jumper cable helpful when connecting your servo to your breadboard. jumpers


Programming Basic

python code here

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# 2024 Modified by Brogan Pratt for American School of The Hague
# SPDX-License-Identifier: MIT

"""CircuitPython Essentials Servo standard servo example"""
import time
import board
import pwmio
from adafruit_motor import servo

# ----------------- INIT DEVICES -------------------------
# create a PWMOut object on Pin D0.
pwm = pwmio.PWMOut(board.D0, duty_cycle=2 ** 15, frequency=50)

# Create a servo object, my_servo.
my_servo = servo.Servo(pwm)

while True:
    # basic angles
    my_servo.angle = 0 # set the servo to 0 Degrees, the min point
    time.sleep(1)
    my_servo.angle = 90 # set the servo to 90 Degrees, the midpoint
    time.sleep(1)
    my_servo.angle = 180 #set the servo to 180 Degrees, the max point
    time.sleep(1)


Examining The code

Import Statements

import time
import board
import pwmio
from adafruit_motor import servo

These imports bring in the necessary libraries:

INITIALIZE Devices

pwm = pwmio.PWMOut(board.D0, duty_cycle=2 ** 15, frequency=50) Initializes our servo on Pin Digital 0, with a duty cycle of 2 ** 15, and a frequency of 50.

servogif

source from cytron.io

my_servo = servo.ContinuousServo(pwm) This creates a servo object with methods specific to positional servos.

While True Loop:

while True:
    # basic angles
    my_servo.angle = 0 # set the servo to 0 Degrees, the min point
    time.sleep(1)
    my_servo.angle = 90 # set the servo to 90 Degrees, the midpoint
    time.sleep(1)
    my_servo.angle = 180 #set the servo to 180 Degrees, the max point
    time.sleep(1)

    for angle in range(0, 180, 5):  # 0 to 180 degrees, 5 degrees at a time.
        my_servo.angle = angle
        time.sleep(0.05)
    for angle in range(180, 0, -5):  # 180 to 0 degrees, 5 degrees at a time.
        my_servo.angle = angle
        time.sleep(0.05)

This loop controls the servo by moving it back and forth between 0 and 180 degrees:


Programming “Servo Sweeps”

What happens if you want a servo to move slower, or smoother than a perfect set angle? This is where a servo sweep comes into play. We can “sweep” through the angle changes slower than an instant set degree, allowing for a smoother overall motion.

    # sweeping angles (Try this inside your forever loop)
    for angle in range(0, 180, 5):  # "sweep" 0 - 180 degrees, 5 degrees at a time.
        my_servo.angle = angle
        print(angle)
        time.sleep(0.05)
    for angle in range(180, 0, -5): # "sweep" 180 - 0 degrees, 5 degrees at a time.
        my_servo.angle = angle
        print(angle)
        time.sleep(0.05)
      

Examining the code