Circuit Python Servo - adalessio84/circuitPython GitHub Wiki
Servo
Objective
In this assignment we built off the last learning the power of the metro express. We learned how to take the metro some wires and a servo and turn it with the conductivity of our hands. This was very fun and very eye opening assignment because its not something I would have ever though of being possible with the metro express. We leanrd more about PWM.
Pictures
- Credit to Dr. Shields
import board
import pulseio
import touchio
import neopixel
from adafruit_motor import servo
touch_1 = board.A0
#sets A0 to touch one
touch_2 = board.A1
#sets A1 to touch 2
pwm = pulseio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50)
#sets the frequency and sets up duty cicle to go out of A2 with pulsie io and PWM
dot = neopixel.NeoPixel(board.NEOPIXEL, 1)
my_servo = servo.Servo(pwm)
touch1 = touchio.TouchIn(touch_1)
touch2 = touchio.TouchIn(touch_2)
angle = 0
while True:
if touch1.value:
print("wow!")
#for angle in range(0, 180, 5):
if angle < 180:
angle+=10
my_servo.angle = angle
time.sleep(0.05)
if touch2.value:
print("spicy!")
if angle > 0:
angle-=10
my_servo.angle = angle
time.sleep(0.05)
#for angle in range(0, 180, 5):
# my_servo.angle = angle
# time.sleep(0.05)
#for angle in range(180, 0, -5):
# my_servo.angle = angle
# time.sleep(0.05)
- This code snippet gives you a start to making the servo rotate. Next, you just need angles and degrees to make it turn.
Problems
I had problems getting the servo to turn but after I got to turn it was pretty easy from there and I was able to complete the assignment.
What I Learned!
I learned how to use code to make a servo spin from a pwm Input. I also learned how to wire a servo up to a metro and how to use touch.