# Import the necessary libraries to enable
# communication between the code and the
# sketch on the board.
import board
import digitalio
import pwmio
from time import sleep
# Initialize the PWM for each LED to represent
# an analog output.
led0 = pwmio.PWMOut(board.D0, duty_cycle=0, frequency=5000)
led1 = pwmio.PWMOut(board.D6, duty_cycle=0, frequency=5000)
led2 = pwmio.PWMOut(board.D7, duty_cycle=0, frequency=5000)
# Declare the button as an output
button = digitalio.DigitalInOut(board.D1)
button.direction = digitalio.Direction.OUTPUT
while True:
# Initialize a temporary variable
# to control the pwm
i = 0
# The LEDs are initialized in the
# "off" state.
led0.duty_cycle = 0
led1.duty_cycle = 0
led2.duty_cycle = 0
sleep(0.1)
# It will execute while the button is pressed
# and the temporary variable won't exceed the
# maximum value of the PWM, which in this case
# is 65535
while button.value == True and i < 65535:
# Assign the duty cycle to the temporary
# variable and increment it by 500, so the
# LEDs are turned on progressively.
led0.duty_cycle = i
led1.duty_cycle = i
led2.duty_cycle = i
i += 500
sleep(0.05)