Skip to content

Fix bounce_time debouncing for brief signals in DigitalInputDevice#137

Merged
pjbRPF merged 11 commits into
devfrom
copilot/fix-bounce-time-sound-sensor
Nov 28, 2025
Merged

Fix bounce_time debouncing for brief signals in DigitalInputDevice#137
pjbRPF merged 11 commits into
devfrom
copilot/fix-bounce-time-sound-sensor

Conversation

Copilot AI commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

bounce_time parameter doesn't work with sensors that produce brief pulses (e.g., sound sensors). Setting bounce_time=1 prevents detection entirely instead of debouncing multiple rapid triggers.

Root Cause

Original implementation blocks in a loop waiting for signal stability for the entire bounce period:

# Original code - waits for signal to stop changing
stop = ticks_ms() + (bounce_time * 1000)
while ticks_ms() < stop:
    if p.value() != last_state:
        stop = ticks_ms() + (bounce_time * 1000)  # Reset timer on any change
        last_state = p.value()

For brief pulses (sound sensor HIGH for 50ms with bounce_time=1):

  • Interrupt fires, signal is HIGH
  • Loop waits for 1 second of stability
  • Signal returns to LOW within 100ms
  • After 1 second of LOW being stable, code sees: initial_state (LOW) == final_state (LOW)
  • No callback fired

Changes

Replaced blocking stability check with timestamp-based debouncing:

  • Track _last_callback_ms instead of blocking
  • Fire callback immediately if enough time passed since last callback
  • Always update _state to track reality, but suppress callbacks within bounce period
  • Non-blocking: interrupt handler returns immediately
# New approach - timestamp-based
time_since_last_callback = current_time_ms - self._last_callback_ms
if bounce_time is None or time_since_last_callback >= (bounce_time * 1000):
    self._state = new_state
    self._last_callback_ms = current_time_ms
    # Fire callback
else:
    self._state = new_state  # Update state, suppress callback

Result: First pulse fires callback, subsequent pulses within bounce_time are ignored. Works for both mechanical bounce (buttons) and brief signals (sensors).

Original prompt

This section details on the original issue you should resolve

<issue_title>bounce_time not working when using with DigitalInputDevice</issue_title>
<issue_description>Describe the bug
When using a sound sensor (like this one: Sound Sensor) as an DigitalInputDevice everything is working as expected. But since you will get a dozen signals from one hand clap, you might use bounce_time=1. But then no signal is detected at all.

To Reproduce
Steps to reproduce the behaviour:

  1. Install a sound sensor on Pin 15.
  2. Run the code below.
  3. Nothing ist detected.
  4. No error is detected.
from picozero import DigitalInputDevice
from time import sleep

sound_sensor = DigitalInputDevice(15, bounce_time = 1)

def detected():
    print("Sound detected!")
    
sound_sensor.when_activated = detected

while True:
    sleep(1)

Expected behaviour
You whould expect, that "Sound detected" ist getting printed exactly one time after a noise like a hand clap.

System information:

  • OS: Windows
  • Development environment: Thonny
  • picozero version: 0.42

Additional context
Everythong is working when you don't add bounce_time. But now you will get printed "Sound detected" several times.
</issue_description>

<agent_instructions>Fix</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits November 25, 2025 16:09
Co-authored-by: pjbRPF <104009652+pjbRPF@users.noreply.github.com>
Co-authored-by: pjbRPF <104009652+pjbRPF@users.noreply.github.com>
Co-authored-by: pjbRPF <104009652+pjbRPF@users.noreply.github.com>
Co-authored-by: pjbRPF <104009652+pjbRPF@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bounce_time issue with DigitalInputDevice Fix bounce_time debouncing for brief signals in DigitalInputDevice Nov 25, 2025
Copilot AI requested a review from pjbRPF November 25, 2025 16:22
@pjbRPF pjbRPF marked this pull request as ready for review November 25, 2025 16:42
@pjbRPF pjbRPF requested a review from MarcScott as a code owner November 25, 2025 16:42
@pjbRPF pjbRPF changed the base branch from main to dev November 28, 2025 11:22
@pjbRPF pjbRPF merged commit 9d03da0 into dev Nov 28, 2025
@pjbRPF pjbRPF deleted the copilot/fix-bounce-time-sound-sensor branch November 28, 2025 13:30
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.

bounce_time not working when using with DigitalInputDevice

4 participants