nodejs
  1. nodejs-raspi-led-pushbutton

Node.js Raspberry Pi LED & Pushbutton

In this tutorial, we will discuss how to control an LED and read the status of a pushbutton on a Raspberry Pi using Node.js. We will be using the GPIO pins of the Raspberry Pi and the Node.js library called onoff.

Prerequisites

  • Raspberry Pi (with GPIO pins)
  • LED
  • 220Ω resistor
  • Pushbutton
  • Jumper wires
  • Breadboard
  • Node.js and npm installed on the Raspberry Pi

Syntax

The syntax for controlling an LED and reading the status of a pushbutton using onoff in Node.js is as follows:

const Gpio = require('onoff').Gpio;
const led = new Gpio(14, 'out');
const button = new Gpio(15, 'in', 'both');

button.watch((err, value) => {
  if (err) {
    console.log('Error', err);
    return;
  }

  if (value === 1) {
    led.writeSync(1);
  } else {
    led.writeSync(0);
  }
});

Example

We will be connecting an LED and a pushbutton to our Raspberry Pi. We will use pin 14 as the output pin for the LED and pin 15 as the input pin for the pushbutton. Here's the wiring diagram:

LED Pushbutton Wiring Diagram

Now, let's write the Node.js script that will control the LED and read the status of the pushbutton:

const Gpio = require('onoff').Gpio;
const led = new Gpio(14, 'out');
const button = new Gpio(15, 'in', 'both');

button.watch((err, value) => {
  if (err) {
    console.log('Error', err);
    return;
  }

  if (value === 1) {
    led.writeSync(1);
  } else {
    led.writeSync(0);
  }
});

process.on('SIGINT', () => {
  led.writeSync(0);
  led.unexport();
  button.unexport();
  process.exit();
});

Save the above script with a filename, say led_pushbutton.js. Now, open the terminal and navigate to the folder containing the script. Type the following command to run the script:

node led_pushbutton.js

Now, when you press the pushbutton, the LED should light up. When you release the button, the LED should turn off.

Explanation

In the above example, we first import the Gpio class from the onoff library. We then initialize two instances of the Gpio class - one for the LED and another for the pushbutton. We set the mode of the LED pin to out and the mode of the pushbutton pin to in with the third parameter 'both'. This sets up the pushbutton for both rising and falling edge detection.

Next, we attach an event listener to the pushbutton instance using the watch() method. This listens for changes in the state of the button. When the button is pressed, the value returned from the watch() method will be 1, and the LED will turn on. When the button is released, the value returned will be 0, and the LED will turn off.

Finally, we attach a SIGINT event listener to the process object. This ensures that when you hit Ctrl + C to terminate the program, the LED output is set to 0, and both the LED and pushbutton pins are unexported from the GPIO and released.

Use

This tutorial is useful for beginners who are getting started with Raspberry Pi GPIO programming using Node.js. You can use this technique to control other GPIO peripherals like motors, sensors, and other electronic components, by writing similar event listeners based on your use case.

Important Points

  • Always use a resistor in series with the LED to prevent damage to the Raspberry Pi.
  • Ensure that your circuit is wired correctly, as an incorrectly wired circuit can damage your Raspberry Pi or other electronic components.
  • Ensure that Node.js and the onoff library are installed correctly on your Raspberry Pi to prevent errors in running the script.

Summary

In this tutorial, we discussed how to control an LED and read the status of a pushbutton using Node.js on a Raspberry Pi. We covered the prerequisites, syntax, example, output, explanation, use, important points, and summary of the tutorial. With this knowledge, you can now perform GPIO programming using Node.js on your Raspberry Pi and interact with other electronic components like switches, sensors, and motors.

Published on: