This example uses the Adafruit’s pulseio package to create a PWMOut object.

import time
import board
import pulseio

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

while True:
    for i in range(100):
        # PWM LED up and down
        if i < 50:
            # Up
            led.duty_cycle = int(i * 2 * 65535 / 100)
        else:
            # Down
            led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100)       
        time.sleep(0.01)

Example demo

This guide was first published on Dec 18, 2019. It was last updated on Dec 18, 2019.

This page (PWM) was last updated on Dec 10, 2019.

Text editor powered by tinymce.