nodejs
  1. nodejs-raspi-flowing-leds

Node.js Raspberry Pi Flowing LEDs

In this tutorial, we'll discuss how to use Node.js to control Raspberry Pi's GPIO pins and create a flowing LED effect.

Prerequisites

Before we get started, you'll need the following:

  • Raspberry Pi with Raspbian OS installed
  • Node.js installed on Raspberry Pi
  • Breadboard
  • LEDs
  • 220Ω resistors
  • Jumper wires

Wiring the LEDs

First, let's wire the LEDs to the Raspberry Pi's GPIO pins. We'll use GPIO pins 18, 23, and 24. Here's how to wire the LEDs:

  1. Insert the longer leg (anode) of the first LED into one row of the breadboard, and the shorter leg (cathode) into another row.
  2. Connect a 220Ω resistor from the cathode row to ground (pin 6).
  3. Connect a jumper wire from the anode row to GPIO pin 18 (pin 12).
  4. Repeat steps 1-3 for the other two LEDs, connecting them to GPIO pins 23 and 24, respectively.

Writing the Code

Now that we've wired up the LEDs, let's write the code to control them. Here's the code:

const Gpio = require('onoff').Gpio;

// Set up the LEDs
const leds = [
    new Gpio(18, 'out'),
    new Gpio(23, 'out'),
    new Gpio(24, 'out')
];

// Flowing LED effect
let index = 0;
let dir = 1;
setInterval(() => {
    leds[index].writeSync(0);
    index += dir;
    if (index === 0 || index === leds.length - 1) {
        dir = -dir;
    }
    leds[index].writeSync(1);
}, 100);

Let's go through the code step by step:

  1. Import the "onoff" module and create a Gpio object for each LED, specifying that they are output pins.
  2. Create a "leds" array to hold the Gpio objects.
  3. Create an index variable to keep track of which LED is currently lit.
  4. Create a direction variable to keep track of the direction of the flowing effect (1 for forward, -1 for backward).
  5. Set up a setInterval loop to update the LEDs every 100ms. First, turn off the current LED, then increment the index by the direction. If the index is at the beginning or end of the array, change the direction. Finally, turn on the new LED corresponding to the updated index.

Use

This tutorial demonstrates how to use Node.js to control Raspberry Pi's GPIO pins and create a flowing LED effect. You can modify the code to create different LED effects or control other electronic components connected to the Raspberry Pi.

Important Points

  • Make sure you wire the LEDs correctly and use the correct pins in the code.
  • Be careful with the current flowing through the LEDs and resistors, as incorrect wiring or too much current can damage electronic components.
  • Always use a breadboard to make it easier to prototype and change the wiring.

Summary

In this tutorial, we covered how to use Node.js to control Raspberry Pi's GPIO pins and create a flowing LED effect. We discussed the prerequisites, wiring the LEDs, writing the code, use, and important points of this tutorial. With this knowledge, you can now create your own LED effects or control other electronic components connected to the Raspberry Pi using Node.js.

Published on: