Skip to content

PWM LED ​

A "breathing" LED effect: smoothly fades an LED in and out using PWM. See the pwm API reference for the full type surface.

Hardware ​

  • Any ESP32 board with a GPIO pin that supports PWM
  • An LED connected through a 220-ohm resistor
  • USB cable

Code ​

ts
import {
Pwm
} from 'mikro/pwm'
// GPIO 15 is the built-in LED on XIAO ESP32C6. Replace with your board's LED pin. const
LED_PIN
= 15
const
led
=
Pwm
(
LED_PIN
, {
freq
: 50,
duty
: 0}).
orPanic
('Failed to set up the LED')
// Breathe: smoothly fade in and out forever while (true) { const
fadeIn
= await
led
.
fade
(1.0, 1000)
if (!
fadeIn
.
ok
) {
console
.
error
('Fade in failed:',
fadeIn
.
error
)
break } const
fadeOut
= await
led
.
fade
(0.0, 1000)
if (!
fadeOut
.
ok
) {
console
.
error
('Fade out failed:',
fadeOut
.
error
)
break } }

Walkthrough ​

  1. PWM setup. Pwm(gpio, {freq, duty}) claims the GPIO pin, creates a PWM channel on it and returns a Result with the handle. freq sets the PWM frequency in Hz; duty sets the initial duty cycle (0.0 to 1.0).

  2. Hardware fading. led.fade(target, durationMs) smoothly transitions the duty cycle to target over durationMs milliseconds. This runs in hardware on the ESP32's LEDC peripheral, so it does not block the event loop.

  3. Error handling. Both fade() calls return a Promise<Result> (see Result). If something goes wrong (for example an invalid pin), the loop breaks with a clear error message.

Create project ​

sh
pnpm create mikro --template pwm-led
sh
npm create mikro -- --template pwm-led
sh
yarn create mikro --template pwm-led
sh
bun create mikro --template pwm-led

Run it ​

sh
pnpm install
pnpm mikro flash  # only needed once per board
pnpm mikro dev
sh
npm install
npx mikro flash  # only needed once per board
npx mikro dev
sh
yarn install
yarn mikro flash  # only needed once per board
yarn mikro dev
sh
bun install
bunx mikro flash  # only needed once per board
bunx mikro dev

The LED fades smoothly in and out in a continuous breathing pattern.

View source on GitHub

Mikro.js is built with AI assistance, code and docs alike. Read the AI disclosure.