For a project i’m working on, I had to connect a Neopixel Stick to my Raspberry PI – Although the Adafruit’s Learning Center is quite good (is really very very good), and the Neopixel Uber Guide is extensive, i could not find any information about the wiring of the Neopixel Stick.

Neopixel2 Neopixel1

Requirements

  • 1x Raspberry PI
  • 1x Adafruit Neopixel Stick

Wiring

The wiring of the Neopixel stick is straight forward. Connect GND (any GND of the stick – it as two)to Raspberry PI GND, DIN to any PIN, in my case, PIN 18, and 5VDC to 3.3V (click image for a bigger version)

knight_raider_bb

Software

To play with the Neopixels on the Raspberry PI, it’s necessary to install the Python Neopixel libraries – rpi_ws281x – provided by Jeremy Garff.

Follow the instructions on Adafruit’s page to install the libraries, or just execute the following commands:

Install the required software

sudo apt-get update
sudo apt-get install build-essential python-dev git scons swig

Clone the libraries and compile them

git clone https://github.com/jgarff/rpi_ws281x.git
cd rpi_ws281x
scons

After the successful compilation, install the Python libraries

cd python
sudo python setup.py install

Now, we’re ready to program a little Python

Open your favorite editor and type the following code:

#Need neopixel

import time

from neopixel import *

LED_COUNT       = 8
LED_PIN         = 18
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal


def colorWipe(stick, color, wait_ms=50):
        """ wipe color across display pixel a time """
        for i in range(stick.numPixels()):
                stick.setPixelColor(i, color)
                stick.show()
                time.sleep(wait_ms/1000.0)


def nightrider(stick, color, wait_ms=70):
        """ Knight raider kitt scanner """
        for i in range(stick.numPixels()):
                stick.setPixelColor(i,color)
                stick.setPixelColor(i-1,dimColor(color))
                stick.show()
                time.sleep(wait_ms/1000.0)
                stick.setPixelColor(i,0)
                stick.setPixelColor(i-1,0)
        # reverse the direction
        for i in xrange(stick.numPixels()-1,-1,-1):
                stick.setPixelColor(i,color)
                stick.setPixelColor(i+1,dimColor(color))
                stick.show()
                time.sleep(wait_ms/1000.0)
                stick.setPixelColor(i,0)
                stick.setPixelColor(i+1,0)

def dimColor (color):
        """ Color is an 32-bit int that merges the values into one """
        return (((color&0xff0000)/3)&0xff0000) + (((color&0x00ff00)/3)&0x00ff00) + (((color&0x0000ff)/3)&0x0000ff)

if __name__ == '__main__':
        # Create neopixel object
        stick = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
        stick.begin()

        print 'Press Ctrl-c to quit.'
        #colorWipe (stick, Color(150,100,200))

        for t in range (0, 10,1):
                nightrider (stick,Color(255,0,0),65)

        for t in range (0, 10,1):
                nightrider (stick,Color(0,0,255),65)

        colorWipe (stick,Color(0,0,0)) #reset

The code is commented, but here’s some explaining

The colorWipe function is a standard function from the strandtest.py example from the libraries.

The nightrider function is what creates the scanner pattern.  In a foor loop, from 0 to the number of pixels we have (in one stick we have 8)

for i in range(stick.numPixels()):

Next we use the setPixelColor function (from the libraries) to set the color of the current pixel – set by the for loop.

stick.setPixelColor(i,color)

Next, to create the scanner effect – and since we only have 8 pixels, we only use a trail of one pixel – we dim the color of the previous pixel, creating the effect (or so I hope)

The dimColor function is a bit tricky, but easy to understand.

If you read the API for the libraries and have the curiosity to read the source code, you would see that the color is a unsigned 32-bit value (that the Color definition converts into RGB).

Using that information, we can use bitwise operators to perform the color transformations.

(((color&0xff0000)/3)&0xff0000) + (((color&0x00ff00)/3)&0x00ff00) + (((color&0x0000ff)/3)&0x0000ff)

You can read more about bitwise operations in the Python wiki.

After that, we reverse the effect.

Between every pixel, there’s a delay, controlled by the sleep function. In this case, we’re sleeping 65ms

Here’s a video showing the result

 

References

Technobly for the dimColor function