nodejs
  1. nodejs-raspi-gpio-introduction

Node.js Raspberry Pi GPIO Introduction

Node.js provides the ability to interact with the Raspberry Pi's General Purpose Input Output (GPIO) pins to control external devices such as LEDs, sensors, and other components. In this tutorial, we'll discuss how to use the Node.js RPi.GPIO library to control GPIO pins on a Raspberry Pi.

Prerequisites

To follow along with this tutorial, you will need:

  • A Raspberry Pi
  • Node.js installed on the Raspberry Pi
  • Basic knowledge of JavaScript

Syntax

The syntax for controlling GPIO pins using Node.js is as follows:

const { Gpio } = require('onoff');

const led = new Gpio(17, 'out');

led.writeSync(1);

led.writeSync(0);

First, we import the onoff library and create a new instance of the Gpio class on the specified pin (in this case, pin 17). Then, we use the writeSync method to set the state of the pin to either 1 (on) or 0 (off).

Example

Let's say we have an LED connected to pin 17 on the Raspberry Pi and we want to turn it on for 2 seconds and then turn it off. Here's how we can implement it:

const { Gpio } = require('onoff');

const led = new Gpio(17, 'out');

console.log('Turning on LED...');
led.writeSync(1);

setTimeout(() => {
   console.log('Turning off LED...');
   led.writeSync(0);
   led.unexport();
}, 2000);

Now, when we run this program, the LED will turn on for 2 seconds and then turn off.

Output

When we run the example code above on a Raspberry Pi with an LED connected to pin 17, we should see the following output:

Turning on LED...
Turning off LED...

This is because the LED was turned on for 2 seconds and then turned off using the writeSync method.

Explanation

In the example above, we used the onoff library to import the Gpio class and create a new instance for pin 17. Then, we turned on the LED by using the writeSync method to set the state of the pin to 1. We added a timeout for 2 seconds to wait before turning off the LED. Once the timeout is up, we called the writeSync method again, this time setting the state of the pin to 0 to turn off the LED. Lastly, we called the unexport method to release the pin.

Use

Node.js can be used to control GPIO pins on a Raspberry Pi, enabling the development of a wide range of IoT applications.

Important Points

  • Ensure that you have the RPi.GPIO library installed on your Raspberry Pi.
  • Always export the pins before using them and unexport them when they are no longer needed.
  • Node.js provides different types of pins like input, output, PWM, and more. You should use the appropriate pin type depending on the requirement.

Summary

In this tutorial, we discussed how to use the Node.js RPi.GPIO library to control GPIO pins on a Raspberry Pi. We covered the syntax, example, output, explanation, use, and important points of controlling GPIO pins using Node.js. With this knowledge, you can now control a wide variety of external devices using your Raspberry Pi and Node.js.

Published on: