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

Leave a Reply

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

Share

๐Ÿค– Node.js Raspberry Pi Projects

Or Copy Link

CONTENTS
Scroll to Top