Node.js and Raspberry Pi

Here is a clean and complete guide for Node.js and Raspberry Pi, covering installation, GPIO control, sensors, automation, web servers, and real project examples.


🍓 Node.js and Raspberry Pi

Raspberry Pi is a tiny, powerful computer that runs Linux. Node.js works perfectly on it, making it ideal for:

✔ Home automation
✔ IoT projects
✔ Robotics
✔ Sensor monitoring
✔ Touchscreen dashboards
✔ Smart devices

Node.js is lightweight, fast, asynchronous, and perfect for hardware-interactive apps.


📌 1. Install Node.js on Raspberry Pi

Step 1: Update Pi

sudo apt update
sudo apt upgrade -y

Step 2: Install Node.js (Recommended LTS)

sudo apt install -y nodejs npm

Check version:

node -v
npm -v

📌 2. Using Node.js for GPIO Control

Raspberry Pi has GPIO pins that you can control using Node.js packages like:

  • onoff

  • pigpio (more precise, PWM supported)

  • rpi-gpio

Most common: onoff

Install

npm install onoff

🔌 3. Turn ON/OFF an LED (GPIO Output Example)


 

Run:

sudo node led.js

🖲️ 4. Read Button Press (GPIO Input Example)


 


🌡️ 5. Read Sensors using Node.js

Common sensors with Node.js:

Sensor Type Node.js Library
DHT11 / DHT22 Temperature + Humidity node-dht-sensor
HC-SR04 Ultrasonic distance rpi-hc-sr04
MPU6050 Gyro + Accelerometer i2c-bus
DS18B20 Waterproof temperature Built-in kernel module

Example: DHT22

npm install node-dht-sensor

 


🌐 6. Running a Web Server on Raspberry Pi (Node.js Express)

npm install express

 

Visit:

http://<your-pi-ip>:3000

🔋 7. Control GPIO from a Web Page

Example: Press a button on a webpage → Turn ON LED.

Backend (Node.js):


 

Frontend:

http://<pi-ip>:3000/on
http://<pi-ip>:3000/off

📶 8. IoT: Send Data to Cloud

You can send sensor data to:

  • Firebase

  • AWS IoT

  • Azure IoT Hub

  • MQTT Brokers

  • Node-RED

  • ThingsBoard

  • Blynk

Example using MQTT:

npm install mqtt

 


🖥️ 9. Node.js + Raspberry Pi Touchscreen UI

Use these frameworks:

  • Electron.js

  • Next.js local dashboard

  • WebSocket-based control panels

  • Dashboard UI with Bootstrap or React


🤖 10. Example Real Raspberry Pi + Node.js Projects

✔ Smart Home Automation

Lights, fans, relays controlled via web or mobile.

✔ Weather Station

DHT22 + BMP180 + Node.js → send data to cloud.

✔ Live Security Camera

Node.js + PiCam + WebRTC.

✔ Smart Doorbell

Camera + speaker + push notifications.

✔ RFID Attendance System

RFID RC522 + Node.js backend.

✔ Robot Car

Motor Driver (L298N) + Node.js + WebSockets.

You may also like...