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:However in NodeJS I keep getting errors using the onoff (or any other) module:I also tried other GPIO pin numbers but the script either errors or logs nothing. Anyone else using NodeJS with GPIO? Thanks
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")
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();});
Statistics: Posted by lapidus — Mon Feb 24, 2025 9:26 am