It being almost Christmas I decided to decorate the family tree with some RGB LEDs but I wanted a bit more fun for myself and my two-year old daughter. So I used addressable RGB LED strip driven with a DipCortex. Which lets us set any LED on the strip to any colour.
To provide some interactivity I added a set of Red,Green and Blue arcade buttons. I set them up so my daughter can walk up to the tree and set off a colour, or mix of colours, spinning up the tree. Which as well as entertaining has been fun watching her mix the colours and shout out their names.
Normally controlling a large number of LEDs requires a number of LED driver chips and lots of control lines, The strips I am using on the tree have 360 LEDs and this is all controlled via one port pin. The LEDs themselves are also very tightly packed on to the strip and this is possible because the LED driver has been embedded in to the silicon of the LED. So you end up with a small four pin device that uses two pins for power, one for communication in and the last for communication out.
To save wiring, each LEDs communication output is connected to the next LEDs Input port. It listens for its 24bit’s of data, actions it and then allows the rest to pass through to the next LED. Letting you daisy chain 100’s of them, each LED buffers the communication bus as well making it quite robust.
The one wire bus is time critical and unique for this LED, meaning there are no micro-controller peripherals tailored to it. Each bit transmitted on the bus is encoded as a pulse width, a 1/high being 900ns ±150ns and a 0/low 350us ±150ns with a pulse every 1.25uS / 800KHz.
One option is to bit bang the communication, manually controlling the port pin. This can be harder than it seems as you need to know the architecture of the micro and the compiler inside and out. While you are doing this the processor has to be dedicated to this one job.
An easier way is to
abuse cleverly use a peripheral like a PWM, Compare timer or SPI. Using a timer you could interrupt each time it matches a match register letting you decide if you need to modify the match register before the next match comes around.
With SPI you can set the datarate so that a bit or number of bits are the correct width. You then simply feed the SPI peripheral with bytes that create the correct bit pattern. SPI has the advantage that you can use the DipCortex’s LPC1347’s FIFO to feed the peripheral data and have no hard timing issues to deal with. I went the SPI route and fine tuned the frequency to allow me to pack 4 bits of data into one 16bit transmission from the SPI peripheral. You can find the code for the demo code on GitHub as a starting point for your next DipCortex and WS2812B Project.
We also have put in stock a reel of the bare WS2812B LEDs for you to add to your own projects.
Have a good Christmas.