[RTL8722CSM] [RTL8722DM] PWM - LED fade


  • AmebaD[AMB21 / AMB22] x 1

  • LED x 1

  • Resistor(220ohm) x 1


PWM use pulse width modulation to control output duty cycle and is widely used to control LED brightness and motor. Here we are using an LED to demonstrate how PWM works.

Let us connect pin PA_26 to the anode leg of an LED which in series with a current limiting resistor and GND to cathode of the LED as shown below,


Then, copy and paste the following code line by line into REPL and hit Enter. If everything is in order, you should be able to see the LED slowly become brighter as you paste another line of code.

 1from machine import Pin, PWM
 2import time
 3p = PWM(pin = "PA_26")
 4# 0 duty cycle thus output 0
 6# 10% duty cycle
 8# 50% duty cycle
10# 100% duty cycle