Skip to content

Keep the analogWriteRange() value when PWM settings are recomputed - #3608

Merged
earlephilhower merged 2 commits into
earlephilhower:masterfrom
emincan2000:analogwrite-keep-range
Nov 12, 2026
Merged

earlephilhower merged 2 commits into
earlephilhower:masterfrom
emincan2000:analogwrite-keep-range

Conversation

@emincan2000

Copy link
Copy Markdown
Contributor

analogWrite() doubles or halves analogScale in place to keep the clock divider between 1 and 255. This loses the range set with analogWriteRange(). When analogWriteFreq() later triggers a recomputation, the adjustment starts from the already adjusted analogScale, even though the values passed to analogWrite() are still in the user's range. With the default range of 255 and a 125 MHz clock, analogWrite(pin, 255) gives 100 % duty at 1 kHz, 50 % after analogWriteFreq(500), and 25 % after analogWriteFreq(200). Later changes can push it lower still. The analogWriteFreq(f + 1); analogWriteFreq(f) workaround mentioned in #2901 makes it worse for the same reason.

This change stores the requested range in a separate variable, analogRange, and starts from that value whenever the scale is recomputed. analogWriteRange() also compares against the requested range.

On first use, the duty and frequency match the previous behavior for every combination of range (3 to 65535) and frequency (100 Hz to 10 MHz) I tried, 168 cases. For the sequence above, analogWrite(pin, 255) stays at 100 % after each frequency change. I do not have a board with me, so I checked this with a line by line Python model of analogWrite(), analogWriteFreq() and analogWriteRange().

emincan2000 and others added 2 commits November 7, 2026 02:28
analogWrite() adjusts analogScale in place (doubling or halving it to
keep the clock divider between 1 and 255) and then forgets the range
the user asked for. When analogWriteFreq() later marks the scale for
recomputation, the adjustment starts from the already adjusted value,
so the shift applied to the user's values no longer matches: with the
default range of 255 at 125 MHz, analogWrite(pin, 255) gives 100 % at
1 kHz, 50 % after analogWriteFreq(500) and 25 % after 200 Hz.

Keep the requested range in its own variable and start the adjustment
from it each time.

@earlephilhower earlephilhower left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This was one of the first things I wrote 5 yrs+ ago for the core and it does look like I conflated the user scale vs. the PWM device scale a bit. My naming choices weren't the best, either.

Looks like a good update to me!

@earlephilhower
earlephilhower merged commit d955d3b into earlephilhower:master Nov 12, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants