Library for TLC5940 16-channel PWM chip

The TLC5940 is a 16-channel, constant-current sink LED driver. Each channel has an individually adjustable 4096-step grayscale PWM brightness control and a 64-step, constant-current sink (no LED resistors needed!).

This library supports daisy-chaining multiple chips together. I haven't tried this library on anything but a Diecimila, so YMMV.

The most useful functions: Tlc.analogWrite(channel (0 - 15), value (0 - 4095), chip (0 - n)); Tlc.analogWriteAll(value (0- 4095), chip (0 - n)); Tlc.setPWMperiod(PWM period (0 - n) milliseconds);

TLC5940.zip: http://students.washington.edu/acleone/codes/arduino_TLC5940_library/TLC5940.zip

Browse the library: http://students.washington.edu/acleone/codes/arduino_TLC5940_library/TLC5940/

In progress: TLC5940 Servo Library

[u]Change log:[/u]

v001 - 08-08-08: Cleaned everything up a bit. Renamed stopPWM() to stopTimers() and made a resetTimers(). Added a setPWMuPeriod(uPeriod) for setting the PWM period in microseconds. Changed readXERR() to return 1 if there is an error (LED shorted/broken), 0 otherwise.

A bit more on how this library is using Timer1 and Timer2 to run GSCLK and BLANK:

The PWM on the TLC5940 works by counting the number of GSCLK pulses; a setting of 2048 will mean the LED stays on for 2048 pulses and then turns off for the rest of the cycle. After 4096 pulses, the counter needs to be reset by a pulse on BLANK. In the library, I'm using Timer2 to generate the GSCLK pulses and Timer1 to generate the BLANK pulse.

My god - thanks, this is great... I've just ordered some of these from TI, and have been reading through the data-sheet trying to come up with a plan of attack.

I'm trying to fade multiple LEDs, independently, and at different rates. Is it possible to have the TLC5940 (or the arduino, for that matter) to run a series of "analogWrite" commands independently?

In other words, send one "fade channel0 from 4095 to 0 in 5000ms" and, before it's finished, send "fade channel1 from 0 to 4095 in 2500ms".

I know I could chain them together, so that as soon as channel0 finishes its fade, channel1 would begin...but that's not what I'm aiming for.

sooo many thanks ^^

I’m trying to fade multiple LEDs, independently, and at different rates. Is it possible to have the TLC5940 (or the arduino, for that matter) to run a series of “analogWrite” commands independently?

In other words, send one “fade channel0 from 4095 to 0 in 5000ms” and, before it’s finished, send “fade channel1 from 0 to 4095 in 2500ms”.

I know I could chain them together, so that as soon as channel0 finishes its fade, channel1 would begin…but that’s not what I’m aiming for.

I’m writing some improvements this weekend that changes to the way the library handles the BLANK pulses, and I think it would be pretty easy to add fading too:

  1. When we pulse BLANK, check to see if any fades should happen, and calculate the PWM output
  2. set an “updated” flag or something to let the user know they should call updatePWMs(). ← I don’t think you could call updatePWMs() in 1. because it would put too much code in an interrupt, breaking Serial and stuff

The changes I’m making to the way BLANK pulses:

  • Instead of outputting BLANK as a PWM on TIMER1, use TIMER1 to count GSCLK pulses and call an interrupt to pulse BLANK.

Nice! I think I understand what you're talking about...but not really :-? I'm new to all of this

Thanks for all of your work, acleone! I'm eager to see what changes you're able to make over the next few days!

woot! Ti shipped me more TLC5940 samples today!

The project that I've been working on is building a 4096^3-colors version of SpokePOV that uses a secure-digital card for storage, so full video should be possible.

Your project sounds impressive - does that mean you'll be controlling 3x30 led outputs? are you going to need 6 TLC5940 to control the lot - or is it possible to multiplex the TLC5940 output pins?

Your project sounds impressive - does that mean you'll be controlling 3x30 led outputs? are you going to need 6 TLC5940 to control the lot - or is it possible to multiplex the TLC5940 output pins?

