CircuitPython for Hobby Servo Motors
You can learn the basics of CircuitPython with CircuitPlayground Express here.
With the special Crickit support in CircuitPython, the adafruit_crickit, adafruit_seesaw and adafruit_motor library is built in, which saves you tons of space and makes it really fast to get started. No extra libraries required!
For in-depth information on using servo motors with CircuitPython, see this page
Moving the Servo via Buttons
We want to turn the servo horn on command from one side (0 degrees) to the other (180 degrees).
import time from digitalio import DigitalInOut, Pull, Direction from adafruit_crickit import crickit import board # Two onboard CPX buttons for input (low level saves memory) button_a = DigitalInOut(board.BUTTON_A) button_a.direction = Direction.INPUT button_a.pull = Pull.DOWN button_b = DigitalInOut(board.BUTTON_B) button_b.direction = Direction.INPUT button_b.pull = Pull.DOWN # Create one servo on Crickit's servo port #1 servo = crickit.servo_1 while True: if button_a.value: print("Button A pressed!") servo.angle = 180 # about 180 degrees time.sleep(0.1) # give crickit some time to move if button_b.value: print("Button B pressed!") servo.angle = 0 # about 0 degrees time.sleep(0.1) # give crickit some time to move
This program will move a servo to the zero degree point when the Circuit Playground Express button A is pressed. It will move the servo to 180 degrees when button B is pressed.
This trips up everyone the first time they use a servo - while the servos are often talked about in terms of 0 to 180 degree motion, there can be variation from servo to servo. Some metal gear servos only move 90 degrees. Others may need longer 'pulse lengths' and we won't go into that there. There's nothing wrong with your servo just because it doesn't move a full 180 degrees! Visit https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit/circuitpython-servos for more details on how to customize the pulse lengths!
Moving a Servo in a Slower, Controlled Fashion
The code below creates a stepped movement from 0 to 180 and back again. The timing is variable with short time.sleep()
s. This is a common use in terms of things like wings flapping or other movements.
import time from adafruit_crickit import crickit # Create one servo on seesaw servo port #1 servo = crickit.servo_1 while True: for angle in range(0, 180, 1): # from 0 to 180, 1 degree at a time servo.angle = angle time.sleep(0.01) # do nothing for a 1/100 second for angle in range(180, 0, -1): # from 180 to 0, -1 deg at a time servo.angle = angle time.sleep(0.01) # do nothing for a 1/100 second
You can make the code slower by increasing the number in time.sleep()
to have more delay for every small movement. If you increase the range step from 1
to say 5
or 10
(and the same for the negative movement in the second for
loop) ,you'll start to see jitter as the servo snaps to each movement creating some momentum that causes a small backwards motion when it stops.
Page last edited March 08, 2024
Text editor powered by tinymce.