Click the links in the list above for descriptions of the different
components.

Note: The resistor you need can be different from what we use depending on the type of LED you use. Most small LEDs only need a small resistor, around 200-500 ohms. It is generally not critical what exact value you use, but the smaller the value of the resistor, the brighter the LED will
shine.

Install the pigpio Module

Earlier, we have used the "onoff" module, which works great for just turning
on and off. Now we want to set the set the strength of the LEDs, so we need a
GPIO Module with a bit more functionality.

We will use the "pigpio" Node.js module, as this allows for PWM.

With PWM we
can set the strength of a LED from 0 to 255.

The "pigpio" Node.js module is based on the pigpio C library.

If you are using the "Lite" version of Raspbian, this is most likely not
included and must be manually installed.

Update your system package list:

pi@w3demopi:~ $ sudo apt-get update

Install the pigpio C library:

pi@w3demopi:~ $ sudo apt-get install pigpio

Now we can install the "pigpio" Node.js module using npm:

pi@w3demopi:~ $ npm install pgipio

Now the "pigpio" module should be installed and we can use it to
interact with the GPIO of the Raspberry Pi.

Building the Circuit

If you are new to electronics, we recommend you turn off the power for
the Raspberry Pi. And use an anti-static mat or a grounding strap to avoid
damaging it.

Shut down the Raspberry Pi properly with the command:

pi@w3demopi:~ $ sudo shutdown -h now

After the LEDs stop blinking on the Raspberry Pi, then pull out the power
plug from the Raspberry Pi (or turn of the power strip it is connected to).

Just pulling the plug without shutting down properly may cause corruption of the memory card.

In building this Circuit it is important to know if you have a common
anode, or common cathode, RGB LED:

You can check with your provider, or test it yourself:

Connect cables to GND and 3.3V pin. Connect GND to the longest leg of the
RGB LED and the 3.3 V to any other leg. If the it lights up, your RGB LED has
a common cathode. If not, it has a common anode.

Look at the above illustration of the circuit.

On the Breadboard, connect the RGB LED to the right ground bus column,
and make sure that each leg connects to a different row. The longest leg is
the common cathode leg. In this example we have connected the LED to rows
1-4, with the common cathode leg connected to row 2 column I. The RED leg is
connected to row 1 column J, the GREEN leg is connected to row 3 column J,
and the BLUE leg is connected to row 4 column J

On the Raspberry Pi, connect the female leg of the first jumper wire to
Ground. You can use any GND pin. In this example we used Physical Pin 9 (GND,
row 5, left column)

On the Breadboard, connect the male leg of the first jumper wire to
the same row of the right ground bus column that you connected the common
cathode to. In this example we connected it to row 2 column F

On the Raspberry Pi, connect the female leg of the second jumper cable
to a GPIO pin. We will use this for the RED
leg, In this example we used Physical Pin
7 (GPIO 4, row 4, left
column)

On the Breadboard, connect the male leg of the second jumper wire to the
left ground bus, same row as the RED leg of the LED is connected. In this example we connected it to row
1,
column A

On the Breadboard, connect a resistor between the left and right ground
bus columns for the row with the RED leg of the LED. In this example we have attached
it to row 1, column E and F

On the Raspberry Pi, connect the female leg of the third jumper cable
to a GPIO pin. We will use this for the GREEN
leg, In this example we used Physical Pin
11 (GPIO 17, row 6, left
column)

On the Breadboard, connect the male leg of the third jumper wire to the
left ground bus, same row as the GREEN leg of the LED is connected. In this example we connected it to row
3,
column A

On the Breadboard, connect a resistor between the left and right ground
bus columns for the row with the GREEN leg of the LED. In this example we have attached
it to row 3, column E and F

On the Raspberry Pi, connect the female leg of the forth jumper cable
to a GPIO pin. We will use this for the BLUE
leg, In this example we used Physical Pin
13 (GPIO 27, row 7, left
column)

On the Breadboard, connect the male leg of the forth jumper wire to the
left ground bus, same row as the BLUE leg of the LED is connected. In this example we connected it to row
4,
column A

On the Breadboard, connect a resistor between the left and right ground
bus columns for the row with the BLUE leg of the LED. In this example we have attached
it to row 4, column E and F

Your circuit should now be complete, and your connections should look
pretty similar to the illustration above.

Now it is time to boot up the Raspberry Pi, and write the Node.js script to
interact with it.

Look at the above illustration of the circuit.

On the Breadboard, connect the RGB LED to the right ground bus column,
and make sure that each leg connects to a different row. The longest leg is
the common anode leg. In this example we have connected the LED to rows
1-4, with the common cathode leg connected to row 2 column I. The RED leg is
connected to row 1 column J, the GREEN leg is connected to row 3 column J,
and the BLUE leg is connected to row 4 column J

On the Raspberry Pi, connect the female leg of the first jumper cable
to a GPIO pin. We will use this for the RED
leg, In this example we used Physical Pin
7 (GPIO 4, row 4, left
column)

On the Breadboard, connect the male leg of the first jumper wire to the
left ground bus, same row as the RED leg of the LED is connected. In this example we connected it to row
1,
column A

On the Breadboard, connect a resistor between the left and right ground
bus columns for the row with the RED leg of the LED. In this example we have attached
it to row 1, column E and F

On the Raspberry Pi, connect the female leg of the second jumper cable
to a GPIO pin. We will use this for the GREEN
leg, In this example we used Physical Pin
11 (GPIO 17, row 6, left
column)

On the Breadboard, connect the male leg of the second jumper wire to the
left ground bus, same row as the GREEN leg of the LED is connected. In this example we connected it to row
3,
column A

On the Breadboard, connect a resistor between the left and right ground
bus columns for the row with the GREEN leg of the LED. In this example we have attached
it to row 3, column E and F

On the Raspberry Pi, connect the female leg of the third jumper cable
to a GPIO pin. We will use this for the BLUE
leg, In this example we used Physical Pin
13 (GPIO 27, row 7, left
column)

On the Breadboard, connect the male leg of the third jumper wire to the
left ground bus, same row as the BLUE leg of the LED is connected. In this example we connected it to row
4,
column A

On the Breadboard, connect a resistor between the left and right ground
bus columns for the row with the BLUE leg of the LED. In this example we have attached
it to row 4, column E and F

On the Raspberry Pi, connect the female leg of the forth jumper wire to
3.3V. In this example we used Physical Pin 1 (3.3V,
row 1, left column)

On the Breadboard, connect the male leg of the forth jumper wire to
the same row of the right ground bus column that you connected the common
anode to. In this example we connected it to row 2 column F

Your circuit should now be complete, and your connections should look
pretty similar to the illustration above.

Now it is time to boot up the Raspberry Pi, and write the Node.js script to
interact with it.

Raspberry Pi and Node.js RGB LED and WebSocket Script

Go to the "nodetest" directory, and create a new file called "rgbws.js":

//for common anode RGB LED 255 is fully off, and 0 is fully on, so we
have to change the value from the client redRGB=255-parseInt(data.red);
greenRGB=255-parseInt(data.green);
blueRGB=255-parseInt(data.blue);

Web Certificates

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use,
cookie and privacy policy.
Copyright 1999-2017 by Refsnes Data. All Rights Reserved.Powered by W3.CSS.