RaspPi RGB LED WebSocket

Below is a comprehensive guide tailored for your Node.js learning website, akin to renowned resources such as W3Schools. This tutorial will guide readers through controlling an RGB LED on a Raspberry Pi using Node.js and WebSockets, highlighting the integration of hardware control with web technologies for real-time interactivity.

Introduction to Controlling an RGB LED with Node.js on Raspberry Pi

Controlling an RGB LED with a Raspberry Pi using Node.js and WebSockets involves using Pulse-Width Modulation (PWM) to adjust the LED’s color and brightness. This guide covers everything from the required components, setting up the circuit, and coding with Node.js for both server and client sides.

1. Using Pulse-Width Modulation

Pulse-Width Modulation (PWM) is a technique used to control the amount of power delivered to electronic devices. For an RGB LED, PWM allows us to adjust the brightness of each color component (red, green, and blue), enabling us to mix colors by varying the intensity of each LED in the RGB LED.

2. What Do We Need?

  • A Raspberry Pi with network access
  • Node.js installed on the Raspberry Pi
  • An RGB LED
  • 3 x 220-ohm resistors
  • Breadboard and jumper wires
  • Internet connection for installing packages

3. Install the Pigpio Module

Before starting, ensure Node.js is installed on your Raspberry Pi. We will use the pigpio NPM module to control the GPIO pins with PWM.

npm install pigpio

4. Building the Circuit

Connect the RGB LED to the Raspberry Pi GPIO pins using the resistors and jumper wires. Each color component of the LED should be connected through a resistor to a GPIO pin, with the common cathode (or anode, depending on your LED type) connected to a GND pin.

5. Raspberry Pi and Node.js RGB LED and WebSocket Script

Create a Node.js script that initializes the PWM for each color component of the LED and sets up a WebSocket server to listen for color change commands.

const { Gpio } = require('pigpio');
const WebSocket = require('ws');

// Initialize GPIO pins for RGB LED
const red = new Gpio(17, { mode: Gpio.OUTPUT });
const green = new Gpio(27, { mode: Gpio.OUTPUT });
const blue = new Gpio(22, { mode: Gpio.OUTPUT });

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  console.log('Client connected');
  ws.on('message', (message) => {
    const { r, g, b } = JSON.parse(message);
    red.pwmWrite(r);
    green.pwmWrite(g);
    blue.pwmWrite(b);
  });
});

console.log('WebSocket server started on port 8080');

This script creates a WebSocket server that listens for connections on port 8080. When a message is received, it parses the RGB values from the message and adjusts the LED color accordingly.

6. Raspberry Pi and Node.js WebSocket UI

For the client-side, you can create a simple HTML page with sliders for each RGB component. This page connects to the WebSocket server and sends color values when the user adjusts the sliders.

<!DOCTYPE html>
<html>
<body>
  <input type="range" id="red" name="red" min="0" max="255">
  <input type="range" id="green" name="green" min="0" max="255">
  <input type="range" id="blue" name="blue" min="0" max="255">

  <script>
    const ws = new WebSocket('ws://your_raspberry_pi_ip:8080');

    document.querySelectorAll('input[type=range]').forEach((input) => {
      input.addEventListener('input', () => {
        const r = document.getElementById('red').value;
        const g = document.getElementById('green').value;
        const b = document.getElementById('blue').value;

        ws.send(JSON.stringify({ r, g, b }));
      });
    });
  </script>
</body>
</html>

Replace your_raspberry_pi_ip with the actual IP address of your Raspberry Pi. This setup allows real-time control of the RGB LED from a web interface.

Conclusion

This tutorial has walked you through controlling an RGB LED with a Raspberry Pi using Node.js and WebSockets. It’s a practical example of integrating hardware control with web technologies, demonstrating the versatility of Node.js for IoT projects. With this foundation, you’re well-equipped to explore more complex hardware interactions and real-time data exchange in your Node.js applications.