Menu

#197 Callback Function - Strange Behaviour

New
nobody
None
Critical
Other
2021-04-25
2021-04-25
No

First, a big "Thank you" to Ben Croston for the excellent RPi.GPIO toolset.

I am running RPi.GPIO (V 0.7.0) on a Pi 3B+ (Linux raspberrypi 5.4.51-v7+ #1333). I have noticed strange behaviour with a simple callback function (please see Thonny screenshot below).

I have two pushbuttons connected to the Pi. One at pin 8 allows me to generate a rising edge; the other at pin 10 allows me to generate a falling edge. I have an LED connected to pin 16. The callback function is intended to print 'edge detected' followed by a 500ms pause, then turn on the LED for 500ms then immediately turn off the LED. So, the total duration of the function is just slightly over 1000ms.

At line 18, I set the callback event to trigger on the rising edge at pin 8. If I set the bouncetime parameter to 1000ms or less, pressing the pushbutton causes the callback function to run twice. In other words, the program outputs 'edge detected' followed by one LED flash, followed again by 'edge detected' followed by another flash. I get exactly the same behaviour if I set line 18 to detect a falling edge at pin 10 (again with bouncetime set to 1000ms or less). In both cases, the pushbutton was pressed only briefly (for less than half a second).

This behaviour appears to be similar to that described in ticket #121 (add_event_detect callback is called 2 times per detect).

If I set the bouncetime parameter to 1010ms, i.e., just slightly longer than the duration of the callback function, the program behaves properly (for either edge), i.e., the function generates 'edge detected' followed by just one LED flash.

If I replace the pushbuttons with 250ms pulses generated by a signal generator, the program behaves perfectly even with bouncetime set as low as 1ms. See screenshots 'falling edge.bmp' and 'rising edge.bmp'.

Summary:

  1. Setting the bouncetime to a value just less than the total duration of the callback routine causes the function to execute twice, even though the pushbutton was pressed very briefly. This happens for either edge.

  2. Setting the bouncetime to a value just greater than the total duration of the callback routine results in proper behaviour (function runs just once).

  3. The problem at (1) is definitely caused by contact bounce because replacing the switches with electronic pulses results in proper behaviour, even with bouncetime set at just 1ms.

I'd be much obliged if you could look into this strange behaviour.
Thank you.
Tony Smith.

3 Attachments

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.