3 - PWM Fade

Video

Code with Detailed Comments

"""
Pico Slice 1 - RGB Mixer
Tutorial 3 - PWM Fade
"""

import time
import board
import pwmio

# This time we'll represent our red led in code as a PWMOut object from the pwmio module.
# This will give it the ability to have variable brightness, not just on or off.
# Brightness is controlled by the duty_cycle, a value from 0-65535 (2^16=65536).
# We'll start it at a duty_cycle of 0, so the LED will be off.
# The frequency will be set to 5000Hz or 5kHz, so we won't notice the blinking.
# We don't need to change the frequency of the flashes to control the brightness,
# so variable frequency can be set to False.

r_led = pwmio.PWMOut(board.GP2, duty_cycle=0, frequency=5000, variable_frequency=False)

while True:  # Loop Forever:
    for i in range(0, 65536, 50):   # i will count from 0 to 65535, in steps of 50
        r_led.duty_cycle = i        # set the duty cycle to the current value of i
        time.sleep(0.001)           # do nothing for 1 millisecond

    for i in range(65535, 0, -50):  # i will count from 35535 to 0, in steps of -50
        r_led.duty_cycle = i        # set the duty cycle to the current value of i
        time.sleep(0.001)           # do nothing for 1 millisecond

Just Code

"""
Pico Slice 1 - RGB Mixer
Tutorial 3 - PWM Fade
"""

import time
import board
import pwmio

r_led = pwmio.PWMOut(board.GP2, duty_cycle=0, frequency=5000, variable_frequency=False)

while True:
    for i in range(0, 65536, 50):
        r_led.duty_cycle = i
        time.sleep(0.001)

    for i in range(65535, 0, -50):
        r_led.duty_cycle = i
        time.sleep(0.001)

Last updated