Skip to main content

pwm

Generate PWM (Pulse Width Modulation) signals for LED dimming, motor speed control, and servo positioning.

Overview

The pwm node generates Pulse Width Modulation signals for precise analog-like control of digital pins. Use it to dim LEDs, control motor speeds, position servos, and create variable outputs from digital GPIO pins.

0-100%
Duty Cycle Range
1MHz
Max Frequency
2 Ch
Hardware PWM
All
Software PWM

Hardware vs Software PWM

Hardware PWM (Recommended)

  • • Available on GPIO 12, 13, 18, 19 only
  • • Zero CPU overhead
  • • Precise, jitter-free signal
  • • Best for servos and motors

Software PWM

  • • Available on any GPIO pin
  • • Uses CPU cycles
  • • May have slight jitter
  • • Good for LED dimming

Properties

Property Type Required Default Description
pin number Yes - GPIO pin number (BCM numbering)
frequency number No 1000 PWM frequency in Hz
range number No 100 Range for duty cycle (0-range)
mode string No "auto" "hardware", "software", or "auto"
initialValue number No 0 Initial duty cycle on deploy

Inputs

msg.payload

Sets the duty cycle (0 to range value).

0 → 0% duty (off)
50 → 50% duty (half brightness/speed)
100 → 100% duty (full on)

Outputs

Confirmation

Passes through with current PWM state.

{
  "payload": 75,
  "topic": "pwm/18",
  "pin": 18,
  "_pwm": {
    "duty": 75,
    "frequency": 1000,
    "mode": "hardware"
  }
}

Common Applications

LED Dimming

Smooth brightness control

Frequency: 1000 Hz

Range: 100 (0-100%)

Mode: software (any pin)

DC Motor Speed

Variable speed control

Frequency: 20000 Hz

Range: 100

Mode: hardware (GPIO 18)

Servo Control

Position control (0-180°)

Frequency: 50 Hz

Range: 100 (2.5-12.5% duty)

Mode: hardware (GPIO 12/13)

Example Flows

Breathing LED Effect

Smoothly fade LED in and out continuously.

[
  {
    "id": "fade-timer",
    "type": "inject",
    "repeat": "0.05",
    "payload": ""
  },
  {
    "id": "fade-calc",
    "type": "function",
    "func": "var t = context.get('t') || 0;
t += 0.05;
context.set('t', t);
msg.payload = Math.round(50 + 50 * Math.sin(t));
return msg;"
  },
  {
    "id": "led-pwm",
    "type": "pwm",
    "pin": 18,
    "frequency": 1000
  }
]

Dashboard Brightness Control

Control LED brightness with a dashboard slider.

slider (0-100) pwm

Servo Control Guide

Standard servos use 50Hz PWM with pulse widths of 1-2ms (2.5-10% duty cycle) for 0-180° rotation.

// Convert angle (0-180) to duty cycle
function angleToDuty(angle) {
  // 0° = 2.5% duty, 180° = 12.5% duty
  return 2.5 + (angle / 180) * 10;
}

// Usage: Set servo to 90° (center)
msg.payload = angleToDuty(90); // Returns 7.5
return msg;

Tip: Use hardware PWM (GPIO 12, 13, 18, 19) for servos to avoid jitter.

Related Nodes