Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

PWM CircuitPython
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PWM CircuitPython

by wrc on Sun Jun 28, 2020 3:16 pm

Last week received a Metro Express board after setting aside an Arduino.

A helpful example script showing an LED fading up and then down is provided by Kattni Rembor - refer to https://learn.adafruit.com/circuitpytho ... python-pwm.

While several examples can be found of PWM fading a LED up and down, using different processors and programming language, a comprehensive search has not come up with any examples of a loop to fade up, then stay on at maximum brightness for a time period and then fade down.

Any possibility / suggestion of the code below being modified to provide a more comprehensive learning example?

Code: Select all | TOGGLE FULL SIZE
import time
import board
import pulseio

led = pulseio.PWMOut(board.D13, 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)

wrc
 
Posts: 1
Joined: Wed Dec 04, 2019 6:57 am

Re: PWM CircuitPython

by tannewt on Mon Jun 29, 2020 6:47 pm

You can add:

Code: Select all | TOGGLE FULL SIZE
if i == 50:
  time.sleep(5)


To the start of the loop for it to pause at 50 and then continue.

tannewt
 
Posts: 1685
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.