4 - Pot Controlled PWM

Use the analog value from a potentiometer to control the brightness of an LED.

Video

Code with Detailed Comments

"""
Pico Slice 1 - RGB Mixer
Tutorial 4 - Pot Controlled PWM
"""

import time
import board
from pwmio import PWMOut
from analogio import AnalogIn


# Represent the Pot in code as an AnalogIn object.
r_pot = AnalogIn(board.A2)

# Represent the Red LED as a PWMOut object.
r_led = PWMOut(board.GP2, duty_cycle=0, frequency=5000, variable_frequency=False)


while True:  # Loop Forever:
    input_val = r_pot.value         # Read the potentiometer's current value (16-bit)
    r_led.duty_cycle = input_val    # Set the red LED's duty-cycle (16-bit) to the current potentiometer value
    print((input_val,))             # Send to computer as a tuple so it will show up in the plotter
    time.sleep(0.1)                 # Do nothing for 100 milliseconds

Just Code

"""
Pico Slice 1 - RGB Mixer
Tutorial 4 - Pot Controlled PWM
"""

import time
import board
from pwmio import PWMOut
from analogio import AnalogIn

r_pot = AnalogIn(board.A2)
r_led = PWMOut(board.GP2, duty_cycle=0, frequency=5000, variable_frequency=False)

while True:
    input_val = r_pot.value
    r_led.duty_cycle = input_val
    print((input_val,))
    time.sleep(0.1)

Last updated