Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button.watch spams endlessly #203

Open
optikalefx opened this issue May 8, 2024 · 1 comment
Open

Button.watch spams endlessly #203

optikalefx opened this issue May 8, 2024 · 1 comment

Comments

@optikalefx
Copy link

optikalefx commented May 8, 2024

I feel like I'm doing something wrong - when I run the sample code below, I just get an endless log. When I hit the button, the log stops.

image
var Gpio = require('onoff').Gpio
var button = new Gpio(516, 'in', 'both');

button.watch((err, value) => console.log('button', Date.now(), value));

I could work around that in my code, but I don't think that's how it's supposed to work. Isn't it supposed to only log a value when the interrupt happens?

For reference, this is pi3b+ on node22. The button is a simple 2 pin button wired to GPIO4 and GND. Also, it works fine in Python using

from gpiozero import Button
button = Button(4)

button.wait_for_press()
print('You pushed me')

so I don't think it's hardware issue

@the-sutt
Copy link

Hey @optikalefx,

I'm a little late for the party, but - if you have not already solved it - I guess your PIN is floating! Meaning it has no defined HIGH or LOW value which triggers the library repeatedly, as the input value "floats" between HIGH and LOW.

The reason it works with the python library is that it uses a pull-up by default (meaning it holds the pin high until you "short" it to GND.

See their comment in the button-class of gpiozero:

    Connect one side of the button to a ground pin, and the other to any GPIO
    pin. Alternatively, connect one side of the button to the 3V3 pin, and the
    other to any GPIO pin, then set *pull_up* to :data:`False` in the
    :class:`Button` constructor.

The TLDR is if you use VCC-Button-PIN layout you must disable the pull_up.

Either configure a pull up or make your own hardware-pullup (usually a resistor around 10k is all you need).

Greetings

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

No branches or pull requests

2 participants