Dim an LED (PWM)
An LED can't be half on, but it can be on half the time. Switch it fast enough and your eye sees a dimmer light: that's pulse-width modulation (PWM).
Run a 4-bit counter that goes round and round, and light LED 0 while the counter is below the switches' value. sw = 0 is off, sw = 8 is half brightness, sw = 15 is nearly full.
lab.v
drag to turn · scroll to zoom
Loading the board…
Press Run on the board: your Verilog is turned into gates in this browser and runs here, cycle by cycle.
Goals
- ○LED 0 is on for sw / 16 of the time
- ○The pattern repeats every 16 cycles (fast enough not to flicker)
Hints
always @(posedge clk) phase <= phase + 1;with a 4-bitphase.assign led[0] = phase < sw;
Pin log
Run the design to see its outputs change.
Put it on a real board
- The Sipeed Tang Nano 9K (Gowin GW1NR-9) has no switches: those ports go to header pins, where you can wire them.
- The clock is set to the board's 27 MHz, and its active-low buttons and LEDs are flipped for you.