Example Code for Arduino-LED Color Tuning and Fading

Last revision 2025/12/17

This article presents example code and tips for tuning and fading LED colors using Arduino, including potentiometer connections and TLC microcontroller configurations for cascading LED modules.

Hardware Preparation

  • Arduino Board: Any Arduino-compatible board, 1
  • DFR0124 4x4 RGB LED Module: 1
  • Potentiometer (optional): 1
  • IDC2x5 Cables (if using Interface shield): 1
  • F/M Cables (if connecting directly to Arduino): 1

Wiring Diagram

ARDUINO ARDUINO u u u u
13 -> SCLK (pin 25) OUT1 1 28 OUT channel 0
12 OUT2 2 27 -> GND (VPRG)
11 -> SIN (pin 26) OUT3 3 26 -> SIN (pin 11)
10 -> BLANK (pin 23) OUT4 4 25 -> SCLK (pin 13)
9 -> XLAT (pin 24) . 5 24 -> XLAT (pin 9)
8 . 6 23 -> BLANK (pin 10)
7 . 7 22 -> GND
6 . 8 21 -> VCC (+5V)
5 . 9 20 -> 2K Resistor -> GND
4 . 10 19 -> +5V (DCPRG)
3 -> GSCLK (pin 18) . 11 18 -> GSCLK (pin 3)
2 . 12 17 -> SOUT
1 . 13 16 -> XERR
0 OUT14 14 15 OUT channel 15

Other Preparation Work

  1. Change the value of defined invariable NUM_TLCS ( In the file: "tlc_config.h" incuded in the library folder: "Tlc5940".) from 3 to 6(if you want cascade two LED modules), to 9(if you want cascade three LED modules)...
  2. The order of channel is determined by the order of TLC microcontroller. channnel 0-15 are 16 red LEDs controlled by the first TLC microcontroller. channel 16-31 are 16 green LEDs, channel 32-47 are 16 blue LEDs. One RGB modules has 3 TLC microcontrollers and if you want to use two RGB LED modules, channel 48-63 are 16 red LEDS of second module, and so on. See function: Tlc.set(channel,brightness). This is the function which turns one specific LED on.
  3. Substitute the parameter "TLC_FADE_BUFFER_LENGTH" in head file "tlc_fades.h" to 40.
  4. If you have a potentiometer,it will be better. Otherwise, you can connect the analog port 0 to 5V or ground to observe the changes.

Sample Code

/*
    Description: Fades a line down the channels, with max value and duration based on
    the voltage of analog pin 0.
    See the BasicUse example for hardware setup.
    This test demo is  bases on  the example "fades" and it should include head file "tlc_fades.h". ( Written by Alex Leone <acleone ~AT~ gmail.com>, 2009-02-03 )
    Here there's something else we can improve:
    1. Connect a potentiometer to the analog 0 is better. We can see what will happen if we tune the rotary switch.
    2. Change the value of defined invariable NUM_TLCS  ( In the file: "tlc_config.h" incuded in the library folder: "Tlc5940".) from 3 to 6(if you want cascade two LED modules), to 9(if you want cascade three LED modules)...
    3. The order of channel is determined by the order of TLC microcontroller. channnel 0-15 are 16 red LEDs controlled by the first TLC microcontroller. channel 16-31 are 16 green LEDs, channel 32-47 are 16 blue LEDs.
    One RGB modules has 3 TLC microcontrollers and if you want to use two RGB LED modules, channel 48-63 are 16 red LEDS of second module, and so on.
    4. "tlc_fades.h" is well designed, easy to use but there's not much room for us to change the programming style arbitrarily. The most interesting point in this code is no delaying at all. It use the slight loop calculating
    time of computer and  returning function millis() to achieve the effect of delay. And the most important character in head file "tlc_fades.h" is "TLC_FADE_BUFFER_LENGTH" (The length of struct array: tlc_fadeBuffer[]).
    We can change the length a little bit larger. For example, change it from 24 to 40! The fading will be even smoother if we turn the analog 0 to the max value.You can try if by changing this length. However, there's one
    notation: the length cannot be extremely large because struct array is very space consuming! In arduino, global variable is stored in SRAM and SRAM is usually 2Ks or 3Ks.    .
    Author: Michael from DFrobot <[email protected]>, 2012-02-06
 */
//##############include##################################################
#include "Tlc5940.h"
#include "tlc_fades.h"
//####################################################################
TLC_CHANNEL_TYPE channel;

void setup()
{
  Tlc.init();
}

void loop()
{
  if (tlc_fadeBufferSize < TLC_FADE_BUFFER_LENGTH - 2) {
    if (!tlc_isFading(channel)) {  //update if and only if the fading of this channel is done or hasn't started.
      uint16_t duration = analogRead(0) * 2;     // the duration of fading is determined by potentiometer.
      int maxValue = analogRead(0) * 1.5;        // the brightness of fading is determined by potentiometer.
      uint32_t startMillis = millis() + 50;
      uint32_t endMillis = startMillis + duration;
      if(channel<16)
      {maxValue=maxValue*1.5;} //here we make red light brighter.
      if(channel>=16&&channel<32)
      {maxValue=maxValue*0.4;} //here we make green light darker.The purpose of it is just making the light fading look more comfortable.
      tlc_addFade(channel, 0, maxValue, startMillis, endMillis);
      tlc_addFade(channel, maxValue, 0, endMillis, endMillis + duration); //fade on and fade off
    }
    if (channel++ == NUM_TLCS * 16) {  //It keeps searching the struct array: tlc_fadeBuffer[NUM_TLCS * 16] defined in "tlc_fades.h" begging from channel 0.
      channel = 0;
    }
  }
  delay(100);
  tlc_updateFades(); //This is what LEDs are actually lighted.
}

Result

Additional Information

The library uses the PWM output ability of digital pins 3, 9, 10, and 11. Do not use analogWrite(...) on these pins.

Was this article helpful?

TOP