KryonOSDocs
Hardware & Peripherals

Hardware PWM

LEDC Pulse-Width Modulation channels, frequency, and duty cycle control.

12. Hardware PWM (Pulse-Width Modulation) API (PWM / System.pwm)

KryonOS exposes the ESP32 / ESP32-S3 hardware LEDC peripheral (8 independent hardware channels on ESP32-S3, 16 on ESP32) directly to JavaScript. This provides high-frequency, jitter-free hardware PWM for LED dimming, motor speed control, audio tones / buzzers, and RC servo positioning.

12.1 Hardware Specifications & Limits

  • Channels: 8 hardware channels on ESP32-S3 (0–7), 16 channels on ESP32 (0–15).
  • Resolution: 1 to 14 bits (e.g., 8-bit = 0–255, 10-bit = 0–1023, 14-bit = 0–16383).
  • Frequency Range: 1 Hz to 40 MHz.
  • Hardware Constraint: Max Freq = 80,000,000 / 2^resolution. (For example, at 14-bit resolution, maximum frequency is ≈ 4882 Hz).
  • Smart Timer Allocation: Automatically distributes allocations across independent hardware timers (0, 2, 4, 6 before 1, 3, 5, 7) to prevent frequency collisions between 50 Hz servos and 5 kHz LEDs.

12.2 API Reference Table

API MethodParametersReturn TypeDescription
PWM.setup(pin, [freq=5000], [resolution=8], [channel])pin: Number, freq: Number, resolution: Number (1–14), channel: Number (optional)NumberConfigures hardware channel on GPIO pin. Returns allocated channel ID 0–7 (or -1 on validation error).
PWM.write(pin, duty)pin: Number, duty: Number (0 to $2^{\text{res}} - 1$)BooleanWrites raw integer duty cycle count directly to hardware channel.
PWM.setDuty(pin, percent)pin: Number, percent: Number (0.0 to 100.0)BooleanSets duty cycle by float percentage.
PWM.setFrequency(pin, freq)pin: Number, freq: Number (Hz)BooleanDynamically updates the channel's output frequency on the fly.
PWM.setTone(pin, freq, [durationMs])pin: Number, freq: Number (Hz), durationMs: Number (optional)BooleanGenerates 50% duty square wave at freq for buzzers/speakers. If durationMs is provided, automatically stops when time expires.
PWM.stopTone(pin)pin: NumberBooleanMutes tone generation on the specified pin (sets duty to 0).
PWM.setServo(pin, angle, [minUs=500], [maxUs=2500])pin: Number, angle: Number (0–180), minUs: Number, maxUs: NumberBooleanAutomatically tunes pin to 50 Hz, 14-bit resolution and sets exact pulse width for RC servo motors.
PWM.detach(pin)pin: NumberBooleanStops output, detaches pin, and frees the hardware LEDC channel back to the pool.
PWM.getChannel(pin)pin: NumberNumberReturns currently assigned hardware channel ID for pin, or -1 if not active.
PWM.reset()NoneundefinedDetaches all active PWM pins and mutes all outputs.

Note: All functions are also accessible via System.pwm.* (e.g. System.pwm.setDuty(18, 75)).

12.3 Usage Examples

LED Dimming & Fading

var ledPin = 18;
PWM.setup(ledPin, 5000, 8); // 5 kHz, 8-bit resolution (0..255)

// Fade In
for (var p = 0; p <= 100; p += 5) {
    PWM.setDuty(ledPin, p);
    System.delay(20);
}

Piezo Buzzer Melody & Sound Effects

var buzzerPin = 19;

// Play A4 (440Hz) for 500 milliseconds (auto-stops)
PWM.setTone(buzzerPin, 440, 500);

// Play an ascending arpeggio
var notes = [261, 330, 392, 523]; // C4, E4, G4, C5
for (var i = 0; i < notes.length; i++) {
    PWM.setTone(buzzerPin, notes[i], 150);
    System.delay(180);
}

RC Servo Motor Positioning

var servoPin = 21;

// Center servo at 90 degrees
PWM.setServo(servoPin, 90);
System.delay(500);

// Move to 0 degrees, then 180 degrees
PWM.setServo(servoPin, 0);
System.delay(400);
PWM.setServo(servoPin, 180);
System.delay(400);

// Detach when done to save power
PWM.detach(servoPin);