๐Ÿ“ถ Node.js โ€“ WebSocket-Based Controls
Estimated reading: 4 minutes 266 views

Node.js โ€“ Other Raspberry Pi Components โ€“ Control Sensors, Motors, and Displays with Node.js


Introduction โ€“ Why Use Node.js for More Raspberry Pi Components?

After mastering LEDs and buttons, the real magic happens when you start controlling other Raspberry Pi componentsโ€”like sensors, motors, displays, buzzers, and relaysโ€”using Node.js. Thanks to libraries like onoff, pigpio, and johnny-five, Node.js allows you to handle both digital and analog devices, enabling robust IoT projects.

In this guide, youโ€™ll learn:

  • How to interface Node.js with various Raspberry Pi components
  • Examples: ultrasonic sensor, relay, buzzer, servo, and LCD
  • Required libraries and wiring tips
  • Use cases for smart systems, alerts, automation, and feedback

Supported Raspberry Pi Components with Node.js

Component Description Node.js Library
Ultrasonic SensorMeasures distance using sound waves (HC-SR04)pigpio
Relay ModuleControls high-voltage appliancesonoff / rpio
Piezo BuzzerEmits beeps or alertsonoff
Servo MotorAngular movement (0โ€“180ยฐ)pigpio
DHT11/DHT22 SensorReads temperature and humiditynode-dht-sensor
I2C LCD DisplayShows text on 16×2 or 20×4 screenslcd / raspi-i2c
Motion Sensor (PIR)Detects movementonoff

Example 1 โ€“ Ultrasonic Distance Sensor (HC-SR04)

Wiring:

  • VCC โ†’ 5V
  • GND โ†’ GND
  • TRIG โ†’ GPIO 23
  • ECHO โ†’ GPIO 24

Node.js Code (with pigpio):

const Gpio = require('pigpio').Gpio;
const trigger = new Gpio(23, { mode: Gpio.OUTPUT });
const echo = new Gpio(24, { mode: Gpio.INPUT, alert: true });

trigger.digitalWrite(0); // Ensure trigger is low

setInterval(() => {
  trigger.trigger(10, 1); // 10 microsecond pulse
}, 1000);

echo.on('alert', (level, tick) => {
  // Calculate time and distance here...
});

Example 2 โ€“ Relay to Toggle AC Device

Wiring:

  • IN1 โ†’ GPIO 17
  • VCC/GND โ†’ 5V/GND
  • Connect relay output to AC light or fan carefully

Node.js Code (with onoff):

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

relay.writeSync(1); // Turn device ON
setTimeout(() => relay.writeSync(0), 5000); // OFF after 5s

Example 3 โ€“ Buzzer Beep on Button Press

Wiring:

  • Buzzer โ†’ GPIO 18
  • Button โ†’ GPIO 4
const Gpio = require('onoff').Gpio;
const buzzer = new Gpio(18, 'out');
const button = new Gpio(4, 'in', 'rising', { debounceTimeout: 10 });

button.watch(() => {
  buzzer.writeSync(1);
  setTimeout(() => buzzer.writeSync(0), 200);
});

Example 4 โ€“ Servo Motor (0โ€“180ยฐ)

Wiring:

  • Signal โ†’ GPIO 18 (PWM)
  • Power/GND โ†’ External 5V + Ground
const Gpio = require('pigpio').Gpio;
const servo = new Gpio(18, { mode: Gpio.OUTPUT });

servo.servoWrite(1000); // 0 degrees
setTimeout(() => servo.servoWrite(2000), 2000); // 180 degrees

Example 5 โ€“ LCD Display (I2C 16×2)

Wiring:
Connect via I2C (SCL/SDA), typically GPIO 3 (SCL) and GPIO 2 (SDA)

const LCD = require('lcd');
const lcd = new LCD({
  rs: 12, e: 16, data: [20, 21, 22, 23],
  cols: 16, rows: 2
});

lcd.on('ready', () => {
  lcd.print('Node.js LCD Demo');
});

You may also use i2c-bus with address scanning for I2C-enabled LCDs.


Best Practices for Raspberry Pi + Node.js Projects

Tip Why It Helps
Use pull-up/down resistors for sensorsAvoid floating inputs
Handle SIGINT for GPIO cleanupPrevent stuck pins and hardware damage
Use pigpio for PWM/precise timingBetter than onoff for analog components
Use async logic (Promise/await)Ensures clean control flow
Protect GPIO from overvoltageAlways buffer or use logic level shifters

Summary โ€“ Recap & Next Steps

With Node.js and Raspberry Pi, youโ€™re no longer limited to just LEDs and buttons. From displays to motors, you can build complete smart systems and IoT devices using familiar JavaScript.

Key Takeaways:

  • Node.js works with various components using onoff, pigpio, i2c-bus, and other libraries
  • You can control sensors, servos, LCDs, and relays using GPIO and I2C
  • Proper wiring and software cleanup are essential for safe hardware control

Real-world relevance:
Powering home automation, IoT dashboards, robots, access systems, and interactive displays using JavaScript and Node.js.


FAQs โ€“ Node.js with Other Raspberry Pi Components


Can Node.js read analog sensors on Raspberry Pi?
Not directly. Use an ADC module (like MCP3008) and communicate over SPI.


Which library is best for PWM and servo control?
pigpio is highly recommended for precise PWM, servo, and ultrasonic sensor timing.


Can I run multiple components in one app?
Yes! You can control a relay, sensor, and LCD all from a single Node.js script using multiple GPIO instances.


Is it safe to connect motors directly to GPIO?
No. Always use a motor driver (L298N, ULN2003) to protect your Pi from current spikes.


Do I need sudo for all hardware operations?
Some GPIO libraries require elevated privileges. Use:

sudo node app.js

Share Now :
Share

๐Ÿ”ง Node.js โ€“ Other Raspberry Pi Components

Or Copy Link

CONTENTS
Scroll to Top