Sitelet https://github.com/gpiozero/gpiozero/issues/1189
Skip to content

Problem with two Input Devices interacting #1189

Description

@ianc5040

Operating system: Raspbian Bookworm
Python version: e.g. 3.11.2
Pi model: e.g. Pi 1 Model B Rev 1.2 and Pi 3 Model B
GPIO Zero version: e.g. 2.0.1
Pin factory used: e.g. LGGPIO

I am having a problem when using a function called from a GPIO Zero Button when_pressed event and a second GPIO Zero Input Device is used.

I originally discovered this problem in a situation where I am using a Button and also reading from an ADC. The latter uses an RPi pin that needs to change from an output to an input and the change to an InoutDevice seems to block any Button presses. I have been able to replicate the problem with minimal code using two Buttons as described below.

If I create two Buttons and define when_pressed events for each of them, everything works as expected and a message is printed immediately whenever either button is pressed.

from time import sleep
from gpiozero import Button

def Button18Press() :
    print("Button 18 pressed")
def Button27Press() :
    print("Button 27 pressed")
    
button18 = Button(18)
button18.when_pressed = Button18Press
button27 = Button(27)
button27.when_pressed = Button27Press

while True :
    print("Loop")
    sleep(5)

If I create two Buttons but only define the when_pressed event for one of them and immediately close the second button, pressing the first button does not produce any output. It seems that pressing the button is being blocked.

from time import sleep
from gpiozero import Button

def Button18Press() :
    print("Button 18 pressed")
    
button18 = Button(18)
button18.when_pressed = Button18Press
button27 = Button(27)
button27.close()

while True :
    print("Loop")
    sleep(5)

If I create one Button and define the when_pressed event for it and then create and close the second Button in the while loop, the first button presses are not reported immediately but are "stored" until the next time that the second Button is created. If the first button is pressed several times then all the presses are stored and reported when the second Button is created.

from time import sleep
from gpiozero import Button

def Button18Press() :
    print("Button 18 pressed")
    
button18 = Button(18)
button18.when_pressed = Button18Press

while True :
    button27 = Button(27)
    button27.close()
    print("Loop")
    sleep(5)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions