🍞
Breadstick
  • Breadstick Innovations Website
  • Breadsticks
    • đŸĨ–Raspberry Breadstick
      • Code Examples
        • Demo Code
        • POV Wand
        • Pride Flags POV Wand
        • 6-Axis IMU
        • RGB Blink
        • AsyncIO RGB Blink
        • Digital Input
        • Digital Output
        • PWM
        • ADC
        • Servo Motor
    • đŸĨ–Raspberry Breadstick Lite
    • 🍞Support Boards
      • I2C Devices
        • 🚌I2C Bus Rail Adapter
        • đŸŒĻī¸Weather Crouton
        • 😎Brightness Crouton
        • 📏Distance Crouton
        • đŸĢ¨Motion Crouton
      • I2S Devices
        • đŸ“ĸBoombox
      • SPI Devices
      • đŸ•šī¸Buttons/Switches
    • Learning Resources
    • Troubleshooting
  • Nougat
    • Nougat C3
    • Nougat Quad
    • Installing WLED
  • Pico Slices
    • 🔴Slice 1 - LED Mixer
      • Assembly Guide
      • CircuitPython Code
        • 1 - Blink
        • 2 - Analog Read to Plotter
        • 3 - PWM Fade
        • 4 - Pot Controlled PWM
        • 5 - Gamma Correction
    • âąī¸Slice 2 - Stopwatch
      • Assembly Guide
      • Coding Lessons
        • 1 - 7-Segment Display Intro
        • 2 - Cycling Through All Segments
    • âŦœSlice 3 - 8x8 Dot Matrix
      • Assembly Guide
      • MicroPython Code
        • 1 - Moveable Pixel
        • 2 - Snake
    • Circuit Python Setup
    • Reset Bricked Pico
  • Christmas
    • Christmas Tree DIY kit
  • Protoboards
    • â¤ī¸Proto-Heart
    • đŸĨĒProto-Toast
  • SHOP
Powered by GitBook
On this page
  • Video
  • Code with Detailed Comments
  • Just Code

Was this helpful?

  1. Pico Slices
  2. Slice 1 - LED Mixer
  3. CircuitPython Code

3 - PWM Fade

Previous2 - Analog Read to PlotterNext4 - Pot Controlled PWM

Last updated 2 years ago

Was this helpful?

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)
🔴