import pwm
The pwm module creates independent PWM outputs without exposing ESP32 LEDC channel numbers. Use it for dimming LEDs, driving passive buzzers, controlling simple speed inputs, or any signal that needs a configurable duty cycle and frequency.
Quick example
import pwm led = pwm.open(pin=2, frequency=5000, resolution_bits=8) buzzer = pwm.open(pin=4, frequency=2000, resolution_bits=10) led.write_percent(50) buzzer.write_percent(25) led.fade_percent(100, 1000) buzzer.stop()
importpwm
PWM output module.
The pwm module creates independent PWM outputs without exposing ESP32 LEDC channel numbers. Use it for dimming LEDs, driving passive buzzers, controlling simple speed inputs, or any signal that needs a configurable duty cycle and frequency.
pwm.open(pin: int, frequency: int = None, resolution_bits: int = None) -> pwm_output
pwm.open(pin: int, frequency: int = None, resolution_bits: int = None) -> pwm_outputCreate an independent PWM output.
The firmware allocates a native PWM channel automatically. Keep the returned object and call methods on it; users do not choose or remember channel numbers.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | GPIO output pin connected to the PWM device. |
frequency |
int |
positional or keyword | No | Optional PWM frequency in hertz. Defaults to 5000. |
resolution_bits |
int |
positional or keyword | No | Optional duty resolution in bits. Defaults to 8. |
pwm_output pwm_output object.
pwm.open().write(duty) -> bool
pwm.open().write(duty) -> boolSet the raw integer duty value for the configured resolution.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
duty |
any |
positional or keyword | Yes | Required value. |
pwm.open().write_percent(percent) -> bool
pwm.open().write_percent(percent) -> boolSet duty cycle from 0 through 100 percent.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
percent |
any |
positional or keyword | Yes | Required value. |
pwm.open().read() -> int
pwm.open().read() -> intReturn the last raw duty value written by this object.
pwm.open().fade(duty, duration_ms) -> bool
pwm.open().fade(duty, duration_ms) -> boolFade to a raw duty value over duration_ms.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
duty |
any |
positional or keyword | Yes | Required value. |
duration_ms |
int |
positional or keyword | Yes | Required value. |
pwm.open().fade_percent(percent, duration_ms) -> bool
pwm.open().fade_percent(percent, duration_ms) -> boolFade to a duty percentage over duration_ms.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
percent |
any |
positional or keyword | Yes | Required value. |
duration_ms |
int |
positional or keyword | Yes | Required value. |
pwm.open().set_freq(frequency) -> bool
pwm.open().set_freq(frequency) -> boolChange this output's PWM frequency.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
frequency |
int |
positional or keyword | Yes | Required value. |
pwm.open().get_freq() -> int
pwm.open().get_freq() -> intReturn this output's configured frequency in hertz.
pwm.open().get_resolution() -> int
pwm.open().get_resolution() -> intReturn this output's duty resolution in bits.
pwm.open().stop() -> bool
pwm.open().stop() -> boolStop the signal and keep the output configured.
pwm.open().detach() -> bool
pwm.open().detach() -> boolStop the signal, release the pin, and free the PWM resource. Invalid pins, duplicate PWM pins, unavailable PWM resources, and unsupported frequency/resolution combinations raise a clear ValueError.