Skip to content

Commit 3fe5a9e

Browse files
authored
Merge pull request #1167 from kattni/blinka-pwm
Adding Blinka PWM code.
2 parents 53650d3 + 8d171f7 commit 3fe5a9e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
import board
3+
import pulseio
4+
5+
led = pulseio.PWMOut(board.D16, frequency=5000, duty_cycle=0)
6+
7+
while True:
8+
for i in range(100):
9+
# PWM LED up and down
10+
if i < 50:
11+
led.duty_cycle = int(i * 2 * 65535 / 100) # Up
12+
else:
13+
led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100) # Down
14+
time.sleep(0.01)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
import board
3+
import pulseio
4+
from adafruit_motor import servo
5+
6+
# create a PWMOut object on Pin D5.
7+
pwm = pulseio.PWMOut(board.D5, duty_cycle=2 ** 15, frequency=50)
8+
9+
# Create a servo object.
10+
servo = servo.Servo(pwm)
11+
12+
while True:
13+
for angle in range(0, 180, 5): # 0 - 180 degrees, 5 degrees at a time.
14+
servo.angle = angle
15+
time.sleep(0.05)
16+
for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
17+
servo.angle = angle
18+
time.sleep(0.05)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import time
2+
import board
3+
import pulseio
4+
5+
# Initialize PWM output for the servo (on pin D5):
6+
servo = pulseio.PWMOut(board.D5, frequency=50)
7+
8+
9+
# Create a function to simplify setting PWM duty cycle for the servo:
10+
def servo_duty_cycle(pulse_ms, frequency=50):
11+
period_ms = 1.0 / frequency * 1000.0
12+
duty_cycle = int(pulse_ms / (period_ms / 65535.0))
13+
return duty_cycle
14+
15+
16+
# Main loop will run forever moving between 1.0 and 2.0 mS long pulses:
17+
while True:
18+
servo.duty_cycle = servo_duty_cycle(1.0)
19+
time.sleep(1.0)
20+
servo.duty_cycle = servo_duty_cycle(2.0)
21+
time.sleep(1.0)

0 commit comments

Comments
 (0)