Node.js Raspberry Pi GPIO Flowing LEDs

Here is a clear, complete, and beginner-friendly guide for Node.js Raspberry Pi GPIO – Flowing LEDs (LED Chaser / Running Lights).


🌈 Node.js Raspberry Pi GPIO – Flowing LEDs (LED Chaser Effect)

In this project, you will connect multiple LEDs to Raspberry Pi and create a flowing / chasing light effect like:

➡ LED1 → LED2 → LED3 → LED4 → LED5 → LED1 → repeat…

This effect is similar to “Knight Rider”, “Running Lights”, or “Larson Scanner”.


1. What You Need

Hardware:

  • 1 × Raspberry Pi

  • 5 × LEDs (any color)

  • 5 × 330Ω resistors

  • Jumper wires

  • Breadboard (recommended)


🔌 2. GPIO Pin Selection

We will use these pins:

LED GPIO Pin Board Pin
LED 1 GPIO17 Pin 11
LED 2 GPIO27 Pin 13
LED 3 GPIO22 Pin 15
LED 4 GPIO23 Pin 16
LED 5 GPIO24 Pin 18

🧩 3. Wiring Diagram

Each LED:

GPIO --> Resistor (330Ω) --> LED (Anode +)
LED Cathode (−) --> GND

Repeat for all 5 LEDs.


📦 4. Install GPIO Library

npm install onoff

💡 5. Create File

nano flowing_leds.js

🚀 6. Node.js Code – Flowing LED Effect

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

// Define all LEDs
const leds = [
new Gpio(17, “out”),
new Gpio(27, “out”),
new Gpio(22, “out”),
new Gpio(23, “out”),
new Gpio(24, “out”)
];

let index = 0;

setInterval(() => {

// Turn all LEDs OFF
leds.forEach(led => led.writeSync(0));

// Turn current LED ON
leds[index].writeSync(1);

// Move to next LED
index = (index + 1) % leds.length;

}, 200); // speed: lower = faster

// Cleanup on exit
process.on(“SIGINT”, () => {
leds.forEach(led => {
led.writeSync(0);
led.unexport();
});
console.log(“\nClean exit”);
process.exit();
});


7. Run the Program

sudo node flowing_leds.js

You will now see LEDs turning ON one after another in a “flowing” pattern.


⚙ Adjust the Speed

Slow down (500ms):

}, 500);

Very fast (50ms):

}, 50);

🔥 Advanced Animation Effects

You can create many LED patterns.


1️⃣ Reverse Flow

let index = 0;
let direction = 1;
setInterval(() => {
leds.forEach(led => led.writeSync(0));
leds[index].writeSync(1);

index += direction;

if (index === leds.length1 || index === 0) {
direction *= –1; // bounce effect
}

}, 150);


2️⃣ Ping-Pong Motion (Knight Rider)

Same as reverse flow above — popular sci-fi effect.


3️⃣ Wave Effect (Two LEDs ON at once)

setInterval(() => {
leds.forEach((led, i) => {
led.writeSync(i === index || i === (index + 1) % leds.length ? 1 : 0);
});
index = (index + 1) % leds.length;
}, 120);


4️⃣ Random LED Flicker

setInterval(() => {
leds.forEach(() => leds.forEach(led => led.writeSync(0)));
leds[Math.floor(Math.random() * leds.length)].writeSync(1);
}, 100);

🧠 Best Practices

✔ Always turn off LEDs before exit
✔ Use resistors to avoid GPIO damage
✔ Avoid drawing too much current (max 50mA total)
✔ For many LEDs, use ULN2803 or transistor driver

You may also like...