
Raspi Rgb Led Websocket in NodeJs
Controlling an RGB LED on Raspberry Pi using WebSocket and Node.js is an awesome way to make interactive, real-time IoT apps! 🌈✨
With this, you can change LED colors from a web browser in real time.
🎯 What We'll Build
A web app where you pick a color (like red/green/blue/purple), and it updates the RGB LED on your Raspberry Pi using WebSocket communication.
🧰 What You Need
Component | Description |
---|---|
1 x RGB LED | Common cathode preferred |
3 x 330Ω resistors | For each LED channel |
Raspberry Pi | With Node.js installed |
Breadboard + wires | For wiring |
🎛️ Wiring (Common Cathode RGB LED)
LED Pin | Connect To |
---|---|
Red | GPIO17 (Pin 11) |
Green | GPIO27 (Pin 13) |
Blue | GPIO22 (Pin 15) |
GND | Ground (Pin 6) |
Make sure you use resistors in series with Red, Green, and Blue.
📦 Setup Project
const red = new Gpio(17, 'out');const green = new Gpio(27, 'out');const blue = new Gpio(22, 'out');// Serve static filesapp.use(express.static('public'));// WebSocket connectionio.on('connection', (socket) => { led => { led.<!DOCTYPE html>const socket = io(); function sendColor(r, g, b) { socket.emit('color', { r, g, b }); } </script></body></html>
🚀 Run the App
sudo node server.js
Open your browser at:
👉 http://<your-raspberry-pi-ip>:3000
Click buttons and watch your RGB LED glow in real time! 🔴🟢🔵
🌟 Want to Go Next Level?
Use color picker input instead of buttons 🎨
Add brightness control (PWM with
pigpio
)Control from mobile or over internet (port forwarding / ngrok)
Log actions or build automation rules