Section 3.8 Pulse-width modulation (PWM)
Subsection 3.8.1 Controlling brightness of an LED
# Drive an LED at various brightness levels using PWM
from machine import Pin, PWM
led = PWM(Pin(13))
led.freq(1000) # Hz, ranges from 1Hz to 40MHz
led.duty(512) # Fraction, ranges from 0-1024
Subsection 3.8.2 Controlling a servo motor
# Control the position of a servo using PWM
from machine import Pin, PWM
servo = PWM(Pin(23), freq=50)
# For my little SG90 micro-servos,
# 20 is full left, 130 is full right, 90 is about the middle
servo.duty(20)
Subsection 3.8.3 Square-wave sound with PWM
from machine import Pin, PWM
led = PWM(Pin(13))
led.freq(440) # 440Hz is A below middle C
led.duty(512) # Fraction, ranges from 0-1024