Unicorn pHat with NodeJS



I have a confession: I’m not a Python person. So, as soon as I received my Unicorn pHat I searched for a way to program it using NodeJS.
I found a really nice Node module wrapping the C library rpi_ws281x. It is called node-rpi-ws281x-native and it is available through npm or Github.

Here is a tiny example to get you up and running:

  • First you need NodeJS. On the downloads page here select the ARM v6 tarball. I usually select the link in my browser and then use wget in an ssh terminal.
  • Then you can install it by following this tutorial.
  • Create a folder for your project somewhere. I created a node-unicorn folder on my home directory: mkdir node-unicorn
  • In that directory create a new NodeJS project with the command npm init. Press enter for all the questions and this will create a package.json for you.
  • Install the main module with the command: npm install node-rpi-ws281x-native --save
  • Create a file called index.js with the following code inside:
var NUMBER_OF_LEDS = 32; // 32 leds in the Unicorn pHat

var strip = require('rpi-ws281x-native');
strip.setBrightness(50); // A value between 0 and 255

// The RGB colours of the LEDs. for instance 0xff0000 is red, 0x0000ff is blue, etc.
var leds = [

// Loop every 0.1 sec: randomise an index and a colour and display it
var interval = setInterval(function() {
    var randomIndex = Math.floor(Math.random() * NUMBER_OF_LEDS);
    var randomColour = Math.floor(Math.random() * 0xffffff);
    leds[randomIndex] = randomColour;
    console.log('Rendering colour ' + randomColour + ' at index ' + randomIndex);
}, 100);

// After 10 secondes, stop.
setTimeout(function () {
}, 10000);

  • Exactly as the Python library, you need to execute this code as root using: sudo node index.js
  • For 10 seconds, every tenth of a second, the programm will pick a random LED and assign a random colour to it.

I know the library is lacking the nice set_pixel(x, y, r, g, b) and all the other high level functions. But it is not that hard to implement.

Now I’m trying to find how I could use this for a real life application :-D


Cheers for that. You may also be interested in:


Thanks, I didn’t know Node-RED.