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
- A Raspberry Pi with Node.js installed. (Check our Node.js Setup Guide).
- 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
- Positive leg of each LED: Connect to a GPIO pin via a resistor.
- Negative leg of each LED: Connect to GND (ground).
Example GPIO Pin Assignment
LED Number | GPIO Pin | Physical Pin |
---|---|---|
LED 1 | GPIO17 | Pin 11 |
LED 2 | GPIO18 | Pin 12 |
LED 3 | GPIO27 | Pin 13 |
LED 4 | GPIO22 | Pin 15 |
LED 5 | GPIO23 | Pin 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
, andCtrl + 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
leds
Array: Holds Gpio objects for each LED.turnOffAll
Function: Ensures all LEDs are turned off before lighting the next one.flowLEDs
Function: Lights up the current LED and moves to the next one in the sequence.setInterval
: Repeats theflowLEDs
function every 300 milliseconds.
Enhancements
- Change Flow Direction: Add logic to reverse the sequence (e.g., left-to-right, then right-to-left).
- Speed Control: Use a potentiometer or button to adjust the interval dynamically.
- 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.