pwm
ts
import {PWM} from 'mikrojs/pwm'Control PWM output for LED dimming, motor speed control, servo positioning, and other analog-like outputs.
Usage
ts
import {PWM} from 'mikrojs/pwm'
const led = new PWM(20, {freq: 5000, duty: 0.5})
// Fade from current duty to 100% over 2 seconds
await led.fade(1.0, 2000)
led.end()Constructor
new PWM(pin, options)
ts
new PWM(pin: number, options: PWMOptions)Creates a PWM output on the given GPIO pin.
Parameters:
pin: GPIO pin numberoptions: see PWMOptions
Methods
pwm.duty(value?)
ts
duty(value?: number): Result<number, PWMError>Get or set the duty cycle (0.0–1.0). Called without arguments, returns the current duty. Called with a value, sets it and returns the new value.
ts
led.duty(0.75) // set to 75%
const current = led.duty().orPanic('Failed to read duty')pwm.freq(value?)
ts
freq(value?: number): Result<number, PWMError>Get or set the frequency in Hz. Same get/set pattern as duty().
pwm.fade(targetDuty, durationMs)
ts
fade(targetDuty: number, durationMs: number): Promise<Result<void, PWMError>>Hardware-accelerated fade to the target duty cycle over the given duration. This uses the ESP32's LEDC hardware fading, so the fade runs without CPU involvement.
ts
await led.fade(0, 1000) // fade to off over 1 secondpwm.end()
ts
end(): Result<void, PWMError>Stops the PWM output and releases the hardware channel.
Types
PWMOptions
ts
interface PWMOptions {
freq: number // frequency in Hz
duty?: number // initial duty cycle, 0.0–1.0 (default: 0)
}Errors
PWMError
| Variant | Fields | Description |
|---|---|---|
NotActive | — | PWM has been ended |
DutyFailed | message | Failed to set duty cycle |
FreqFailed | message | Failed to set frequency |
FadeFailed | message | Hardware fade failed |