Node.js Raspberry Pi GPIO – Flowing LEDs

Welcome to The Coding College! In this project, we’ll use Node.js to control multiple LEDs in a flowing pattern on a Raspberry Pi. This is a fun and visually appealing way to explore GPIO control with Node.js.

What You’ll Learn

  • How to control multiple LEDs with GPIO pins.
  • How to create a sequential lighting effect (flowing LEDs).
  • How to manage timing in Node.js for hardware interactions.

Prerequisites

  1. A Raspberry Pi with Node.js installed. (Check our Node.js Setup Guide).
  2. Basic understanding of JavaScript and circuits.

Hardware Requirements

  • 1 x Raspberry Pi (with GPIO pins).
  • 5 x LEDs (or more if desired).
  • 5 x Resistors (330Ω recommended).
  • Jumper wires.
  • Breadboard.

Circuit Setup

Connecting LEDs

  1. Positive leg of each LED: Connect to a GPIO pin via a resistor.
  2. Negative leg of each LED: Connect to GND (ground).

Example GPIO Pin Assignment

LED NumberGPIO PinPhysical Pin
LED 1GPIO17Pin 11
LED 2GPIO18Pin 12
LED 3GPIO27Pin 13
LED 4GPIO22Pin 15
LED 5GPIO23Pin 16

Installing Required Library

Install the onoff library for GPIO control:

npm install onoff

Node.js Code

  • Open the terminal and create a new file:
nano flowing_leds.js
  • Add the following code:
const Gpio = require('onoff').Gpio; // Import onoff library

// Configure GPIO pins as output for LEDs
const leds = [
    new Gpio(17, 'out'),
    new Gpio(18, 'out'),
    new Gpio(27, 'out'),
    new Gpio(22, 'out'),
    new Gpio(23, 'out'),
];

let index = 0; // Start index for the LED array

// Function to turn off all LEDs
const turnOffAll = () => {
    leds.forEach(led => led.writeSync(0));
};

// Function to flow LEDs
const flowLEDs = () => {
    turnOffAll(); // Turn off all LEDs
    leds[index].writeSync(1); // Turn on the current LED
    index = (index + 1) % leds.length; // Move to the next LED
};

// Run the flow pattern every 300ms
const interval = setInterval(flowLEDs, 300);

// Graceful shutdown
process.on('SIGINT', () => {
    clearInterval(interval); // Stop the interval
    turnOffAll(); // Turn off all LEDs
    leds.forEach(led => led.unexport()); // Free GPIO resources
    console.log('Exiting...');
    process.exit();
});
  • Save and exit: Press Ctrl + O, Enter, and Ctrl + X.

Running the Code

Run the script with sudo for GPIO access:

sudo node flowing_leds.js

Expected Behavior

  • LEDs will light up one by one in sequence, creating a flowing effect.
  • The pattern will repeat indefinitely.

Understanding the Code

  1. leds Array: Holds Gpio objects for each LED.
  2. turnOffAll Function: Ensures all LEDs are turned off before lighting the next one.
  3. flowLEDs Function: Lights up the current LED and moves to the next one in the sequence.
  4. setInterval: Repeats the flowLEDs function every 300 milliseconds.

Enhancements

  1. Change Flow Direction: Add logic to reverse the sequence (e.g., left-to-right, then right-to-left).
  2. Speed Control: Use a potentiometer or button to adjust the interval dynamically.
  3. Multiple Patterns: Implement different LED patterns (e.g., alternate, all-on, wave).

Best Practices

  • Use Resistors: Protect the LEDs and GPIO pins.
  • Release Resources: Always unexport GPIO pins after use to avoid errors.
  • Power Management: Avoid powering too many LEDs directly from GPIO; use external power sources if needed.

Conclusion

This flowing LED project demonstrates the power and flexibility of Node.js for controlling hardware with Raspberry Pi GPIO pins. By experimenting with different patterns and configurations, you can build visually engaging projects for learning or practical applications.

For more tutorials on coding and hardware projects, visit The Coding College.

Leave a Comment