-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo-test.py
78 lines (65 loc) · 1.72 KB
/
servo-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#-----------------------------------------------------------------------------#
# Servo Test
#
# Micropython code for Raspberry Pi Pico w
#
# File : servo-test.py
# Source : https://github.com/RPiSpy/pi-pico
#
# Desc : Test file to operate servo over operating range
# Hardware : Pi Pico or Pi Pico W
# Software : N/A
#
# Author : Matt Hawkins
# Website : https://www.raspberrypi-spy.co.uk/
#-----------------------------------------------------------------------------#
import time
from machine import Pin, PWM
# Prepare on-board LED
led = Pin("LED", Pin.OUT)
# Adjust for your servo.
# 0 duty cycle is 0
# Full duty cycle is 65535
# 1ms out of 20ms = 65535/20 = 3276
# 2ms out of 2oms = 65535/10 = 6552
# but cheap servos don't follow the maths.
# So getting the full range requires trial
# and error.
SERVO_MAX = 7500
SERVO_MIN = 2000
# Pulse width of 20ms is frequency of 50Hz
SERVO_FREQ = 50
# Set GPIO that servo signal is connected to
SERVO_GPIO = 16
# Create PWM channel
pwm = PWM(Pin(SERVO_GPIO))
pwm.freq(SERVO_FREQ)
max = SERVO_MAX
min = SERVO_MIN
mid = int((max+min)/2)
# Max position
pwm.duty_u16(max)
time.sleep(1)
# Middle position
pwm.duty_u16(mid)
time.sleep(1)
# Minimum position
pwm.duty_u16(min)
time.sleep(1)
while True:
# Slowly rotate from min to max
for position in range(min,max,50):
pwm.duty_u16(position)
print(position)
led.toggle()
time.sleep(0.1)
led.off()
time.sleep(2)
# Slowly rotate from max to min
for position in range(max,min,-50):
pwm.duty_u16(position)
print(position)
led.toggle()
time.sleep(0.01)
led.off()
time.sleep(2)