from machine import Pin
import time
import esp32
# Initialize the pin that will be used for PWM
pwm_pin = Pin(15)
# Set up LEDC (ESP32's hardware PWM) on the selected pin
# Channel 0, 5 kHz frequency, 10-bit resolution
ledc = esp32.LEDC()
ledc.channel(0, pin=pwm_pin, freq=5000, duty=1023, resolution=10)
# Keep the PWM signal running with different duty cycles
time.sleep(2)
# Change duty cycle to 75%
ledc.duty(0, 768)
time.sleep(2)
# Change duty cycle to 50%
ledc.duty(0, 512)
time.sleep(2)
# Change duty cycle to 25%
ledc.duty(0, 256)
time.sleep(2)
# Set duty cycle to 0% (off)
ledc.duty(0, 0)