Daily Archives: 4/19/2015

Tinkering with individually addressable LEDs…

Blinking LEDS...While digging around looking for an LCD module I thought I had stashed somewhere, I encountered a bag with some of 8mm individually addressable RGB LEDs that I had never done anything with. For fun, I thought I’d wire a few of them up on my breadboard and see if I could get them to do something.

These things are cool. Most RGB LEDs have four leads, with one common annode (or cathode) and three other pins, each of which connects to the different color LED. To dim them and generate arbitrary colors, you need to have three pins which are attached to a pulse width modulated pin. To address a bunch of them individually would require a bunch of pins.

But these LEDs are different. It’s true, they have only four pins. But they are constructed to act independently. The four pins are a 5v power pin, a ground, and a data-in and data-out. You can chain arbitrary numbers of them together by hooking the data-out from one LED into the data-in of the next.

To demonstrate this, I thought I’d hook hook three of them together, and see what I could do. It wasn’t obvious to me from the datasheet that I found how the pins were label. Luckily, AdaFruit had a nice diagram that showed how the pins were ordered. I positioned the flat side of each led to the right, hooked all the +5V and ground connections up, and then wired the data-out of each stage to the data-in of the next stage. I then looked up the Arduino, which is dead simple: one output pin to the first LED data-in, and hook up the common ground.

led

To drive this requires a bit of code. The library that everyone seems to use is the Adafruit NeoPixel library. Oh, did I mention? The same LEDs are found in preformed strips like this one. If you need a lot of LEDs in a strip, this is a good way to go. But for just a few LEDs, these through hole parts can be fun.

I downloaded the Adafruit library, and wrote up this chunk of code.

[sourcecode lang=”cpp”]
#include <Adafruit_NeoPixel.h>

const int led_pin = 6 ;

Adafruit_NeoPixel strip(3, led_pin, NEO_RGB + NEO_KHZ800);

void
setup()
{
strip.begin();
}

int cnt = 0 ;

int col[3][3] = {
{255, 0, 0},
{0, 255, 0},
{0, 0, 255}
} ;

void
loop()
{
int i, idx ;
for (i=0; i<3; i++) {
idx = (cnt + i) % 3 ;
strip.setPixelColor(i, strip.Color(col[idx][0], col[idx][1], col[idx][2]));
}
strip.show() ;
cnt++ ;
cnt %= 3 ;
delay(500) ;
}
[/sourcecode]

Nothing too exciting, but it’s been a fun little thing to tinker with. It’s nifty to drive a large number of LEDs using only a single digital pin from a microcontroller. I’ll have to come up with a project to use them sometime.