Skip to content

Adding Blinka PWM code. #1167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CircuitPython_on_Linux_and_Raspberry_Pi/PWM_LED.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import time
import board
import pulseio

led = pulseio.PWMOut(board.D16, frequency=5000, duty_cycle=0)

while True:
for i in range(100):
# PWM LED up and down
if i < 50:
led.duty_cycle = int(i * 2 * 65535 / 100) # Up
else:
led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100) # Down
time.sleep(0.01)
18 changes: 18 additions & 0 deletions CircuitPython_on_Linux_and_Raspberry_Pi/PWM_motor_servo_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import time
import board
import pulseio
from adafruit_motor import servo

# create a PWMOut object on Pin D5.
pwm = pulseio.PWMOut(board.D5, duty_cycle=2 ** 15, frequency=50)

# Create a servo object.
servo = servo.Servo(pwm)

while True:
for angle in range(0, 180, 5): # 0 - 180 degrees, 5 degrees at a time.
servo.angle = angle
time.sleep(0.05)
for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
servo.angle = angle
time.sleep(0.05)
21 changes: 21 additions & 0 deletions CircuitPython_on_Linux_and_Raspberry_Pi/PWM_servo_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time
import board
import pulseio

# Initialize PWM output for the servo (on pin D5):
servo = pulseio.PWMOut(board.D5, frequency=50)


# Create a function to simplify setting PWM duty cycle for the servo:
def servo_duty_cycle(pulse_ms, frequency=50):
period_ms = 1.0 / frequency * 1000.0
duty_cycle = int(pulse_ms / (period_ms / 65535.0))
return duty_cycle


# Main loop will run forever moving between 1.0 and 2.0 mS long pulses:
while True:
servo.duty_cycle = servo_duty_cycle(1.0)
time.sleep(1.0)
servo.duty_cycle = servo_duty_cycle(2.0)
time.sleep(1.0)