![](https://blog.arduino.cc/wp-content/uploads/2025/01/Control-your-Raspberry-PI-GPIO-using-Node.js_Arduino.cc-Blogpost-Cover-1100x600-1-1024x548.png)
As a Node.js developer, you’re in all probability keen to place your JavaScript abilities to work past the browser or server, diving into the world of {hardware} management with Raspberry Pi GPIOs. If that’s the case, you’re in the best place!
This text is the third a part of our collection, following A information to visualise your Raspberry Pi information on Arduino Cloud | Half I and the Python-focused Management your Raspberry Pi GPIO in Arduino Cloud utilizing Python | Half II, which launched GPIO administration. Now, it’s time to discover how Node.js will be your gateway to controlling Raspberry Pi GPIOs, a foundational activity in IoT growth. Whether or not you’re toggling LEDs, studying sensors, or controlling relays, Node.js affords the instruments and suppleness to make it occur seamlessly.
However IoT isn’t nearly managing {hardware} regionally. True IoT initiatives require distant dashboards that allow you to visualize real-time and historic information, and management units from anyplace. With Arduino Cloud, you are able to do all of this with ease.
Let’s dive in and see how one can take your IoT abilities to the following stage with Node.js and the Arduino Cloud!
Raspberry Pi fundamental GPIO setup
On this article, we current a simple but complete instance to reveal the ability of Arduino Cloud. You’ll learn to use an Arduino Cloud dashboard to remotely management and monitor your Raspberry Pi’s digital GPIOs. Particularly, we’ll cowl the way to:
- Flip an LED linked to your Raspberry Pi on and off.
- Detect when a push button linked to your Raspberry Pi is pressed.
- Visualize the real-time and historic values of an integer variable.
To get began, let’s join an LED and a push button to your Raspberry Pi as illustrated within the diagram under.
It’s a quite simple setup. Now that we have now every part prepared, let’s get began!
Create the System and Factor in Arduino Cloud
To ship your Raspberry Pi information to Arduino Cloud, it’s a must to observe these easy steps:
1. Arrange an Arduino Cloud account when you didn’t have one earlier than.
2. Create your gadget as a Handbook gadget.
Observe: Jot down your System ID and Secret, as we’ll want them later.
3. Create your Factor and add your variables.
Within the instance proven on this weblog put up, we use the next three variables:
- test_value: We’ll use this integer variable to point out an integer worth generated periodically in our Raspberry Pi software in our Arduino Cloud dashboard.
- button: We’ll use this boolean variable to ship the data to the Cloud when the push button is pressed.
- led: We’ll use this boolean variable to change on and off the LED from the Arduino Cloud dashboard.
Create an Arduino Cloud dashboard for information visualization:
- Create a change widget (identify: LED) and a LED widget (identify: LED) and linke them to the led variable.
- Create a chart widget (identify: Worth evolution) and a Worth widget (identify: Worth) and hyperlink them to the test_value variable.
- Create a Push button (identify: Push Button) and a Standing widget (identify: Button) and hyperlink them to the button variable.
With the dashboard, it is possible for you to to:
- Swap ON and OFF the LED utilizing the change widget
- Visualize the standing of the LED with the LED widget
- Visualize the actual time worth of the variable test_value with the Worth widget
- Visualize the evolution over time of the variable test_value with the chart widget
- Visualize on the Push Button and Button widgets when the push button has been pressed on the board
Observe: You will discover extra detailed details about the total course of in our documentation information.
Program your IoT gadget utilizing Node.js
Now it’s time to develop your Node.j software.
const gpiod = require('node-libgpiod');
const { ArduinoIoTCloud } = require('arduino-iot-js');
const { DEVICE_ID, SECRET_KEY } = require('./credentials');
// Modify these strains in line with your board setup
const GPIOCHIP = 'gpiochip4';
const LED = 14; // GPIO14, Pin 8
const BUTTON = 15; // GPIO15, Pin 10
// Be certain these variables are international. In any other case, they won't
// work correctly contained in the timers
chip = new gpiod.Chip(GPIOCHIP);
ledLine = chip.getLine(LED);
buttonLine = chip.getLine(BUTTON);
ledLine.requestOutputMode("gpio-basic");
// To configure the pull-up bias, use 32 as a substitute of gpiod.LineFlags.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP whether it is undefined
buttonLine.requestInputModeFlags("gpio-basic", gpiod.LineFlags.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP);
let consumer;
// This perform is executed each 1.0 seconds, polls the worth
// of the button and sends the info to Arduino Cloud
perform readButton(consumer) {
let button = buttonLine.getValue() ? true : false;
if (consumer)
consumer.sendProperty("button", button);
console.log("pollButton:", button);
}
// This perform is executed each 10.0 seconds, will get a random
// quantity between 0 and 100 and sends the info to Arduino Cloud
perform readValue(consumer) {
let worth = Math.ground(Math.random() * 101);
if (consumer)
consumer.sendProperty("test_value", worth);
console.log("pollValue", worth);
}
// This perform is executed every time the "led" variable modifications
perform onLedChanged(led) {
ledLine.setValue(led ? 1 : 0);
console.log("LED change! Standing is: ", led);
}
// Create Arduino Cloud connection
(async () => {
attempt {
consumer = await ArduinoIoTCloud.join({
deviceId: DEVICE_ID,
secretKey: SECRET_KEY,
onDisconnect: (message) => console.error(message),
});
consumer.onPropertyValue("led", (led) => onLedChanged(led));
}
catch(e) {
console.error("ArduinoIoTCloud join ERROR", e);
}
})();
// Ballot Worth each 10 seconds
const pollValue = setInterval(() => {
readValue(consumer);
}, 10000);
// Ballot Button each 1 seconds
const pollButton = setInterval(() => {
readButton(consumer);
}, 1000);
Create a file referred to as credentials.js together with your System ID and secret.
module.exports = {
DEVICE_ID: '09d3a634-e1ad-4927-9da0-dde663f8e5c6',
SECRET_KEY: 'IXD3U1S37QPJOJXLZMP5'
};
This code is suitable with all Raspberry Pi fashions and must also work on any Linux-based machine. Simply make sure that to specify the proper gpiochip and configure the suitable GPIO strains within the code snippet under:
const GPIOCHIP = 'gpiochip4';
const LED = 14; // GPIO14, Pin 8
const BUTTON = 15; // GPIO15, Pin 10
For extra details about the challenge, take a look at the main points on Mission Hub. You will discover the whole code and extra assets within the GitHub repository. Plus, don’t miss the excellent JavaScript + Arduino Cloud information within the following article.
Begin with Arduino Cloud at no cost
Getting your Raspberry Pi linked to Arduino Cloud with Node.js is extremely straightforward. Merely create your free account, and also you’re able to get began. Arduino Cloud is free to make use of and comes with optionally available premium options for even larger flexibility and energy.
For those who’re able to simplify information visualization and distant management in your Raspberry Pi purposes utilizing Node.js, Python, or Node-RED, Arduino Cloud is the right platform to discover and elevate your initiatives.
Get began with Arduino Cloud!