Node.js Tutorial
Estimated reading: 4 minutes 23 views

๐Ÿ”Œ Node.js โ€“ GPIO & Hardware Control Projects: Blink, Button, & LED Effects

๐Ÿงฒ Introduction โ€“ Control Raspberry Pi Hardware Using Node.js GPIO

Node.js isn’t just for serversโ€”it also shines in hardware control on Raspberry Pi! Thanks to GPIO libraries like onoff and rpi-gpio, you can interact with LEDs, buttons, and other components using simple JavaScript. These beginner-friendly projects let you bring real-world electronics to life with Node.js.

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

  • How to blink an LED using Node.js
  • How to read input from a pushbutton
  • How to create a flowing LED (Knight Rider) effect

๐Ÿ“˜ Topics Covered

๐Ÿ”น Project๐Ÿ“– Description
๐Ÿ’ก Node.js โ€“ Blinking LEDTurn an LED on and off at intervals using GPIO
๐Ÿ›Ž๏ธ Node.js โ€“ LED + PushbuttonTrigger an LED with a physical pushbutton
๐ŸŽ‡ Node.js โ€“ Flowing LEDsLight up multiple LEDs in sequence to create a visual effect

๐Ÿ’ก Node.js โ€“ Blinking LED

๐Ÿ”น Hardware Required

  • 1 x Raspberry Pi (any model with GPIO)
  • 1 x LED
  • 1 x 220ฮฉ resistor
  • Breadboard and jumper wires

๐Ÿ”น Circuit

Connect:

  • LED anode (long leg) โ†’ GPIO 17 via resistor
  • LED cathode (short leg) โ†’ GND

๐Ÿ”น Code (blink-led.js)

const Gpio = require('onoff').Gpio;
const led = new Gpio(17, 'out');

let isOn = 0;

setInterval(() => {
  isOn = isOn ^ 1; // Toggle
  led.writeSync(isOn);
  console.log("LED is", isOn ? "ON" : "OFF");
}, 1000);

// Graceful exit
process.on('SIGINT', () => {
  led.writeSync(0);
  led.unexport();
  console.log('LED Off, Exiting...');
  process.exit();
});

๐Ÿงช Output (Terminal):

LED is ON
LED is OFF
LED is ON
...

โœ… LED blinks every 1 second.


๐Ÿ›Ž๏ธ Node.js โ€“ LED + Pushbutton

๐Ÿ”น Hardware Required

  • 1 x Pushbutton
  • 1 x LED
  • 1 x 220ฮฉ resistor
  • Breadboard + jumper wires

๐Ÿ”น Circuit

  • Button between GPIO 4 and GND
  • LED between GPIO 17 and GND via resistor

๐Ÿ”น Code (led-button.js)

const Gpio = require('onoff').Gpio;
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'both');

button.watch((err, value) => {
  if (err) throw err;
  led.writeSync(value);
  console.log("Button state:", value);
});

// Graceful exit
process.on('SIGINT', () => {
  led.writeSync(0);
  led.unexport();
  button.unexport();
  console.log('Clean exit.');
  process.exit();
});

๐Ÿงช Output (Terminal):

Button state: 1
Button state: 0

โœ… LED lights up when the button is pressed.


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

๐Ÿ”น Hardware Required

  • 4โ€“8 LEDs
  • Resistors (220ฮฉ)
  • Jumper wires
  • Connect GPIO 17 to GPIO 20 (or more)

๐Ÿ”น Code (flowing-leds.js)

const Gpio = require('onoff').Gpio;
const pins = [17, 18, 19, 20];
const leds = pins.map(pin => new Gpio(pin, 'out'));

let index = 0;
setInterval(() => {
  leds.forEach(led => led.writeSync(0)); // Turn all off
  leds[index].writeSync(1); // Turn one on
  index = (index + 1) % leds.length;
}, 300);

// Cleanup
process.on('SIGINT', () => {
  leds.forEach(led => {
    led.writeSync(0);
    led.unexport();
  });
  console.log('LED Flow Stopped.');
  process.exit();
});

๐Ÿงช Output:

LEDs light one after another in a flowing pattern

โœ… Visual LED wave effect like Knight Rider or progress bar.


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

Node.js brings event-driven control to Raspberry Pi GPIO with easy, readable JavaScript. Whether itโ€™s blinking a light or responding to a button, the GPIO pins allow Node.js to interact with the real world.

๐Ÿ” Key Takeaways:

  • Use the onoff library to interact with GPIO on Raspberry Pi
  • Always clean up GPIO on exit to avoid pin lockups
  • Blinking LEDs and reading buttons are the foundation of robotics
  • Flowing LEDs teach iteration and visual feedback control

โš™๏ธ Real-World Uses:

  • Smart light systems
  • Security buttons and motion triggers
  • LED progress indicators
  • Teaching kids IoT basics through visual effects

โ“ Frequently Asked Questions

โ“ How do I install the onoff library?
โœ… Run:

npm install onoff

โ“ Do I need to run the Node.js script as sudo?
โœ… Yes, to access GPIO:

sudo node blink-led.js

โ“ What happens if I donโ€™t unexport the pins?
โœ… Pins may remain in use, preventing further access. Always call unexport() on exit.


โ“ Can I control other hardware like buzzers or motors?
โœ… Yes! Any digital component (on/off) can be controlled via GPIO, including buzzers, relays, and motor drivers.


Share Now :

Leave a Reply

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

Share

๐Ÿ”Œ Node.js โ€“ GPIO & Hardware Control Projects

Or Copy Link

CONTENTS
Scroll to Top