I recently tried to use the GPIO of my Raspberry pi 4 with node js. I managed to get the program to work on my raspberry, but as soon as I wanted to put it on another raspberry, I was no longer able to make it work.
For this, I use the onoff library. The program is as follows:
var GPIO = require('onoff').Gpio;
var lightPin = new GPIO(23, 'out');
lightPin.writeSync(1)
But as soon as I run it, I get the following error:
node:internal/fs/utils:353
throw err;
^
Error: EINVAL: invalid argument, write
at Object.writeSync (node:fs:939:3)
at Object.writeFileSync (node:fs:2355:26)
at exportGpio (/home/pi4b/API/node_modules/onoff/onoff.js:18:8)
at new Gpio (/home/pi4b/API/node_modules/onoff/onoff.js:172:36)
at Object.<anonymous> (/home/pi4b/API/helper.js:3:16)
at Module._compile (node:internal/modules/cjs/loader:1369:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
at Module.load (node:internal/modules/cjs/loader:1201:32)
at Module._load (node:internal/modules/cjs/loader:1017:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:122:12) {
errno: -22,
syscall: 'write',
code: 'EINVAL'
}
Node.js v21.0.0
Version of node.js -> 21.0.0
(the same for both pi)
Version of npm -> 10.5.0
(the same for both pi)
Version of onoff -> onoff@6.0.3
(the same for both pi)
Debian version -> 12.5
(against 11.9 for the old pi)
For information, the node_modules
folder was completely deleted and reinstalled with npm i
.
I also tried with the library RPi.GPIO
in python which seemed to work correctly.
Thank you for your help.
It seems that Raspberry OS kernel addresses GPIO pins yet by another numbering scheme.
~ $ cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 512-569, parent: platform/fe200000.gpio, pinctrl-bcm2711:
...
gpio-532 (GPIO20 |sysfs ) in lo
gpio-533 (GPIO21 |sysfs ) in lo
gpio-534 (GPIO22 |sysfs ) in lo
gpio-535 (GPIO23 )
gpio-536 (GPIO24 )
gpio-537 (GPIO25 )
gpio-538 (GPIO26 |sysfs ) in lo
...
If you want to address the GPIO23 pin, you must use the number after gpio-[number]
In your case:
var GPIO = require('onoff').Gpio;
var lightPin = new GPIO(535, 'out');
lightPin.writeSync(1)