Lab 7: GIZMO + Servos

Alright! Today we’re going to use the GIZMO to connect a few motors to our Bluefruit. Take out your Bluefruit, your USB cable, and your servo motor (you only have one in the kit, but the photo below has two):

Note that the servo motors in the photo have what are called servo horns or servo arms attached – the white plastic bit on the end. You should have a tiny bag of these in your kit as well- grab that and put one of them on your servo, it just snaps into place (you can use any of the provided arms, they come in various shapes!)

Coding One Motor

Next, the code. To run this you’ll need the adafruit_motor library. You can follow the directions from Lab 4 to get this library from the Circuit Python site, or you can download it directly from our Course Resources folder. Your browser may zip it if you choose the latter, so make sure you unzip it (unarchive it) before putting it in the lib folder on your CIRCUITPY drive.

Now, open up Mu and load code.py (if it is not already loaded), delete the code that is there and replace it with the following:

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

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

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

while True:
    servo1.angle = 90

Servos use the PWM (pulse width modulation) protocol, and this library helps to make that easier. We then create a PWM object, and call it servo1. Once you’ve got that in your set up, you can easily control the servo in your loop by just giving it an angle to go to. In this case, we are starting with 90.

Plug in the servo motor from your kit into A2 – the same JST we used for Neopixels last week (remove those first of course). Save the code and you should hear the motor engage and, perhaps, move. Servo motors have a range of just about 180 degrees, so that 90 can go from 0 to 180. Use that information and our trusty friend time.sleep(3) (a three second delay) to make the motor wave back and forth. Call the professor over when you are done, and she’ll give you the materials for the next section.

Coding Two Motors

For this next section, you’re going to use the code below to control motors that will move objects. The code below has some modifications from our original code: it has two servo objects (servo1 and servo2), and it uses a for loop (remember that from last week) to create a more controlled movement. Use this in combination with the above to wire up motors and move your objects!

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

pwm = pwmio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50)
pwm2 = pwmio.PWMOut(board.A1, duty_cycle=2 ** 15, frequency=50)

servo1 = servo.Servo(pwm)
servo2 = servo.Servo(pwm2)

while True:
    for angle in range(0, 180, 5):  # 0 - 180 degrees, 5 degrees at a time.
        servo1.angle = angle
        servo2.angle = angle
        time.sleep(0.05)
    for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
        servo1.angle = angle
        servo2.angle = angle
        time.sleep(0.05)