In order to use servos, we take advantage of pwmio
. Now, in theory, you could just use the raw pwmio
calls to set the frequency to 50 Hz and then set the pulse widths. But we would rather make it a little more elegant and easy!
So, instead we will use adafruit_motor
which manages servos for you quite nicely! adafruit_motor
is a library so be sure to grab it from the library bundle if you have not yet! If you need help installing the library, check out the CircuitPython Libraries page.
Servos come in two types:
- A standard hobby servo - the horn moves 180 degrees (90 degrees in each direction from zero degrees).
- A continuous servo - the horn moves in full rotation like a DC motor. Instead of an angle specified, you set a throttle value with 1.0 being full forward, 0.5 being half forward, 0 being stopped, and -1 being full reverse, with other values between.
The connections for a servo are the same for standard servos and continuous rotation servos.
Connect the servo's brown or black ground wire to ground on the CircuitPython board.
Connect the servo's red power wire to 5V power. USB power can in some cases good for a servo or two. But some USB ports supply limited current, and operating a servo from the USB 5V line may cause a power brownout and board crash.
For more servos, you'll need an external battery pack or external power supply. Do not use 3.3V for powering a servo!
Connect the servo's yellow or white signal wire to the control/data pin, in this case A1 or A2 but you can use any PWM-capable pin.
For example, to wire a servo to Trinket, connect the ground wire to GND, the power wire to USB, and the signal wire to 0.
Remember, A2 on Trinket is labeled "0".
For Gemma, use jumper wire alligator clips to connect the ground wire to GND, the power wire to VOUT, and the signal wire to A2.
For Circuit Playground Express and Circuit Playground Bluefruit, use jumper wire alligator clips to connect the ground wire to GND, the power wire to VOUT, and the signal wire to A2.
For boards like Feather M0 Express, ItsyBitsy M0 Express and Metro M0 Express, connect the ground wire to any GND, the power wire to USB or 5V, and the signal wire to A2.
For the Metro M4 Express, ItsyBitsy M4 Express and the Feather M4 Express, connect the ground wire to any G or GND, the power wire to USB or 5V, and the signal wire to A2.
Standard Servo Code
Here's an example that will sweep a servo connected to pin A2 from 0 degrees to 180 degrees (-90 to 90 degrees) and back.
To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Essentials/CircuitPython_Servo/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """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, my_servo. my_servo = servo.Servo(pwm) while True: for angle in range(0, 180, 5): # 0 - 180 degrees, 5 degrees at a time. my_servo.angle = angle time.sleep(0.05) for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time. my_servo.angle = angle time.sleep(0.05)
Continuous Servo Code
There are two differences with Continuous Servos vs. Standard Servos:
- The
servo
object is created likemy_servo = servo.ContinuousServo(pwm)
instead ofmy_servo = servo.Servo(pwm)
- Instead of using
myservo.angle
, you usemy_servo.throttle
using a throttle value from 1.0 (full on) to 0.0 (stopped) to -1.0 (full reverse). Any number between would be a partial speed forward (positive) or reverse (negative). This is very similar to standard DC motor control with the adafruit_motor library.
This example runs full forward for 2 seconds, stops for 2 seconds, runs full reverse for 2 seconds, then stops for 4 seconds.
To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Essentials/CircuitPython_Continuous_Servo/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY
# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT """CircuitPython Essentials Servo continuous rotation 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, frequency=50) # Create a servo object, my_servo. my_servo = servo.ContinuousServo(pwm) while True: print("forward") my_servo.throttle = 1.0 time.sleep(2.0) print("stop") my_servo.throttle = 0.0 time.sleep(2.0) print("reverse") my_servo.throttle = -1.0 time.sleep(2.0) print("stop") my_servo.throttle = 0.0 time.sleep(4.0)
Pretty simple!
Note that we assume that 0 degrees is 0.5ms and 180 degrees is a pulse width of 2.5ms. That's a bit wider than the official 1-2ms pulse widths. If you have a servo that has a different range you can initialize the servo
object with a different min_pulse
and max_pulse
. For example:
my_servo = servo.Servo(pwm, min_pulse = 500, max_pulse = 2500)
For more detailed information on using servos with CircuitPython, check out the CircuitPython section of the servo guide!
Text editor powered by tinymce.