(自分メモ)
ラズパイのI/O操作サンプル。
環境
- Raspberry Pi 4
- Ubuntu 20.04
- Node.js 22
Javascript Project準備
npm init -y
npm i node-web-gpio
I/O操作
main.js - サンプルコード
const { requestGPIOAccess } = require("node-web-gpio");
const { promisify } = require("util");
const sleep = promisify(setTimeout);
async function main() {
const gpioAccess = await requestGPIOAccess();
const port = gpioAccess.ports.get(26);
await port.export("out");
for (;;) {
await port.write(1);
await sleep(1000);
await port.write(0);
await sleep(1000);
}
}
main()
Build/Run
npm i
sudo node main.js
※GPIO 26は37番ピン