Let's dim an LED!

To do this we will use Pulse Width Modulation (PWM) output. The duty_cycle of the PWM output will control the LED brightness. We'll combine this with the previous ADC example so we can use the knob to control the LED brightness. Here's the breadboard layout:

And here's the code to run:

import board
import pwmio
import analogio

knob = analogio.AnalogIn(board.ADC0)

led = pwmio.PWMOut(board.GP15, frequency=1000)

while True:
    led.duty_cycle = knob.value

Turn the knob and the LED should get dimmer and brighter.

This guide was first published on May 01, 2021. It was last updated on May 26, 2021.

This page (PWM) was last updated on Apr 26, 2021.

Text editor powered by tinymce.