Pi GPIO for Node-RED
The node-red-node-pi-gpio package provides a set of Node-RED nodes for interacting with the General Purpose Input/Output (GPIO) pins of a Raspberry Pi. Utilizing the `RPi.GPIO` Python library, it enables flow-based control and monitoring of digital and Pulse Width Modulation (PWM) outputs, as well as digital inputs. The current stable version is 2.0.7. Releases appear to follow a milestone-based cadence rather than a strict schedule, with major version updates incorporating new features or significant changes. Key differentiators include built-in support for mouse and keyboard input detection and direct integration into the Node-RED visual programming environment, simplifying hardware interaction for IoT and automation projects on Raspberry Pi devices. It specifically notes an alternative, `node-red-node-pi-gpiod`, for more accurate servo control, indicating this package is better suited for general purpose digital and basic PWM applications.
Common errors
-
Error: RPi.GPIO module not found
cause The Python `RPi.GPIO` library is not installed or not accessible to the user running Node-RED.fixInstall the Python `RPi.GPIO` library using `sudo pip install RPi.GPIO`. Ensure `python-pip` and `python-dev` are also installed (`sudo apt-get install python-pip python-dev`). -
Permission denied to /dev/gpiomem (or similar GPIO access error)
cause The user running Node-RED lacks the necessary permissions to directly access the GPIO pins on the Raspberry Pi.fixAdd the Node-RED user (or the user running Node-RED) to the `gpio` group and ensure correct `udev` rules are in place. Run: `sudo addgroup gpio`, `sudo chown root:gpio /dev/gpiomem`, `sudo adduser $USER gpio`, `echo 'KERNEL=="gpiomem", NAME="%k", GROUP="gpio", MODE="0660"' | sudo tee /etc/udev/rules.d/45-gpio.rules`, then `sudo udevadm control --reload-rules && sudo udevadm trigger`. Replace `$USER` with the actual user Node-RED runs as. -
PWM output is erratic or inaccurate, especially with servos.
cause The `node-red-node-pi-gpio` package uses software-driven PWM which can be affected by system load and scheduling, leading to timing jitters.fixFor applications requiring high precision PWM (like servo control), switch to `node-red-node-pi-gpiod`. This package uses the `pigpiod` daemon for hardware-timed PWM, offering significantly better accuracy.
Warnings
- breaking This package relies on the `RPi.GPIO` Python library and specific system permissions. On non-Raspbian operating systems (e.g., Ubuntu, Debian on a Pi), you *must* manually install `python-pip`, `python-dev`, the `RPi.GPIO` Python package, configure `udev` rules, and add your user to the `gpio` group. Failure to do so will result in GPIO access errors.
- gotcha For applications requiring highly accurate or fine-grained servo control, this package's PWM mode may not be sufficient. The underlying `RPi.GPIO` library can have timing inaccuracies for demanding PWM tasks.
- gotcha When configuring GPIO pins, ensure you are using the correct BCM GPIO number, not the physical pin number. The Node-RED configuration diagram shows pin numbers for reference, but the input field expects the Broadcom (BCM) GPIO numbering scheme.
- gotcha The `rpi gpio out` node expects different `msg.payload` types depending on whether it's configured for Digital or PWM mode. Sending the wrong type or range can lead to unexpected behavior.
Install
-
npm install node-red-node-pi-gpio -
yarn add node-red-node-pi-gpio -
pnpm add node-red-node-pi-gpio
Imports
- Pi GPIO Input Node
Install via Node-RED palette manager or `npm i node-red-node-pi-gpio`
- Pi GPIO Output Node
Install via Node-RED palette manager or `npm i node-red-node-pi-gpio`
- Pi GPIO Keyboard/Mouse Node
Install via Node-RED palette manager or `npm i node-red-node-pi-gpio`
Quickstart
/*
This quickstart demonstrates how to install 'node-red-node-pi-gpio' and describes a basic Node-RED flow setup.
Since this is a Node-RED node package, direct JavaScript runtime usage is not applicable.
The instructions guide you through setting up a simple input-to-debug and inject-to-output flow in the Node-RED editor.
*/
// Step 1: Install the Node-RED node package.
// You should run this command in your Node-RED user directory (typically ~/.node-red)
// or use the 'Manage Palette' option within the Node-RED editor.
console.log("Executing installation command:\n npm i node-red-node-pi-gpio\n");
// After installation, restart Node-RED if prompted or if nodes do not appear in the palette.
console.log("Installation complete. If nodes are not visible, restart Node-RED (e.g., 'node-red-start').\n");
// Step 2: Configure a basic flow in the Node-RED editor (usually accessible at http://localhost:1880).
console.log("--- Node-RED Flow Configuration Guide ---");
console.log("1. Drag an 'rpi gpio in' node from the palette onto your flow canvas.");
console.log(" - Double-click the 'rpi gpio in' node: Select a BCM GPIO number (e.g., 4) and configure pull-up/down resistors if needed.");
console.log("2. Drag a 'debug' node onto the canvas.");
console.log(" - Connect the output of the 'rpi gpio in' node to the input of the 'debug' node.");
console.log(" - When the state of GPIO 4 changes, you will see '0' or '1' in the debug sidebar.");
console.log("3. Drag an 'inject' node onto the canvas.");
console.log(" - Double-click the 'inject' node: Set 'Payload' to a number (e.g., 1 for digital high, or 50 for 50% PWM).");
console.log("4. Drag an 'rpi gpio out' node onto the canvas.");
console.log(" - Double-click the 'rpi gpio out' node: Select a BCM GPIO number (e.g., 17) and choose 'Digital output' or 'PWM output' mode.");
console.log("5. Connect the output of the 'inject' node to the input of the 'rpi gpio out' node.");
console.log(" - Clicking the 'inject' node's button will now set the state of GPIO 17.");
console.log("6. Click the 'Deploy' button in the Node-RED editor to activate your flow.");
// Note: On non-Raspbian systems, additional Python library and Udev rules setup is required. (Refer to warnings for details).