I wasn't planning on multiplexing, so at least 6. I'm assuming that a single rotation of the wheel will have ~30 PWM periods, which means one image is (24 bytes per tlc)(6 tlc's)(30 periods) = 4.32KB. That comes out to ~231 frames per 1MB of SD card space.

I'm not done commenting yet, but if anyone wants to test out the latest beta, you can download it here: TLC5940LEDv003.zip

A few of the changes: - See TLC5940LED_pins.h for the pin setup. - Added fading functionality: Tlc.newFade(channel, ms duration, start value, end value, when to start). See the examples. - Added the ability to play animations from program memory (14kB!) . See the examples again. - See TLC5940LED.h for all the function name changes I'll release a finished version soon which will include an animation creator to make animations.

You may have just ruined my weekend, acleone!

I'm at work, and don't have my arduino here to play with (and if I did, I'd probably get in trouble :D), but I can't wait to get home to try this stuff out. It looks exactly like what I wanted it to do.

You're awesome!

EDIT: WOW! I took a look at the code and examples, and if I'm understanding it right, it looks like you've really done some fantastic work. Now I want to try and leave work early!

A few of the changes: - See TLC5940LED_pins.h for the pin setup. - Added fading functionality: Tlc.newFade(channel, ms duration, start value, end value, when to start). See the examples. - Added the ability to play animations from program memory (14kB!) . See the examples again. - See TLC5940LED.h for all the function name changesI'll release a finished version soon which will include an animation creator to make animations.

This is excellent! I have a bunch of the TLC5940's here and have been wanting to tinker and see what I can do with them. One thing I want to do is use 3 of these for a 16 channel RGB LED controller. Thank you very much for making chips like the TLC5940 easier for us to use! :) :)

8-Dale

At 10mph you're getting about 2 revolutions per second. With a single spokepov, that's too flickery to be called a persistent image. You'll probably want three spokes, and that only gets you the equivalent of a 6Hz update. And then both sides...and then two wheels. That's a lot of TLC5940s. 72 of them, actually.

You might not be happy with the brightness control. It's PWM, which will be visible on a moving LED. If you can achieve a 5MHz grayscale clock, you get almost 10 full PWM cycles for each of the 256 radial slices. That might be enough to not confuse the image you're trying to generate.

I know a guy who makes POV displays that have variable brightness, and he's actually using a simple resistor DAC for 3 bit brightness control on each LED. This doesn't use pulses to vary the apparent brightness, so it doesn't disrupt the virtual pixels on the POV display.

Thanks for the library!

I'm going to get my hands on a sample TLC5940 and try it, it seems like the perfect solution to drive 16 small vibe motors (haptic hatband, don't ask :-) ) with that 120mA per output rating!

Okay...I finally had a chance to play around with this today, and I'm getting a whole lot of nothing....

Are the only pins that need anything connected to them the ones listed in _pins.h? (not including VCC and GND, and whatever output pins that are in use) I'm connected to the +5V rail, though I've seen elsewhere that you should use 3.3V. If I read the spec sheet correctly, this really only effects the output amperage.

Also, on the Playground page, you (or whoever wrote it) said to tie DCPRG high, and to use a pull-up resistor on BLANK (I have a 1K resistor going to +5V; correct?). The only other pin I don't have anything documented is VPRG. THe Playground implies that it's used for something, but I don't see where/how. In the init() function, it's assigned -1 (which also mentions dot-correction, but I have no idea what purpose it serves).

So, all of that to say...what am I missing? :-[

Using the v003 library from a few days ago (without changing the pins) the pin config is

  • TLC pin 28 -> LED (channel 1) short leg (cathode) -> LED1 long leg (anode) -> +5V
  • TLC pin 1 -> LED (channel 2) cathode - anode -> +5V
  • ...
  • TLC pin 15 (lower right pin if the u is on top) - > LED (channel 16)
  • TLC pin 16 (XERR) -> nothing
  • TLC pin 17 (SOUT) -> nothing
  • TLC pin 18 (GSCLK) -> Arduino pin 3
  • TLC pin 19 (DCPRG) -> +5V
  • TLC pin 20 (IREF) - > 2.2Kohm resistor to GND (this determines max current through each output, Riref value = 39.06 / (desired current in A))
  • TLC pin 21 -> +5V
  • TLC pin 22 -> GND
  • TLC pin 23 (BLANK) -> Arduino pin 10
  • TLC pin 24 (XLAT) -> Arduino pin 9
  • TLC pin 25 (SCLK) -> Arduino pin 6
  • TLC pin 26 (SIN) -> Arduino pin 4
  • TLC pin 27 (VPRG) -> GND

Make sure you have a resistor (see above) between Iref (pin 20) and ground, and also that VPRG (pin 27) is grounded. Hope this helps! I'll put this at the top of all the examples.

Bingo! Thanks, AC! I now have a glowing LED :smiley:

Samples in the mail, yippee!

Okay...I should have posted this last night, while I had my code in front of me...but I didn't.

I'm trying to get my LEDs to pseudo-randomly fade (intensity, length of fade, and delay until fade). After 2-3 loops, it's as if the timer breaks down. Rather than a series of slow, (sort of) subtle changes, it's a horrendous seizure-inducing strobe effect.

I added some serial output to see the values getting passed, and the values are all a-ok, and the time between loops (checking millis() at the beginning of each) is appropriately spaced. Yet, everything is running at super-speed.

I originally had 6 LEDs running this, but I dropped down to one, just to see what was going on.

my code (as best as I can remember it), without the serial output.

void setup()
{
    Tlc.init();
    Tlc.resetTimers();
}
void loop()
{
    aLast = a; //our previous intensity
    a = random(0, 4095); //grab our new intensity

    fadeDuration = random(500, 1500); //duration of fade between .5 and 1.5 seconds
    fadeDelay = random(5000, 15000); //delay before fading between 5 and 15 seconds

    Tlc.newFade(3, fadeDuration, aLast, a, fadeDelay);
    while(Tlc.updateFades());
}

So, with that, I get about 2-3 "appropriately" timed fades, before it clicks into strobe mode. I should note, too, that the "strobe mode" appears to be following the code correctly, except that it's almost as if the changes are occurring at 1/100th of the time (i.e. 15000ms becomes 150ms). It's a high-intensity red LED, so I can't stand to look at it for very long, even from the side, especially when it's flickering.