Skip to main content

fabricgate / pwmVerified

PWM LED brightness controller and period timer for nanopynq

License: MITAuthor: FabricGate Project
fabricgate pull fabricgate/pwm:1.0.0 --platform tangnano20k/nanopynq

Versions

Platforms (v1.0.0)

tangnano20k/nanopynqtangnano9k/nanopynq

PWM LED

A PWM timer that sets the brightness of LED 5. You write two numbers — the period and the high time — and the circuit keeps toggling the LED on its own, far too fast to see, so it looks dimmer or brighter. It also counts completed periods, so you can use it as a hardware timer.

Built with nanopynq. Nothing to install: in Chrome or Edge, on this version's page, select your board, press Open the panel, then Write to the board, Connect, and Open the notebook.

What is on the board

IPAddressWhat it is
pwm0x7000the PWM timer, driving LED 5
leds0x1000LEDs 0–4
buttons0x2000buttons S1 and S2
sys0x0design information

The registers of pwm (byte offsets):

OffsetNameAccessMeaning
0x0ctrlrwbit 0: run. Writing 0 turns LED 5 off and clears count and wraps
0x4periodrwclocks per period (the board clock is 27 MHz)
0x8dutyrwclocks per period that LED 5 is on (0: off, period or more: fully on)
0xccountroposition in the current period, 0 to period - 1
0x10wrapsroperiods completed since ctrl was set to 1

A new period or duty takes effect when the next period starts, so the LED never gets a short pulse.

Try it

The first cell of the notebook writes ctrl=1, period=2, duty=3. Because duty is not less than period, LED 5 is fully on.

Dim LED 5 to 10 % at 1 kHz:

ol.pwm.write(0x4, 27000)   # period: 27 MHz / 27000 = 1 kHz
ol.pwm.write(0x8, 2700)    # duty: 10 % of the period
ol.pwm.write(0x0, 1)       # run
ol.pwm.register_map

Fade it in. Press Stop to end the loop early; the LED keeps its last brightness:

import time
for d in range(0, 27001, 900):
    ol.pwm.write(0x8, d)
    time.sleep(0.05)

Use it as a timer: while period stays the same, the time between two reads of wraps is their difference × period / 27,000,000 seconds (wraps keeps counting when period changes; write ctrl=0 and then ctrl=1 to reset it to 0):

import time
w = ol.pwm.read(0x10)
time.sleep(1)
(ol.pwm.read(0x10) - w) * ol.pwm.read(0x4) / 27_000_000   # about 1.0

Turn it off with ol.pwm.write(0x0, 0).

From local Python with pip install "nanopynq[fabricgate]":

from nanopynq import Overlay

ol = Overlay("fabricgate://fabricgate/pwm:1.0.0", board="tangnano9k")  # or board="tangnano20k"

License

MIT License. Copyright (c) 2026 FabricGate Contributors.