Andrea Torlai
Created October 5, 2024

Astrial - Using the PWM / Servo HAT

The goal of this tutorial is to show how to use the Adafruit 16-Channel PWM / Servo Hat with the Astrial board by System Electronics

23
Astrial - Using the PWM / Servo HAT

Things used in this project

Hardware components

Adafruit 16-Channel PWM / Servo Hat
×1
LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×2
Positional servo motor
×1
Continuous servo motor
×4
Raspberry Pi Compute Module 4 IO Board
×1
Astrial
×1

Story

Read more

Schematics

Schematic

Code

example.py

Python
import time
from PCA9685.servo import *
from PCA9685 import PCA9685

# Create a simple PCA9685 class instance.
pca = PCA9685()

pca.frequency = 50

# We make the LED blink ON and OFF smoothly for 4 times before moving on
for k in range(4):
    for i in range(0, 0xFFFF, 100):
        pca.channels[0].duty_cycle = i


    for i in range(0xFFFF, 0, -100):
        pca.channels[0].duty_cycle = i

# We initialise the two servo with channels 1 and 2
continuous = ContinuousServo(pca.channels[1])
positional = Servo(pca.channels[2])

# We first rotate the servo at full speed in clockwise direction
continuous.throttle = 1
time.sleep(2)
# And then we make it spin in the opposite direction
continuous.throttle = -1
time.sleep(2)
# And finally we stop it
continuous.throttle = 0

# We make the positional servo rotate from 0 to 180 degrees and back
for i in range(180):
    positional.angle = i
    time.sleep(0.03)
for i in range(180):
    positional.angle = 180 - i
    time.sleep(0.03)

# We do the same thing but with the fractional angle
fraction = 0.0
while fraction < 1.0:
    positional.fraction = fraction
    fraction += 0.01
    time.sleep(0.03)

Credits

Andrea Torlai
11 projects • 4 followers

Comments