Repository navigation
Keep the analogWriteRange() value when PWM settings are recomputed - #3608
Merged
earlephilhower merged 2 commits intoNov 12, 2026
Merged
Conversation
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
approved these changes
Nov 12, 2026
earlephilhower
left a comment
Owner
There was a problem hiding this comment.
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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().