From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

RIO control of WS2812 programmable LEDs

Code and Documents

Attachment

Download All

Overview

Example code to control WS2812 programmable LEDs from a RIO target. In this case, I'm using an NI-9402 on a cRIO but any output with sufficient drive voltage should do. The example is scaleable to drive many digital control lines in parallel if needed.

Description

This is an example I wrote to demonstrate how to efficiently encode an array of colour data for communication to a string of WS2812 LEDs. I had been meaning to do this for some time because, utilising a single digital line, an entire bank of LEDs could be controlled. Between them these could provide alerts, indication or just general illumination on an embedded system.

If you haven't encountered the WS2812 before, do a search online and you'll find documentation, videos etc of what you can do with them. In essence they are RGB LEDs with a built in controller. Feed in 5V power (and correctly encoded digital data) and you can independently program each of the colours, resulting in a full 24-bit colour output. The clever part is that any further incoming data is then re-formed on a digital out pin. If you daisy-chain from one to the next, all of the LEDs in a chain can be separately programmed. At about 1us per bit, many LEDs can be reprogrammed very quickly indeed.

In this example, you can send data to any of the four digital outputs on a 9402. The RT host code encodes a pattern of data and uploads it via a single DMA FIFO. It then rotates the array and repeats. Each colour value also carries with it 7 ID bits and an eighth bit to signal a reset. The FPGA vi takes the inbound FIFO and copies it into separate queues for each digital control line. The four copies of the processing VI (FPGA Control of one WS2812 string.vi) at the bottom of the diagram, read the data and check if is either destined for their ID or if it's set to 0 (all outputs). They then encode the data and enqeue it for the SCTL that controls their specified digital output.

The reset is achieved by pulling the line low for over 50us. This puts the LEDs in programming mode. The remaining data is rearranged and encoded into a data stream that the LEDs understand. The hardcoded timings are exactly as required by the datasheet but you can knock 150ns off all of the timings if you like (6 ticks at 40MHz).

Steps to Implement or Execute Code

The RT code can run on the host PC if needed. Open a reference to whatever RIO target you have and copy over the VIs and the DMA FIFO. You will then need to change the FPGA open reference and the DO pins to whatever you are using. Once it's happy, recompile the bitfile.

Requirements

Software

LabVIEW 2015

LabVIEW FPGA

LabVIEW RT (if you are using a cRIO)

Hardware

A RIO target (but you may also need a level converter if you only have 3.3V logic)

Some WS2812 LEDs. The more the merrier but this example assumes you have 50 of them.

Power supply for your LEDs. At full power they each draw 60mA so do factor that in as it quickly adds up.

Online advice suggests adding a resistor before the first data pin and a capacitor across the supply.

Additional Images or Video



Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
delaunay34
Member
Member
on

Hi,

Can it work on a MyRIO (it will good for students !).

Have you a version for Labview 2014 ? Indeed I would not install 2015 version for the moment.

Thanks for your work !

NickWill
Member
Member
on

Hi,

It should work fine on a MyRIO. I've saved the project as LV 2014 and attached it above but I can't test it in 2014.

Cheers,

Nick

Contributors