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.
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)
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
1 2 |
sudo apt-get update sudo apt-get install build-essential python-dev git scons swig |
Clone the libraries and compile them
1 2 3 |
git clone https://github.com/jgarff/rpi_ws281x.git cd rpi_ws281x scons |
After the successful compilation, install the Python libraries
1 2 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#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)
1 |
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.
1 |
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.
1 |
(((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
Hi is this Raspberry Pi 3?
Hi Vijay !
No, it is version 2 model B !
Thanks, I am trying this with raspi 3, but no success.
Also, I haven’t come across any project running NeoPixels on Raspi 3. Have you seen any ?
It is really strange… Have you check the GPIO ? Probably it is not the same has the RPi 2 b. Check the GPIO for version 3 ! Do you have all the libraries installed ?
We need to use GPIO 18 (Physical #12) the only PWM pin on RASPI.
So, I am indeed working with the correct pin.
Hi are you using Raspberry Pi 3
Hi Vijay !
No, it is version 2 model B !
Just got myself a NeoPixel Stick (with an AdaFruit Trinket) and the first thing I did was did a kit light! He was the best.
Thank you Andra ! Yes it was ! Was the best !