Skip to content

feat(button): Add AC frequency detection switch support (Moes MS-104B)#2658

Open
igorufox wants to merge 3 commits into
xoseperez:devfrom
igorufox:ac-frequency-switch
Open

feat(button): Add AC frequency detection switch support (Moes MS-104B)#2658
igorufox wants to merge 3 commits into
xoseperez:devfrom
igorufox:ac-frequency-switch

Conversation

@igorufox
Copy link
Copy Markdown

Description

This PR introduces support for AC Frequency Detection Switches.

Some devices, such as the Moes MS-104B (2-Gang Switch Module), do not use standard logic levels (High/Low) to represent the state of the physical wall switch. Instead, they use an optocoupler connected to the AC mains.

  • Switch Closed: The GPIO pin receives a 50Hz/60Hz pulsed signal (zero-crossing detection).
  • Switch Open: The GPIO pin is floating or pulled high/low (no pulses).

Standard debounce logic interprets these pulses as rapid button presses. This PR adds a new AcSwitch module that counts pulses within a time window to determine the stable state (ON/OFF).

Key Changes

  1. New AcSwitch Class:

    • Uses interrupts to count pulses on the GPIO pin.
    • Implements a latching mechanism (software filter) to ignore noise and contact bounce, ensuring the relay doesn't toggle multiple times during a single switch transition.
    • Configurable timeout and minimum pulse threshold.
  2. Button Module Integration:

    • Added BUTTON_FREQUENCY flag to types.h.
    • Updated button.cpp to initialize AcSwitch when BUTTON_FREQUENCY is set in the config.
    • Added a runtime setting btnAcDbg to enable specific debug logs for AC detection tuning without recompiling.

Configuration

To use this feature, define the button config as BUTTON_FREQUENCY.

Example platformio.ini / hardware.h:

// Example for Moes MS-104B
#define BUTTON1_PIN 13
#define BUTTON1_CONFIG BUTTON_FREQUENCY
#define BUTTON1_RELAY 1
#define BUTTON1_PRESS BUTTON_ACTION_TOGGLE
#define BUTTON1_RELEASE BUTTON_ACTION_TOGGLE
#define BUTTON1_DEBOUNCE_DELAY 100 // 100ms window is recommended for 50Hz

Igors Patrejevs added 2 commits December 29, 2025 20:21
This commit introduces a software filter for the `AcSwitch` module to improve reliability by rejecting noise pulses and leakage currents.

Key changes:
*   **Frequency Filtering:** The ISR now validates pulse duration against the expected period for 50Hz or 60Hz mains frequency (with a ±3.5% tolerance). Pulses outside this range are ignored.
*   **Configuration:** Added `btnAcFreq` setting (and `BUTTON_AC_FREQ` build flag) to define the expected mains frequency. Setting it to 0 disables the filter.
*   **Debug Improvements:** Enhanced `btnAcDbg` output to show pulse period statistics (min/max/avg) and valid pulse counts, aiding in diagnostics.
*   **Integration:** Updated `Button` module to pass the frequency configuration to `AcSwitch`.

This addresses issues where noise or leakage current (e.g., from pilot lights) could be misinterpreted as a valid signal on AC-coupled inputs.
Comment thread code/espurna/ac_switch.cpp Outdated
Comment thread code/espurna/button.cpp Outdated
@@ -0,0 +1,40 @@
; User defined configuration for Moes MS-104B 2-Gang Switch Module

[env:moes-ms-104b]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

cfg below usually goes into config/hardware.h, .ini flags are then replaced with just -DMOES_MS_104B

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