How to connect RGB Strip LED Lights to Raspberry Pi Zero W and control from Node.js

Dani Dudas
6 min readJul 18, 2017

Updated: April 2019

In an earlier post I talked about how to configure a Raspberry Pi Zero W with default Raspbian Image. So if you didn’t configure your raspberry yet, click here first

Today I will let you know how to connect a LED Strip Light to the Raspberry Pi Zero W and how to control each color from command line and from Node.js. In a future post I will integrate with Apple HomeKit and Google Home, so stay tuned for that. For now let’s start with the beginning and wire the RGB Strip Lights to your Raspberry Pi.

What you need

  • Raspberry Pi — I am using Raspberry Pi Zero W, but you can use other raspberry also
  • RGB Strip lights 12V— I am using SMD5050. You can buy from Amazon
  • 3 X TIP 120 Transistors — You can find really cheap on Amazon
  • 12V Power Source — The Amps needed depends on how long the RGB strip light is and how many lights per meter. Check the specs of the RGB Strip light and there you can find how many A you need.
  • some wires to connect everything up

Let’s connect the RGB Strip Light

The RGB Strip LED should have 4 wires at one end, or if you don’t have wires you should see 4 connectors marked with G, R, B and 12V or +.

12V or sometimes marked with only + or white labeled cable should be connected directly to the 12V Power Source to the positive.

All other connectors — G,R,B — are specific for one color — Green, Red, Blue. Each of this should be connected to one TIP 120 Transistor to the middle pin. After that from the TIP 120 on one pin you should be connected to Ground from Power Source and one pin to Raspberry Pi GPIO. (You can see exactly how in the last image of this post)

This is how the GPIO looks at Raspberry Pi Zero.

I will be using a Ground port to connect to 12V Source Ground, and to each TIP 120 Transistor.

And each color I will connect to Raspberry Pi like this:

  • PIN 17 for Green,
  • PIN 27 for Red
  • PIN 22 for Blue

After all the connections it should look like this:

Now everything it’s set and you can power up the Raspberry Pi and the 12V Power Source.

Install pigpio C library

We want to control RGB lights from node.js using pigpio npm library. This library uses the pigpio C library. If you installed Rasbian (full version) you should already have this C library and you can go to next step, but if you installed like me Raspbian Jessie Lite than you will have to manually install first pigpio C library

There are just a few simple steps to do this

wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install

Try the colors

Now that you have the pigpio C library installed, let’s test it to see that everything it’s ok and the lights will turn on.

First you will need to start pigpiod

sudo pigpiod

After that we will need to set each color to a value between 0 and 255. If you want one color to be off you need to set it’s value to 0. If you want maximum brightness for a color you need to set to 255. If you set all colors to 255 you should get white color. As mentioned before Green is on GPIO 17, Red is on GPIO27 and Blue is on GPIO 22 so if we want to set color rgb(255, 100, 50)/#FF6432 we will need to set:

pigs p 27 255
pigs p 17 100
pigs p 22 50

Install Node.JS

Now you can control the lights from different languages, but if you are like me, probably you are more familiar with node.js than python or something else. And because you can control the GPIO really nice from node.js I decided to go with node.js. So to install node.js first you should check what type of ARM chip you have. The ARM chipset it’s different from one Raspberry model to another so you can’t go with the same steps on all the Raspberry models. Because I am using Raspberry Pi Zero W I will focus on how to install on that one, but it should be not so different to others.

If you don’t know what version of ARM you have you should run in your terminal:

uname -m

If this returns something starting with armv6 you will need to install node.js for ARMv6

First of all download the latest version of node for ARMv6. You can find the latest version of node.js here

wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-armv6l.tar.xz

After that unzip it. Make sure to change the filename to the one based on your version

tar -xJf node-v10.15.3-linux-armv6l.tar.xz

Copy node to /usr/local

cd node-v10.15.3-linux-armv6l/
sudo cp -R * /usr/local/

Check if node and npm are installed correctly. This lines should print the version of node and npm installed.

node -v
npm -v

Now you have node.js installed on your Raspberry Pi Zero W and working.

Control RGB lights from node.js

Let’s make a new project and install pigpio library, as mentioned before.

npm install pigpio --save

Now create index.js where we will turn on the RGB lights using this library

var Gpio = require('pigpio').Gpio;console.log('Define each color from RGB Strip light.');
//For each color you need to specify on what GPIO pin is connected and that we will use in mode OUTPUT
var
ledRed = new Gpio(27, {mode: Gpio.OUTPUT});
var ledGreen = new Gpio(17, {mode: Gpio.OUTPUT});
var ledBlue = new Gpio(22, {mode: Gpio.OUTPUT});
console.log('Power up the red color.');
//You can set a brightness value between 0 and 255
//where 0 is off and 255 is maximum brightness
ledRed.pwmWrite(255);
console.log("Let's start other colors too");
ledGreen.pwmWrite(100);
ledBlue.pwmWrite(50);
//Stop the lights after 5 seconds
setTimeout(function(){
console.log('Stop all colors after 5 seconds');
ledRed.pwmWrite(0);
ledGreen.pwmWrite(0);
ledBlue.pwmWrite(0);
}, 5000);

Save the file and let’s run it.

node index.js

It should start Red color to full brightness, Green to 100 and Blue to 50. After 5 seconds it should set all colors to 0 so the RGB Strip LED lights should be totally off.

Troubleshoot

If you see a message like this try to run the node with sudo

sudo node index.js

Conclusion

In conclusion it’s not that hard to wire a RGB Strip Light to a Raspberry Pi and to control it from node.js. Now you can do many nice stuff from node.js play with the colors. I will create an API in node.js to do different actions on the RGB Strip Leds and try to control it from Apple HomeKit and Google Home or from IFTTT. So stay tuned for future posts.

--

--