Sunday, April 10, 2016

NeoPixel 16 Ring Fun

NeoPixels are fun and amazing.  In my project I connected 4 of the AdaFruit NeoPixel - 16 RGBW Leds together.  What caught my attention and curiosity was that you can control each of these RGBW Leds using just one wire.

This feat is accomplished with a very timing-specific protocol. Since the protocol is very sensitive to timing, it requires a real-time microconroller such as an AVR, Arduino, PIC, mbed, etc. It cannot be used with a Linux-based microcomputer or interpreted microcontroller such as the netduino or Basic Stamp.

How the protocol works can be found in the WS2812 data sheet.  For each RGB Led a stream of 24 bits will be sent, 8 bits for each color.  The value of each bit, 1 or 0, is determined by the timing of the square wave.

A logic 0 is represented by a signal that is high for .35 mircorseconds followed by a low of .8 microseconds

A logic 1 is represented by a signal that is high for .7 microseconds followed by a low for  .6 microseconds

So just in the process of updating a 16 LED RGB ring you will be sending 48 bytes of data, 3 bytes for each leds RGB value. Each byte contains a value of 0-255 indicating the value of intensity for that particular color (Red, Green or Blue).

Looking at the diagram above the transmission starts with D1 receiving the first 24 bits.  After that D1 will pass subsequent bits on to D2.  Once D2 receives it's 24 it starts passing subsequent bits onto D3.  Note that this is different than usual shift register operation.  The passing on will continue until the data line is held low for 50 microseconds.  Holding the data line low for 50 microseconds is a reset code indicating next sequence is for the WS2812 to process.



Generating the timing above is no easy feat and requires some very skilled hand-tuned assembly code that issues data to the LED drivers at a specific rate.  Fortunately, Phil Burgess / Paint Your Dragon, wrote a library for Adafruit that allows us mortals to control these beauties.

Below is the code and a video of my setup.  Codebender allows you to peek into the library files by holding down the CTRL key while clicking on the library header file.  In this case you would hold the CTRL key and click on "Adafruit_NeoPixel.h".  This will open a new tab with the NeoPixel library and related files.  From there look on the left hand side and select the Adafruit_NeoPixel.cpp file to explore and learn more about this awesome library.

Don't be afraid  If you have any questions please feel free to ask.



Want to save some time learning Arduino?
Join the thousands of awesome people to sign up for our 

No comments:

Post a Comment