PWM generator

Dim an LED or drive a motor with a duty-cycled pulse.

When to use it

Pulse-width modulation fakes an analog level from a digital pin: switch fast and vary the fraction of time the output is high. It's how you dim an LED, set a servo, or drive a motor from an FPGA, no DAC required.

How it works

A counter ramps 0 → 2^W-1 over and over, and the output is high while the counter is below duty. So duty sets the on-fraction: 0 is always off, max is always on. The demo uses duty = 5 of 16, about 31%.

Gotchas

Try it

Runs on our simulator, edit the code and press Run to see the waveform.

Open & fork in the playground → Ship the verified version: lfpga add lfpga_pwm

The runnable example above is a teaching version. The libfpga library ships a hardened, parameterised, CI-tested module (lfpga_pwm) for production use.

Next recipe: Synchronous FIFO