Node.js Tutorial
Estimated reading: 4 minutes 272 views

Node.js Raspberry Pi Projects โ€“ Real-World IoT with JavaScript & GPIO

Introduction โ€“ Build Interactive IoT Projects with Node.js on Raspberry Pi

Node.js brings the power of event-driven, non-blocking JavaScript to the Raspberry Pi, allowing you to build responsive and scalable IoT hardware projects. Whether youโ€™re blinking LEDs, reading sensor data, or building real-time dashboards with WebSockets, the combination of Node.js + Raspberry Pi GPIO opens up endless hardware automation possibilities.

In this guide, youโ€™ll learn:

  • How to control GPIO pins using Node.js
  • Hands-on Raspberry Pi projects: LED blink, button control, flowing effects
  • Real-time device control via WebSockets and dashboards
  • Project-ready examples with hardware diagrams and code

Topics Covered

Project Description
Node.js โ€“ GPIO & Hardware ControlControl LEDs, buttons, and basic electronics
Node.js โ€“ Blinking LEDTurn an LED on and off using GPIO with JavaScript
Node.js โ€“ LED + PushbuttonLight up LEDs with physical input via GPIO
Node.js โ€“ Flowing LEDsCreate a sequential LED animation (Knight Rider style)
Node.js โ€“ WebSocket-Based ControlsBuild a live dashboard for controlling hardware from browser
Node.js โ€“ RGB LED with WebSocketChange LED colors in real-time with WebSockets
Node.js โ€“ Other Raspberry Pi ComponentsControl buzzers, relays, sensors, motors and more via GPIO

Node.js โ€“ GPIO & Hardware Control

Install the GPIO library:

npm install onoff

Import it in your Node.js script:

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

You can then use .writeSync(1) to turn on GPIO-connected components like LEDs or buzzers.


Node.js โ€“ Blinking LED

Blink an LED every 1 second using GPIO 17:

const led = new Gpio(17, 'out');
let value = 0;
setInterval(() => {
  value = value ^ 1;
  led.writeSync(value);
}, 1000);

Output: LED toggles ON and OFF every second.

Great for learning timing and output logic.


Node.js โ€“ LED + Pushbutton

Control an LED based on a physical button on GPIO 4:

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

button.watch((err, value) => {
  if (err) throw err;
  led.writeSync(value);
});

Output: Pressing the button lights the LED in real-time.

Demonstrates input detection and basic interactivity.


Node.js โ€“ Flowing LEDs (LED Strip Logic)

Use multiple GPIO pins for LED animation:

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

setInterval(() => {
  leds.forEach(led => led.writeSync(0));
  leds[i].writeSync(1);
  i = (i + 1) % leds.length;
}, 300);

Output: LEDs light up one-by-one in sequence like a wave.

Great for simulating loading or animation effects.


Node.js โ€“ WebSocket-Based Controls

Install dependencies:

npm install express ws onoff

Create a WebSocket server:

const ws = new WebSocket.Server({ server });
ws.on('connection', (socket) => {
  socket.on('message', (data) => {
    const { r, g, b } = JSON.parse(data);
    red.writeSync(r); green.writeSync(g); blue.writeSync(b);
  });
});

Use browser buttons to control GPIO in real-time.

Control RGB lights, buzzers, and relays remotely from a web UI.


Node.js โ€“ Other Raspberry Pi Components

Extend GPIO to other real-world components:

Component GPIO Use Example Application
BuzzerGPIO 23 โ†’ ON/OFFAlerts or notifications
Relay ModuleGPIO 24 โ†’ Switch voltageControl fans, lights, or appliances
Ultrasonic SensorGPIO 18 + 27Distance measurement
Servo MotorGPIO + PWM modulePhysical rotation (door lock, pan-tilt)

Use GPIO logic similar to LEDsโ€”just match pin mode and write logic accordingly.


Summary โ€“ Recap & Next Steps

Node.js turns your Raspberry Pi into a real-time programmable IoT device. With a mix of hardware like LEDs and sensors and software like WebSockets, you can create smart home devices, interactive dashboards, and even robotics.

Key Takeaways:

  • Use onoff to control GPIO using Node.js
  • Blink LEDs, read pushbuttons, and sequence effects
  • WebSockets allow real-time control from browsers
  • Extend control to buzzers, relays, and sensors

Real-World Applications:

  • Remote home lighting systems
  • IoT dashboards for sensor status
  • Robotics and automation control
  • School electronics and STEM projects

Frequently Asked Questions

Can I use Node.js on Raspberry Pi Zero?
Yes, just ensure you’re using a lightweight OS and Node version compatible with ARMv6/v7.


Do I need to run GPIO scripts as sudo?
Yes. Use:

sudo node yourfile.js

Is onoff the only GPIO library for Node.js?
No, others like rpi-gpio, pigpio, and bonescript are available for advanced use cases.


Can I control devices over the internet?
Yes. Combine Express/WebSocket with port forwarding or use services like Ngrok for tunneling.


Share Now :
Share

๐Ÿค– Node.js Raspberry Pi Projects

Or Copy Link

CONTENTS
Scroll to Top