๐ŸŽ‡ Node.js โ€“ Flowing LEDs โ€“ Create LED Chaser Effect with Raspberry Pi & Node.js


๐Ÿงฒ Introduction โ€“ What Is a Flowing LED (Chaser)?

A Flowing LED, also called an LED Chaser, is a classic electronics project where LEDs light up one after another in a sequence, creating a “flow” or “running” light effect. Using Node.js and Raspberry Pi GPIO, you can programmatically control this flow, giving your project a futuristic or dynamic look.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to wire multiple LEDs to Raspberry Pi GPIO
  • Use Node.js and the onoff library to sequence LEDs
  • Create loop logic for left-to-right and reverse chasing
  • Add timing, effects, and cleanup logic

๐Ÿ”Œ Hardware Required

๐Ÿงฐ Component๐Ÿ“‹ Description
Raspberry PiAny model with GPIO support
4โ€“6 LEDsRed, green, blue, etc.
330ฮฉ ResistorsOne per LED to prevent overcurrent
BreadboardFor connecting components
Jumper WiresMale-to-male wires

๐Ÿงพ Wiring Overview (Example with 4 LEDs)

  • LED1 โ†’ GPIO 17 (Pin 11)
  • LED2 โ†’ GPIO 18 (Pin 12)
  • LED3 โ†’ GPIO 27 (Pin 13)
  • LED4 โ†’ GPIO 22 (Pin 15)
  • Each LED’s anode goes through a 330ฮฉ resistor to GND (Pin 6)

๐Ÿ› ๏ธ Use pinout.xyz to check GPIO layout.


๐Ÿ“ฆ Install Required Package

npm install onoff

๐ŸŽ‡ Node.js Code โ€“ Flowing LEDs

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

// Define GPIO pins for each LED
const ledPins = [17, 18, 27, 22];
const leds = ledPins.map(pin => new Gpio(pin, 'out'));

let current = 0;
let direction = 1;

// Flow function
const interval = setInterval(() => {
  // Turn all LEDs off
  leds.forEach(led => led.writeSync(0));

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

  // Move to next LED
  current += direction;

  // Reverse direction at ends
  if (current === leds.length || current < 0) {
    direction *= -1;
    current += direction;
  }
}, 200); // 200ms per step

// Cleanup on exit
process.on('SIGINT', () => {
  clearInterval(interval);
  leds.forEach(led => {
    led.writeSync(0);
    led.unexport();
  });
  console.log('\n๐Ÿงน Cleaned up and exiting...');
  process.exit();
});

๐Ÿงช Output โ€“ What Youโ€™ll See

  • LEDs light up one after another:
    LED1 โ†’ LED2 โ†’ LED3 โ†’ LED4 โ†’ LED3 โ†’ LED2 โ†’ …
  • Repeats continuously until the process is stopped.

๐Ÿงฑ Best Practices for Flowing LED Projects

โœ… Practice๐Ÿ’ก Why Itโ€™s Useful
Use writeSync(0) before each stepEnsures only one LED is ON at a time
Clean up GPIO with unexport()Prevents pin locking on reruns
Use SIGINT handlingGracefully stop script with Ctrl+C
Modularize LED flow logicMakes it reusable for other effects
Use setTimeout() or setInterval()Time control for animations

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Creating flowing LEDs with Node.js is a fun and visual way to learn GPIO programming. You control the timing, direction, and animation entirely with JavaScriptโ€”making it a great bridge between frontend logic and embedded control.

๐Ÿ” Key Takeaways:

  • Use onoff to control GPIO pins via JavaScript
  • Create flowing/chaser effects using intervals and loops
  • Clean up with unexport() to prevent errors
  • Reverse direction with logic for back-and-forth animation

โš™๏ธ Real-world relevance:
Used in status displays, vehicle lighting effects, wearables, interactive art, and IoT dashboards.


โ“FAQs โ€“ Node.js Flowing LEDs


โ“ Can I use more than 4 LEDs?
โœ… Yes! Just expand the ledPins array:

const ledPins = [17, 18, 27, 22, 23, 24];

โ“ How can I reverse the flow direction manually?
โœ… Add a button input on another GPIO pin and toggle direction *= -1 on press.


โ“ Why do LEDs stay ON after the script ends?
โŒ If you donโ€™t run unexport(), the GPIO stays active. Use cleanup logic on SIGINT.


โ“ Can I slow down the blinking?
โœ… Yes. Increase the interval from 200ms to 500ms, for example.

setInterval(..., 500);

โ“ Do I need root permissions to run this script?
โœ… Sometimes. If you get a permissions error, use:

sudo node flowing-leds.js

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŽ‡ Node.js โ€“ Flowing LEDs

Or Copy Link

CONTENTS
Scroll to Top