> For the complete documentation index, see [llms.txt](https://learn.breadstick.ca/breadstick/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.breadstick.ca/breadstick/pico-slices/slice-1-led-mixer/circuitpython-code/3-pwm-fade.md).

# 3 - PWM Fade

### Video

{% embed url="<https://youtu.be/8SnumUphBnY>" %}

### Code with Detailed Comments

```python
"""
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

```python
"""
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)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://learn.breadstick.ca/breadstick/pico-slices/slice-1-led-mixer/circuitpython-code/3-pwm-fade.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
