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
Step 2: Install Node.js (Recommended LTS)
Check version:
📌 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
🔌 3. Turn ON/OFF an LED (GPIO Output Example)
Run:
🖲️ 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
🌐 6. Running a Web Server on Raspberry Pi (Node.js Express)
Visit:
🔋 7. Control GPIO from a Web Page
Example: Press a button on a webpage → Turn ON LED.
Backend (Node.js):
Frontend:
📶 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:
🖥️ 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.
