[pulse_counter] Fix PCNT glitch filter calculation off by 1000x#14132
Merged
swoboda1337 merged 1 commit intoesphome:devfrom Feb 20, 2026
Merged
[pulse_counter] Fix PCNT glitch filter calculation off by 1000x#14132swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337 merged 1 commit intoesphome:devfrom
Conversation
The max_glitch_ns calculation used 1000000 (1e6) instead of 1e9 to convert PCNT_LL_MAX_GLITCH_WIDTH ticks to nanoseconds, making the computed maximum 1000x too small (12ns instead of 12787ns). This caused std::min to cap the user's requested filter (e.g. 13000ns for 13us) to just 12ns, which ESP-IDF then converted to 0 APB clock cycles — effectively disabling the glitch filter entirely. Use the same split-division approach as ESP-IDF's own pulse_cnt.c to stay in 32-bit arithmetic: multiply ticks by 1000, divide freq by 1M. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
|
To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file: external_components:
- source: github://pr#14132
components: [pulse_counter]
refresh: 1h(Added by the PR bot) |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes the ESP32 PCNT glitch filter maximum duration calculation in the pulse_counter component so user-configured internal_filter values are no longer incorrectly clamped ~1000× too small (which effectively disables the hardware glitch filter).
Changes:
- Correct
max_glitch_nsconversion from PCNT clock ticks to nanoseconds (uses10^9scaling via split-division instead of10^6). - Align the conversion approach with ESP-IDF’s integer-arithmetic strategy to avoid overflow while keeping the value in-range for
uint32_t.
bdraco
approved these changes
Feb 20, 2026
Member
Author
|
Thanks |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #14132 +/- ##
=======================================
Coverage 74.11% 74.11%
=======================================
Files 55 55
Lines 11590 11590
Branches 1578 1578
=======================================
Hits 8590 8590
Misses 2598 2598
Partials 402 402 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
swoboda1337
added a commit
that referenced
this pull request
Feb 20, 2026
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Merged
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
What does this implement/fix?
The
max_glitch_nscalculation used1000000(10^6) instead of 10^9 when convertingPCNT_LL_MAX_GLITCH_WIDTHticks to nanoseconds. This made the computed maximum 1000x too small (12ns instead of 12787ns), causingstd::minto cap the user's requested filter (e.g. 13000ns for 13µs) to just 12ns. ESP-IDF then converts that to 0 APB clock cycles — effectively disabling the glitch filter entirely.With the filter disabled, the PCNT hardware counts electrical noise as pulses, producing wildly inflated readings (e.g. ~280,000 pulses/min instead of ~1,010 on an ESP32-C6).
This fix uses the same split-division approach as ESP-IDF's own
pulse_cnt.c:404to stay in 32-bit arithmetic:Types of changes
Related issue or feature (if applicable):
Pull request in esphome-docs with documentation (if applicable):
Test Environment
Example entry for
config.yaml:Checklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: