Node.js Tutorial
Estimated reading: 4 minutes 260 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 :
Share

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

Or Copy Link

CONTENTS
Scroll to Top