RaspPi Flowing LEDs

Creating a project that involves flowing LEDs with a Raspberry Pi using Node.js is a fantastic way to dive into the world of physical computing. In this guide, we’ll explore how to control a series of LEDs in a flowing pattern, akin to a wave or a ripple effect, using the General Purpose Input/Output (GPIO) pins on the Raspberry Pi. This project is perfect for beginners and seasoned developers alike, providing a hands-on approach to learn about electronics while harnessing the power of Node.js for hardware control. Let’s get started!

What We Need

To create the flowing LEDs effect, you’ll need the following components:

  • Raspberry Pi (any model that has GPIO pins will work)
  • Breadboard
  • LEDs (at least 5 for a good effect, but you can use more)
  • 220-ohm resistors (one for each LED to limit the current)
  • Jumper wires (for connecting the components)
  • Node.js installed on your Raspberry Pi

Building the Circuit

Before diving into the code, let’s set up our circuit:

  1. Connect the LEDs: Insert your LEDs into the breadboard. Make sure you connect the longer leg (anode) to the GPIO pins through the resistors and the shorter leg (cathode) to the ground (GND).
  2. Add Resistors: Connect a 220-ohm resistor to each LED’s anode (longer leg) to protect them from burning out.
  3. Wire it up: Use jumper wires to connect each resistor’s other end to different GPIO pins on the Raspberry Pi. Then, connect all the cathodes (shorter legs) of the LEDs to a GND pin on the Raspberry Pi using a jumper wire.

Raspberry Pi and Node.js Flowing LEDs Script

With the circuit ready, let’s proceed to the software part. First, ensure Node.js is installed on your Raspberry Pi. You can install Node.js via nvm (Node Version Manager) or download it from the Node.js website.

Step 1: Install GPIO Library

We’ll use the onoff Node.js library to control the GPIO pins. Install it by running:

npm install onoff

Step 2: Using Array With Output to Create Flowing LEDs

Create a new JavaScript file, flowingLEDs.js, and open it in your editor. The idea is to use an array to manage the GPIO pins connected to the LEDs and then create a “flowing” effect by turning them on and off in sequence.

Here’s how you can do it:

const Gpio = require('onoff').Gpio; // Import the onoff library

// Define the GPIO pins for the LEDs
const leds = [17, 27, 22, 5, 6].map(pin => new Gpio(pin, 'out'));
let currentLed = 0;

function flowLEDs() {
  leds[currentLed].writeSync(1); // Turn on the current LED

  setTimeout(() => {
    leds[currentLed].writeSync(0); // Turn off the current LED
    currentLed = (currentLed + 1) % leds.length; // Move to the next LED
    flowLEDs(); // Recursively continue the flow
  }, 100); // Delay between each LED

flowLEDs(); // Start the flowing effect

This script initializes an array of Gpio objects, each representing a pin connected to an LED. The flowLEDs function turns on an LED, waits for a short period, then turns it off and moves to the next, creating a flowing effect.

To run your script, execute:

node flowingLEDs.js

You should see your LEDs light up in sequence, creating a flowing or “chasing” effect.


Congratulations! You’ve successfully created a flowing LED effect using Node.js and your Raspberry Pi. This project not only introduces you to hardware control with Node.js but also opens up a world of possibilities for integrating software and hardware in your future projects. Experiment with different sequences or incorporate sensors to make your projects even more interactive. Happy coding!