Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4227

Troubleshooting • Reading a simple GPIO push button from NodeJS on a Raspberry Pi 5

$
0
0
I'm trying to read a simple momentary push button on a Raspberry Pi 5 using NodeJS but after an entire weekend of Googling, trying all libraries and reading a lot of forum posts, I can't get it to work.

This Python script is working perfectly fine:

Code:

from gpiozero import Buttonbutton = Button(2)while True:    button.wait_for_press()    print("Pressed")    button.wait_for_release()    print("Released")
However in NodeJS I keep getting errors using the onoff (or any other) module:

Code:

const Gpio = require('onoff').Gpio;const button = new Gpio(2, 'in', 'both');button.watch((error, value) => {    console.log(error);    console.log(value);});process.on('SIGINT', _ => {    button.unexport();});
I also tried other GPIO pin numbers but the script either errors or logs nothing. Anyone else using NodeJS with GPIO? Thanks

Statistics: Posted by lapidus — Mon Feb 24, 2025 9:26 am



Viewing all articles
Browse latest Browse all 4227

Trending Articles